accessing function and variable of another C++ class nullptr problem
hi everyone, i am really confused by some simple stuff, i am trying to get some values from class A and return that value into class B and do something with the value. first thing that came to mind was that i should create an instance of class A in class B so i will be able to use class A's public functions and vars. so i made an instance in B.h.
i tried to use some of functions of someName in B.cpp, but the problem is when i play the game it shows that someName is nullptr. in programming viewsight its true to me. we can use class instances in another classes. so what the problem can be?
What you doing is not instantiation but only deceleration. But variable of type with * symbol is a pointer, it points something in memory, in technical sense it a integer varable containing memory address. Just declare it won't magically make it point to the object, same as integer it will have value 0 aka nullptr.
You need to set pointer either by instantiation or getting the pointer from diffrence sources (like get functions) if they already been created. Normally in C++ you would use
To instantiate Actor you use function in UWorld called SpawnActor and oyu get UWorld instance using GetWorld in any actor:
SpawnActor has special template argument (look example 1), which allows auto casting (you don't need to cast return varable) and automaticlly fill UClass* function argument (which oyu can see i used in argument 2)
If SpawnActor fails to spawn, it will print reason in the log and return nullptr
To instantiate UObject oyu use NewObject, similar as Spawn actor, but this time you need to define outer object which in most cases are object you creating object in
But what im saying so far is just creation of object, UE4 is big and it create many objects in it's lifetime and your world is big and each time you palce actor on the level you already creating instance of it so you dont need to create new ones, you need to get one or make newly create object to send pointer to it self somewhere else.
For example AGameMode is a core of the game code that exist in span of whole level (unlike UGameInstance which exist in spawn of entire game), it is always create by UE4 automaticly and to access it instance you need to call function in UWorld called GetAuthGameMode():
As oyu can see this function also have template argument that do automatic casting, it will return nullptr if game mode class is not the one that you requested.
If you make single player game, you can get APlayerController same way:
If you used blueprint, you can see the exact same pattern here... yes, because blue pins and link are equivalent of object pointers in C++ and it uses mostly same functions.
Getting actor on level is a lot more tricky, if there is importent actor for a game mode oyu making, in general practice actor should send it's pointer to GameMode where other actors can access it. For example you make some football game and oyu got ball ABall* placed in the level on BeginPlay of the ball you set varable Ball in gamemode AMyGameMode* :
Other way is search for a actor, you should use it as last resort as it's most expensive way to do it... and yes it the same thing you use in blueprint, GetAllActorsOfClass:
But this function was designed for blueprints that does not support iterators which oyu can use C++ and are a lot better as you can break search loop once you find actor you looking for:
A lot easier way is to make actor pointer
And when you place an actor having this variable on the level you can set this variable in property editor, this allows to easily link actors together without need for them to register themselves or search or each other.
Also remeber if you declere pointer to UObject or AActor (which is also UObject) in class you need to place UPROPERTY(), this will make varbale visible for the engine and it will monitor object referencing for GC, it will also automaticly set property to
To use other class in this example A class member functions, they need to be accessible for class B, eg they need to be declared as
Hope this simple example helps.
answered May 16 '19 at 05:29 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here