I have this function here:
spawnParams.Owner = NULL;
spawnParams.bNoCollisionFail = true;
spawnParams.Instigator = NULL;
AMyWeapon* myWeaponClass = GetWorld()->SpawnActor<AMyWeapon>(AMyWeapon::StaticClass(), spawnParams);
myWeaponClass->AttachWeaponToPawn(GetWorld()->GetFirstPlayerController()->GetPawn(), "CharacterMesh0"); // should be "HeroTPP"
FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(TEXT("myWeaponClass could not be spawned.")));
And I’m trying to access myWeaponClass in another method in the same class, but assigning the pointer to be public doesn’t seem to be allowed in Unreal. Is there a way around this problem?
I tired to declare in the header, then initialize in the constructor, but that didn’t work either.
In your header, something like:
If you want it to be blueprint accessible, you need to add the proper tags in the UPROPERTY. Then just spawn the actor as you are doing. With this exception, obviously:
// Don't declare a local variable, just assign it to the one in the class.
myWeapon = GetWorld()->SpawnActor<AMyWeapon>(AMyWeapon::StaticClass(), spawnParams);
You probably should study up on your C++ some more.
That does not work, which is what I was trying to say.
error C2143: syntax error : missing ‘;’ before ‘*’
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Then you have messed up something basic, like forgetting to declare/include AMyWeapon in the header perhaps. I’d post another question with a simplified reproduction of the problem you are having.
I think you have solved this problem.
In case you haven’t, since I’m a noob in Unreal Engine and C++, I can give you a basic suggestion (forgive me, if this doesn’t help you).
If you want to access your variable outside your method in the same class, you need to make the variable at least have a private access modifier. So you need to declare the variable in the header so you can access it everywhere in your class.
If you already did this, but still getting error, maybe you have another problem that you haven’t shared it with us.
“…If you want to access your variable outside your method in the same class”
If you have a class and you declare your variable anywhere in that class; Private, Public or Protected, you can access that variable from any method within that class…you shouldn’t need an accessor if it is within the same class.
However, as stated, the error “missing type specifier - int assumed” is probably taken from the line:
in the header as it doesn’t know what AMyWeapon is and is assuming an integer which then throws an error.
He would have needed to include “MyWeapon.h” in the header where he is using AMyWeapon.