How do I change a variable that is in a different blueprint?

I have a variable called ‘max zombies’ which is located inside an enemy spawner which I have created. I need to be able to change this variable from a blueprint located in my character… I need the ‘max zombie’ variable’ to update every time I pick something up which I also have a variable for… the issue is that these 2 variables are not in the same blueprint so I am having trouble.

The ‘max zombie’ variable is public, I can cast to the enemy spawner from my character blueprint, for the target of the enemy spawner cast I have used ‘self’, after the cast I simply have ‘max zombie’ is equal to my other variable… however this

setup simply does not work.

I have attached a screenshot and hope someone can help with this.

Thanks.

Ok so just I understand the system you have here. You have a character blueprint which is Liam_CH I assume. Then you have a spawner object that has a max zombie variable that you want to change. And you have a pick up object. Now you want to set the max zombie variable in your spawner object when you get the pick up object, am I right?

Ok give a few minutes to show you an example of how to do it

If I understand right, you have to change the self referrence to a reference to the spawner, depending on your game this can be achieved in different ways. You can “get all actors of class” and then cast to all of these if there are more than one. This is the easiest solution, but it depends on your game if it is the best for performance, but it shouldn’t matter if you don’t have more than around a thousand spawners :).

What it is doing now, is basically telling it self that it has to change the variable, not the spawner. This could also cause a loop, if the variable changes every time the variable changes in the LiamBP. Could cause a crash.

You are correct. That is exactly what I want.

Thanks for the reply… I can not connect the output of the ‘get all actors of class’ to ‘cast to’ node… the output is an array and the input is an object so they simply do not connect up… any ideas what I am doing wrong?

I dont understand what that means…

You have to get every actor from the array via an For each loop
plug the array in the left side and on the right you get an actor and an integer. you probably just need the actor reference to plug into the cast to node and that into the set max zombie node like above, but make sure that if you want to add something behind, you have to get it from the “Complete” execution on the for each loop otherwise it will integrate it in the looped actions.

i’ll try to setup a better explanation

There are various ways to do it. You could have a direct reference to your spawner in every pick up object or inside your character blueprint. You could also use the get all actors of class node and use it to look for your spawner class, in your pick up object when the overlap or hit event is triggered. And finally, the solution that I like the most for this type of cases is an event dispatcher.

An event dispatcher allows you to send a message to any class that you want when something happens and you can pass any info into this message. So here we go.

First go to your character class. This is the class that will be broadcasting the message.

Add an event dispatcher with the + button in the event dispatcher tab. Name it what you want. Now make a new function and drag the dispatcher to the graph. A little menu will appear. Chose “call” and connect it in the function. Like this:

Now go to your zombie spawner blueprint

On begin play do this. Get your character cast it to your own Character class (Liam_CH) and drag from the reference pin in the Cast and search for assign “name of your event dispatcher”. Now when you choose this action it should create an event automatically for you, in this event is where you set the max zombies variable to whatever you want.

Finally go to your pick up object.

In this object I’m pretty sure you have an overlap or hit event for when the player touches it. So from this event drag from the other actor pin, which is a reference to your player, cast it to your Character class and from this call the function that you created it that calls the event dispatcher. Like this.

So now, everytime your character picks up an object of this type it will send a message to every zombie spawner so they can do something about it.

Tell me if you have any questions!

You have the set Points, then you connect that execution pin to the for loop. then that execution pin (loop body) to the cast to node and then stop. dont connect the max zombie yet. then you connect the arry node from the for each loop to the get all actor from class array node. Then you connect the array element from the for Each loop in the cast to node. and then you can set max zombie from the blue node to get the right blueprint class that contains that variable.

I have set all those nodes up… but where am I makign the change to the variable? in what blueprint?

For every pickup I need 1 adding to ‘max zombies’… but I do not know where to add this.

In the second image, the zombie spawner blueprint

Do it like this

That ++ is called an increment node. It adds one to your variable

I still dont understand… I thought I was calling the dispatcher in the add points function?

Sorry that I do not understand I am still new to ue4 :confused:

think you are confusing our two answers :slight_smile:
The dispatcher solution was give by MacDX. My solution is different.
I’ll make a Picture of how us have to set my solution up, so you have both ways. It’ll take some time.

This should be very clear. Had to make it a bit smaller to fit in the screen :slight_smile:

Also could you please set this as resolved if it could fix your problem, so that other people know that there is a solution.

My answer is the easy one, while MacDX is probably better for performance, if you have many Actors. Just to let you know. :slight_smile: