How to get/set vars from other blueprints

Hi guys,

Apologies that this question may be similar to others that keep coming up - I’ve looked at several posts and tutorials, but none of them were quite don’t what I’m looking for and context if everything with the UE4 blueprint interface.

I have 2 blueprints and want one of them to check for a var change and then do something based upon it. In this example, play an explosion and remove a door from the world. Script A waits for the player to run through a gate (with a box collision) a number of times, before changing a var that Script B that is supposed notice and then trigger the particle FX and destroy itself.

The part I’m struggling with is the script-to-script comms. I’ve looked at things on referencing and casting, but I’m still not getting it.

Script A - When health var decrements to 0 set the destroy boolean to true

Script B - Activate the particles and destroy the object

Any help would be greatly appreciated!

You shouldn’t work with variables to do this, or at least, not just with them. There are some methods to do this, I would use full [Observer Pattern][1], but there are simpler ways, for example:

ScriptA

Create an Event Dispatcher and a Health variable = “number of times”.

269981-0.png

Create a function “AddHealth” that receives a Integer parameter and sums it to Health. After the operation, call the event dispatcher.

Gate Actor

Add a “Collision Box” component and bind its Begin Overlapping event.

Add a ScriptA property, check it as “Instance Editable”. Place this actor in the world and in the details panel, click ScriptA property and select desired ScriptA object.

269983-3.png

ScriptB

Should also has a ScriptA reference var like Gate.

Bind to ScriptA event dispatcher.
Check if Health is <= 0, and blows up

EDIT:

Also check if ScriptA reference “IsValid” in ScriptB Event Graph

If your objects are spawned by script, you cand do this bindinds and refs with script too.

I agree with the above. I have an entire tutorial series that is beginner friendly which you may find helpful. The first video is all about casting, how and when to use it. Videos 24-26 go over Interfaces, Creating References and Event Dispatchers. All very useful things for what you are trying to accomplish.

This is also a tutorial explaining overlap events in detail if you decide to go that route may also be helpful.

Thanks for the help guys.

I’ll look into everything that you’ve recommended.