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.
asked Oct 16 '16 at 12:47 AM in Blueprint Scripting
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.
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!
Follow this question
Once you sign in you will be able to subscribe for any updates here