Hi all, I’ve hit a complete wall. The tutorial class I’m following is using Blueprints, and I am converting it into c++ which was going fine until he created the LocalWeapon (local variable) as a class reference in BPs by promoting the output node; is there anyway to write this in c++? I can’t seem to find an answer anywhere.
I assume BP Weapon Base
derives from AActor
. You can make a pointer to such an object using TSharedPtr<AActor>
. However, this only gives you access to the AActor
methods and functions, not whatever custom code you’ve added to BP Weapon Base
.
This is the drawback of putting code in blueprints: You can’t easily reference blueprint-only data structures in C++. You have to laboriously look up the per instance reflection info to try to read/write properties and call functions that way – this is not great.
Thus, the way to do this conversion, is to ALSO convert the class of BP Weapon Base into C++, name it something like AWeaponBase
. You can still have each weapon instance be a subclass of your C++ AWeaponBase
, and you can even re-parent BP Weapon Base
to derive from AWeaponBase
instead of AActor
or whatever, but the C++ code will use a TSharedPtr<AWeaponBase>
to talk to the instance, and the blueprint just overrides data and event handlers that aren’t directly addressed by the C++ code.
This obviously also means that all the event dispatchers and interfaces and so on that you need to interact with from the C++ code, need to be defined in the AWeaponBase
class.
I may have not been 100% clear; I created my own c++ class already called WeaponBase (so in VS AWeaponBase). I’m just trying to convert what’s in the BP screenshot into the c++ SpawnWeapon() function. Can I still try to use TSharedPtr to access that class or will that not work either?
Regardless; thank you for your response.
It’s a lot easier if the code is in text, rather than screenshots. Surround it with three backticks (
`) on each side so it formats correctly.
So, to write this in C++, since you’re really far off the mark, your code isn’t making any sense at all…
your .h:
AWeaponBase* WeaponSlot;
SpawnWeapon(TSubclassOf<AWeaponBase> WeaponType);
your .cpp
void APlayerCharacter::SpawnWeapon(TSubclassOf<AWeaponBase> WeaponType) {
if (!WeaponSlot) {
WeaponSlot = SpawnActor(WeaponType, ...);
}
}
… most of that blueprint is cruft
Apologies, this is my first thread on here.
This is what I have so far in the cpp relating to this; taken from what you replied with:
int APlayerCharacter::SpawnWeapon(TSubclassOf<AWeaponBase> WeaponType)
{
if (!WeaponSlot)
{
FVector Location = GetOwner()->GetActorLocation();
FRotator Rotation = GetOwner()->GetActorRotation();
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();
WeaponSlot = GetWorld()->SpawnActor(WeaponType, Location, Rotation, SpawnParams);
}
}
I feel the answer solution getting closer, but now I get these errors during compiling:
D:\UE_Projects\LashFPS\Source\LashFPS\PlayerCharacter.cpp(92): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
D:\UE_Projects\LashFPS\Source\LashFPS\PlayerCharacter.cpp(92) : error C2664: 'AActor *UWorld::SpawnActor(UClass *,const FVector *,const FRotator *,const FActorSpawnParameters &)': cannot convert argument 2 from 'FVector' to 'const FVector *'
I agree with the bp not making a ton of sense; the course this is from is by an Epic employee, so I thought it’d be a good resource.
Thanks.
hmm.
WeaponSlot = GetWorld()->SpawnActor<AWeaponBase>(WeaponType, Location, Rotation, SpawnParams);
perhaps? the non-template version of SpawnActor might be a little fiddly weird.
The other variables I eliminated i’d say are of… questionable… value . . so I did shorthand this a bit, removing the apparently unnecessary bIsFirstSlotFilled . . . and this was probably done on an engine version prior to when it automatically created local function variables for parameters… so… i guess not entirely cruft, just from a bygone era.
Yeah, not really sure how to resolve this so I guess it’s best to reorganize the project I’m trying to do. Your help is very much appreciated.
That error says that you’re trying to pass a FVector into an argument that should be a pointer-to-FVector. That has nothing to do with whether your class is custom or not.
The error in the screen shot is because you’re typing code that is not valid C++, and also has nothing to do with whether your class is Blueprint or C++.
only thing i’m seeing wrong with the code in the last thing, is that it says it returns int, but it’s not returning anything… so that would be a different error. ::shrug::