I want to be able to access another actors variables How can i do this? I’m trying to create a pointer but it is giving me errors about not being able to initalise UClass *
AActor * Character = ADungeonExplorerCharacter::StaticClass();
I want to be able to access another actors variables How can i do this? I’m trying to create a pointer but it is giving me errors about not being able to initalise UClass *
AActor * Character = ADungeonExplorerCharacter::StaticClass();
ADungeonExplorerCharacter* Character = Cast(ReferenceToTheOtherActorrHere);
You will need to cast it across to the actor type you want like so. I use this method to get custom controller classes:
AMyControllerClass* ThePC = Cast<AMyControllerClass>(GetWorld()->GetFirstPlayerController());
I can then use ThePC to call my function I have created in AMyControllerClass.
Thanks for the response,
Why do i have to cast? I’ve seen this around but i struggle to understand
And also what do you mean ReferenceToTheOtherActorHere
You will need to cast if you are getting the pawn class from somewhere like the Controller class (GetPawn() function), which returns a pointer to ACharacter class. However, your ADungeonExplorerCharacter inherits from ACharacter (I assume?) and therefore you need to cast it to this type.
And the ReferenceToOtherActorHere is your reference to the character where you have spawned it. For example, if this is your character, you can access this through your controller:
GetWorld()->GetWorld()->GetFirstPlayerController()->GetPawn();
ADungeonExplorerCharacter* Character = Cast<ADungeonExplorerCharacter>(GetWorld()->GetWorld()->GetFirstPlayerController()->GetPawn());
This may not be the best way to do this and someone may give a better example however.
Why do i need to reference where it was spawned if i’m just trying to access it’s members?
When you say access it’s members, what do you mean exactly? Static variables or…?
In theory, you can just do MyDungeonExplorerCharacter->MemberHere I would have thought? You need to have an instance of the class to have the variables accessible to you I believe, otherwise there not even initialized in memory.
The whole point of this pointer ( no pun intended ) is to be able to access and change variables, which i have managed to do, however I was just confused as what to set the pointer to in the beginning.
Correct me if i’m wrong, but this:
GetWorld()->GetWorld()->GetFirstPlayerController()->GetPawn())
Retrieves the Controller class of the character? And also why do you have to put GetWorld() Twice?
In fact, if you could answer why
AMyCharacter * Character = AMyCharacter::StaticClass();
Doesn’t work i’d be mighty impressed & grateful
So are you trying to access the members of a Actor that is spawned and “existing” in the level? Or are you trying to access static members?
AMyCharacter::StaticClass() refers to the “Class” of the object, which means its “type declaration”.
If you want to modify attributes of your player, you want to modify the “object” (the instanciation) not the “class”. And then you need to get a pointer to the object.
Trying to access the number of keys the character has, so yes, trying to access members of an actor that is spawned
Makes sense, my thanks
Sorry for late reply. If you’re trying to access Actors in the world then have a look at this tutorial: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine ForumsObject%26_Actor_Iterators,_Optional_Class_Scope_For_Faster_Search
It’ll teach you how to access any object in the world you want; then it’s your job to just keep a pointer reference to it then you won’t have to use the Actor Iterator again.