What is the best way to consolidate 3 variables?

Hi. I have a game with 3 variables in 3 different locations. 1 in the level blueprint, 1 in an Actor blueprint and 1 in a c++ class. What is the best way to make sure they all carry the same value?

Thanks i advance

Also I need the variable to be at the beginning of the game and UE4 crashes when these variables are not equal.

I am not sure exactly what the situation is or if what I say even helps, but here is something that I might try. Hopefully it provides at least a starting point.

It sounds like there needs to be an external variable that is stored outside of all three, and then referenced back to it. Assuming it needs to be changed, I would try to create a subclass of AGameState in C++, and then have all those other locations reference that variable. In my opinion, this would be great for replication to clients from the server, but it would not exist at the start of the executable… Another thought is to put it into a subclass of UGameInstance, but that only exists on that one executable. The UGameInstance would exist at the beginning of the executable too regardless of the level loaded. Of course the correct URPOPERTY specifiers would need to be set to expose them to blueprints.

Ok so I’m using a UGameInstance and I am sure the 2 blueprint variables are working correctly. How do I set the c++ variable? Its used in the construction script for the main actor. I have tried:
UMyGameInstance* x;
x = NewObject();
mazeSize = x->getMazeSize();
and I still cant get the variable set properly. Can you help please?


Actually that shouldnt work either because I need the single instance of UGameInstance and not a copy. Help!!

I’m still learning c++ can you provide an example?

Basically I just don’t understand how to set a variable from within a blueprint as a c++ variable

Wouldn’t setting this variable by reference be exactly what you need to do?

If you dont know, passing a variable by reference is basically like sharing the memory address of the variable in and out of functions in order for the data contained at that address to be updated as needed.
Passing and setting by reference would automatically make sure that all the variables match - always.
Regardles sof when or how they get updated.

Where you instantiate the original variable and how you call back to it matters, but its also up to you.
If you plan on using blueprint and you want to connect to a variable you set elsewhere you may need to create your own blueprint cpp function to do just that.
If you need to read the stored value you would also need to create a function for that.

Put it into a place where all 3 can have read/write access, like the gameinstance or the gamestate and call this on each as a reference, not as a copy. Or even in the most important of these 3 if they interact with each other.

So, one of the basic “tennants” of proper programming is understanding the stack and how it works.
You can refer to this for a simple explanation of what it is and how it works.

You are better off looking at C examples and learning from C programmers then looking this up in C++.
That is mostly because in C you have to do all of it almost entirely by hand - and if you mess up stuff doesn’t work - whereas in C++ the compilers tend to handle most of it for you by way of garbage disposal or other built in ways of preventing memory leaks.

The stack, how it works, memory addresses and therefore passing by reference vs creating copies is a rather big and important part of programming - even if you do use engines that take care of most of it for you.
You don’t necessarily need to understand all of it as much as the underlying concept of it. That page I linked should do a decent job at explaining it enough.

As far as examples of “by reference” Unreal uses the same classic CPP & symbol and * for pointers.

So, for instance, you could call a function with (int &VarName) to pass the value as a reference into the function and utilize it. There are also other variations like “& int”(no space) VarName - and I can’t recall which one works best for blueprint functions as I’m not in front of any code.
You should be able to find samples of it specific to what you need though. A decent example that you can draw from is any Quaternion library implementation, or any Double library implementation.

Ok still working on that here is my latest try at it:
UMyGameInstance* x = Cast(GetGameInstance());
if (x)

can you help me at all I’m no good with pointers.


Ok still working on that here is my latest try at it:

    	UMyGameInstance* x = Cast<UMyGameInstance>(GetGameInstance());
    	if (x)

can you help me at all I’m no good with pointers.


Pointer Example:

The function main is on the stack, and the variables in it are also on the stack.

int main( int argc, char** argv) {

    int x = 98; // Let's say x has address 0xFF00AA11

    // Make an integer point variable y.
    // The variable y will also have its own address such
    // as 0xFF00AA1F
    int* y = NULL;

    // Notice that y points to NULL which is basically 0x0
    // We want it to point to the address of x using the
    // & operator on x.
    y = &x;
    // Now y points to the address of x, and the value 98
    // lives at it's address.
    // To get that value 98 using the variable y, we need
    // to dereference it like this.
    int xValue = *y;

    // Note that y itself has its own address separate from
    // the address of x,
    // and that the value of y is in fact the variable x's address.
    // Then if we want
    // the value of the address y points to, then we need to
    // use the dereference operator *.
    // The value of the variable xValue now equals 98.

    // You can also change the value of x through the variable
    // y by using the * operator.
    *y = 4;

    // Now x should equal 4, but the value of xValue will
    // still equal 98.

    return 0;

If you want variables to live outside of the stack and on the heap, you need to use the “new” operator on it.

If you want a member variable of a class to point to another variable, you need to make sure it is declared as a pointer. Then assign it to the address of another variable using the & operator, and get the value of its address it is pointing to using the dereference operator *.

In C++ there is another form of pointers called references that work in a similar manner, but they are different syntactically.

If you want to set the variable from somewhere else you could simply do it like this.


class UMyGameInstance : public UGameInstance

	int32 MyImportantVariable;


    	auto MyGameInstance = Cast<UMyGameInstance>(GetGameInstance());
    	if (MyGameInstance)
    		MyGameInstance->MyImportantVariable = 5;

MyImportantVariable has to be public though

No actually I want to get the variable from a constructor script at the start of the level. Can you help?


How would I do that to get the variable fro my GameInstance?


You won’t be able to set a variable in Blueprint and access it in the native Constructor in C++ since it runs before the Blueprint class is even created afaik. You should be able to read the variable from a config file though.