# Accessing function and variable of another C++ class nullptr problem

**URL:** <https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071>\
**Category:** C++\
**Tags:** nullptr, unreal-engine, UE4, question, CPP\
**Created:** [May 16, 2019, 5:13pm UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071 "2019-05-16T17:13:09Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shayan.ebz99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shayan.ebz99](https://forums.unrealengine.com/u/Shayan.ebz99)\
**Post date:** [May 16, 2019, 5:13pm UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/1 "2019-05-16T17:13:09Z")

</div>

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.

```
class A* someName;

```

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?

---

<div class="post-metadata">

**Author:** ![Vertex\_Soup](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vertex\_Soup](https://forums.unrealengine.com/u/Vertex_Soup)\
**Post date:** [May 16, 2019, 5:29pm UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/2 "2019-05-16T17:29:57Z")

</div>

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 `public` or class B need to be `friend` class of A. Consider this simple example:

```
class A
{
public:
  // A Constructor
  A() {}
  
  void DoSomeStuff();
};

class B
{
private:
  A* _a;

public:
  // B Constructor
  B()
  {
    // Create new instance of A class
    _a = new A();
  }

  // B Class Destructor (needed because we need to free memory we 
  // allocated by creating A class
  ~B()
  {
    // need to call this to free memory
    delete _a;
  }

  void ExecuteSomeStuffInA()
  {
    // First check if pointer to instance is valid
    if (_a != nullptr) {
      // pointer is valid, execute function in A class
      _a->DoSomeStuff();
    }
  }
};

```

Hope this simple example helps.

---

<div class="post-metadata">

**Author:** ![Shadowriver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shadowriver](https://forums.unrealengine.com/u/Shadowriver)\
**Post date:** [May 16, 2019, 6:54pm UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/3 "2019-05-16T18:54:18Z")

</div>

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 `new` operator to create a instance, in general it is operator to dynamically allocate variable, including integer and float varables. But in UE4 you not really gonna use that, as UE4 got special function to create objects and you rarely (there are some advance cases) create integer and float pointers.

To instantiate Actor you use function in UWorld called SpawnActor and oyu get UWorld instance using in any actor:

```
someName = ()->SpawnActor<A>();

```

or

```
someName = Cast<A>(()->SpawnActor(A::StaticClass()));

```

> **[UWorld::SpawnActor](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UWorld/SpawnActor/4/)**
>
> Templated version of SpawnActor that allows you to specify location and rotation in addition to class type via the template type

> **[UWorld::SpawnActor](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UWorld/SpawnActor/3/)**
>
> Spawn Actors with given transform and SpawnParameters

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

> **[NewObject](https://docs.unrealengine.com/5.1/en-US/API/Runtime/CoreUObject/UObject/NewObject/3/)**
>
> Convenience template for constructing a gameplay object

```
someName = NewObject<A>(this);

```

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():

```
AMyGameMode* MyGameMode = ()->GetAuthGameMode();

```

> **[UWorld::GetAuthGameMode](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UWorld/GetAuthGameMode/1/)**
>
> Returns the current Game Mode instance cast to the template type.

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:

> **[UWorld::GetFirstPlayerController](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Engine/UWorld/GetFirstPlayerController/1/)**
>
> May return NULL if the cast fails.

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\* :

```
()->GetAuthGameMode()->Ball = this;

```

`this` is pointer to instance of class that function is currently executing in, so object it self can tell where they are in memory. Now you can access game mode anywhere and access Ball from nearly anywhere in the code

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:

> **[UGameplayStatics::GetAllActorsOfClass](https://docs.unrealengine.com/5.1/en-US/API/Runtime/Engine/Kismet/UGameplayStatics/GetAllActorsOfClass/)**
>
> Find all Actors in the world of the specified class.

```
TArray<AActor> Actors;
UGameplayStatics::GetAllActorsOfClass((),ABall::StaticClass(), Actors);
//Knowing we have only one ball on the level we just gonna grab first item of array
ABall* Ball
//checking if array is not empty before getting it
if(Actors.Num() > 0) Ball = Actors[0];

```

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 new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

A lot easier way is to make actor pointer `EditAnywhere`, or more optimally `EditInstanceOnly`:

```
UPROPERTY(EditInstanceOnly, Category="Related Actors")
ABall* Ball;

```

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 `nullptr` if object gets destroyed, something that does not happen automatically. If object pointer won’t be nullptr when it’s destroyed, you got so called invalid pointer, pointing to invalid memory address which either points to trash data in memory or nonallocated memory and accessing it will cause a imminent crash. You Would need to use expensive IsValid() function which checks if pointer is pointing to valid created UObject that engine tracks.

---

<div class="post-metadata">

**Author:** ![Shayan.ebz99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shayan.ebz99](https://forums.unrealengine.com/u/Shayan.ebz99)\
**Post date:** [May 17, 2019, 6:56am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/4 "2019-05-17T06:56:33Z")

</div>

hi and thank you for answering, i tried to use public vars and functions of class A but as i said it shows that i didnt instantiate A in correct way.

---

<div class="post-metadata">

**Author:** ![Shayan.ebz99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shayan.ebz99](https://forums.unrealengine.com/u/Shayan.ebz99)\
**Post date:** [May 17, 2019, 7:04am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/5 "2019-05-17T07:04:16Z")

</div>

hi and thaks for answring. i read the information uve mentioned. it was helpful, but just to be clear, i have a Coin class and when player overlaps coins, score variable(its int) at class Coin gets plus plus. now i want to send this score value to my playerPawn class, so i declared coin as i said and yes i know in cpp programing we use ‘‘new’’ keyword but i havnt seen that in ue4 coding as u said it rerly gets used. so story short, whats yr idea about sending class Coin’s(coins are spawning in random places of world) score var into my playerPawn class so i can send it to blueprint widgete and show the score on screen?

---

<div class="post-metadata">

**Author:** ![Shayan.ebz99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shayan.ebz99](https://forums.unrealengine.com/u/Shayan.ebz99)\
**Post date:** [May 17, 2019, 7:19am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/6 "2019-05-17T07:19:07Z")

</div>

by the way why i am insisting to send the score to playerPawn is i cant find a way to show score on screen from Coin(which is actor) but i know how we can do it from controlling pawn(which is the pawn we play with). i created a widgetBlueprint and i bind the scoreText, then i created a variable inside the blueprint function that i made and i specified the variable type, it is working fine when i use playerPawn type, and it updates the screen accurately but Coin type is not working fine.  
one basic question : coins are just objects that spawn in the world and gets overlapped, which i want to point that they have not any controller classes cuz they dont need to. can i send information and update the widgetBlueprint by only using class Coin(and basically can i send information from classes that aint my game’s main classes like the coin )which only have to spawn and gets destroy?)

---

<div class="post-metadata">

**Author:** ![Podgyhodgy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Podgyhodgy](https://forums.unrealengine.com/u/Podgyhodgy)\
**Post date:** [May 17, 2019, 8:46am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/7 "2019-05-17T08:46:15Z")

</div>

Shayan,  
Maybe I misunderstand but, why are you increasing score in the coin class? When your player overlaps a coin should you not be updating a score in your player rather than the coin?

---

<div class="post-metadata">

**Author:** ![Shayan.ebz99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shayan.ebz99](https://forums.unrealengine.com/u/Shayan.ebz99)\
**Post date:** [May 17, 2019, 9:05am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/8 "2019-05-17T09:05:46Z")

</div>

well thats becuz i have many other overlapping events. i am practicing an endless game. floor has BoxComponent and when i overlap Floor’s boxComponent it spawns the next floor in world. thats why i cant really implement the score inside PlayerPawn.

---

<div class="post-metadata">

**Author:** ![Podgyhodgy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Podgyhodgy](https://forums.unrealengine.com/u/Podgyhodgy)\
**Post date:** [May 17, 2019, 9:55am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/9 "2019-05-17T09:55:49Z")

</div>

But your Game Instance is persistent so you could store your players’ score in there so that it persists between levels/maps. (If that is what you mean when you say next floor). I don’t see why storing the score in the coins gives you any benefit in this situation. They will not persist between levels any more than the player pawn will? I don’t think it can be at all sensible to keep your players score in all the individual coins. I also don’t see why the amount of overlapping events has any bearing on this. When an overlap happens you find what other actor is overlapping and then decide what to do. So, if your player overlaps a coin you add score and if your player overlaps somethings else then you don’t.

---

<div class="post-metadata">

**Author:** ![Shayan.ebz99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shayan.ebz99](https://forums.unrealengine.com/u/Shayan.ebz99)\
**Post date:** [May 17, 2019, 11:32am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/10 "2019-05-17T11:32:56Z")

</div>

O\_o yes thats a good idea…  
void OnOverlapBegin(class UPrimitiveComponent\* OverlappedComp, class AActor\* OtherActor, class UPrimitiveComponent\* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);  
so in playerPawn class that i implement OnOverlapBegin i can use OtherActor param and the logic should be if(OtherActor == Coin){score++;}  
but how can i get the coin as i said at the main question it returns nullptr

---

<div class="post-metadata">

**Author:** ![Podgyhodgy](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Podgyhodgy](https://forums.unrealengine.com/u/Podgyhodgy)\
**Post date:** [May 17, 2019, 2:34pm UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/11 "2019-05-17T14:34:55Z")

</div>

just like in blueprints you try casting it to the coin class and see if it is a coin. If your coin instance is of class ACoinClass then, you do this:-

```
ACoinClass* PointerNameULike = Cast<ACoinClass>(OtherActor);

```

If the OtherActor is a coin then ‘PointerNameULike’ will be a pointer to it. If the OtherActor is not a coin then ‘PointerNameULike’ will be null. I think that will work for you.

---

<div class="post-metadata">

**Author:** ![Shayan.ebz99](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shayan.ebz99](https://forums.unrealengine.com/u/Shayan.ebz99)\
**Post date:** [May 17, 2019, 4:09pm UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/12 "2019-05-17T16:09:10Z")

</div>

thanQ so much very helpful indeed.

---

<div class="post-metadata">

**Author:** ![Mynkela](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mynkela](https://forums.unrealengine.com/u/Mynkela)\
**Post date:** [July 30, 2021, 6:23am UTC](https://forums.unrealengine.com/t/accessing-function-and-variable-of-another-c-class-nullptr-problem/449071/13 "2021-07-30T06:23:02Z")

</div>

Check This : [https://forums.unrealengine.com/t/referencing-trouble-illegal-text-reference-to-a-private-object/138705/2](https://forums.unrealengine.com/t/referencing-trouble-illegal-text-reference-to-a-private-object/138705/2)

Its Work from me
