Download

Cast

How do i cast a value to another blueprint without a triggerbox that have the “other actor” thing?

Are you asking how to access a value from one blueprint in another blueprint?

Yes, im kinda new to this stuff so yeah …

No worries! We hope you have an easy time getting into Unreal.

Here’s the reference on BP interfaces: https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/Types/Interface/UsingInterfaces/index.html

And a youtube tutorial on them: http://www.youtube.com/watch?v=HtB_00h2Ykw

Best of luck!

Not exactly sure what you’re asking, but I think what you’re looking for is an interface.

An interface is basically a layer placed on top of a blueprint that defines what functions are available. The purpose is multiple blueprints can implement the same interface, then when you wish to access it, you simply try to access that interface function.

Here’s an example. Let’s say you’re making an adventure game and there’s lots of objects in the world the player can interact with. So when the player presses “E”, you run a line trace to see what the player is looking at.

Now, in order to interact with the object, you could do

Character Blueprint:
Press e > --------- > Single Line Trace, Break Hit -------> Hit Actor
With a long series of branches to see if the actor is a light or a lever or a button or a door… it will get messy fast.

All of those objects (light, level, button, door) all have one thing in common: They can be triggered by the player. This is where an interface can come in handy.

If you right click in the content browser and create a new interface blueprint, you can call it something like “Interaction”. Add one function to the event graph, call it “Activate”. That’s it - the interface is done (unless you wish to make it more complex later).

Now you’d go to each one of your objects, let’s say the light for example, and in the blueprint properties, add the Interaction interface.

In the light’s event grid, right click and add the “Activate” event. It will be available now because you added the interface.

Now in the character blueprint, we don’t need to have all those branches anymore.

Press e > --------- > Single Line Trace, Break Hit -------> Hit Actor --------> Activate

That’s all you need now. So the logic is as follows

  1. Player presses “E”
  2. A trace is run to see what the player is looking at
  3. If the trace hits an actor, attempt to run the “Activate” event on that actor
  4. “Activate” event is called on the light

So any object that uses the “Interaction” interface can use the “Activate” event. The interface defines that function so that the character blueprint really doesn’t care what kind of object it is… it doesn’t need to know if it’s a light or a box or a lever, etc. It just tries to use it and the object itself can define it’s “activate” behavior.

I realize I’m not great at explaining things, but I tried. Hopefully it helps.

Thanks for the comment, but i don’t know if that’s possible to do it for what i want to do.
I have 2 blueprints one with a value of 0 and another with a value of 250
So when I’m near that object (inside a triggerbox) and click F (basically a gate with the triggerbox to open and close) and after pressing F it’ll change the value in the first variable (0) to 250 which is the value of the second one and the second one will reset.(integer+integer and set the value of the 2nd variable to 0)
and now how do i do that ? each variable is in another blueprint.

Still a little trouble understanding, but I’ll try my best. In reading this now, I don’t think you need an interface.

I would not do this in two blueprints. I would put both objects into a single blueprint, allowing them to be moved independently of each other. This will make it much easier for both objects to communicate with each other. You can make it so two blueprints communicate with each other, but it’s messier and I wouldn’t do it unless I had to.

You can have 2 meshes and 2 collision boxes (one per mesh, a child to the mesh). You would have a pretty much identical event for each collision box (or rather, pressing F inside each collision box).

Player presses F inside collision box 1 – > Set Variable 1 = Variable 2 – > Set Variable 2 = 0
Player presses F inside collision box 2 – > Set Variable 2 = Variable 1 – > Set Variable 1 = 0

Edit: If you really need to have each object in its own blueprint, let me know and I can help. I did this with a teleporter system where I needed to make sure I could link any number of teleporter pads together.

I need to put them in 2 blueprints because the second blueprint is suppose to communicate with more blueprints and not just that one. (btw it’s a variable i made in the character blueprint)

I don’t suppose you could give me some context here as to what it is you’re trying to do? Might help me get a better idea as to how to approach it… right now I’m really just guessing.

In general, talking about two blueprints talking to each other, the first problem you might have is “finding” that second blueprint. I don’t know if this is also an issue for you at the moment so I won’t address it, but if this is a problem let me know.

Ok, so if you have two blueprints: Object1 and Object2. Object1 has a variable, “Variable1” and Object2 has a variable, “Variable2”. In this case, it is simple as long as Object1 knows about Object2 and vice-versa.

So Object1 just needs to say [Object2] → [Cast to (actor): Object2] → [Set Variable2: New value]

As long as you cast it to that specific object, you can access the variables contained.

in player blueprint i have “player_fuel” which the value is 0 and i made a jerrycan blueprint which adds to the “player_fuel” 250 to the value when you hit it.

And till here it’s all good.
But now there’s the generator.
I made in the blueprint that if you press E you can turn and off the generator and if the generator doesn’t have fuel it cannot turn on.

And then to add fuel i basically want to do that if you press F, it takes the value of the player_fuel in the character BP and add it to the generator’s fuel variable and resets the player_fuel to 0, like you are fueling it.
So my problem is that I’m not sure how to cast it to the character blueprint because this is how i made this:
a9f89d30c8c0f8e7e40e1c04457acd69951951df.png

So basically what i want to do is, when i’m pressing F near the generator (collision box) it will refuel the generator by taking the player’s fuel (the value of player_fuel) and refuelling the generator (adds the value of player_fuel to the fuel variable in the generator blueprint class and resets the player_fuel)
How do i cast it to character blueprint like i did with the jerrycan ?

Ah ha. Ok, that post was very helpful in understanding.

The “OnComponentHit” did a bit of the work for you with the can. It says “When this is hit, do this with the thing that hit it”. You could do the same thing with the generator, I suppose. Add a “OnComponentHit” and then you’ll have the player passed in which you can cast and modify values.

But if you want this to work with the “F” key, I would start with the character blueprint, not the generator. I think it’s a bit more organized to have all the inputs at the character blueprint level so it’s easier to find them later.

Ok, so… first thing’s first. Is the character going to be pressing “F” to activate a large number of objects or just this one? If a large number of objects (or even more than 1, really) will need to be activated with “F”, I would use an interface. It’s not a bad idea to make one anyways just in case you want to use it in the future.

Interfaces… like I said in an earlier post, it’s basically a way to standardize a set of events that can be used. Think of it as a face plate on an electronic device. You create a face plate that has 2 buttons “On” and “Off”. Every electronic device can be turned on and off, so you can put this face plate on all of them. Now when you actually want to turn something on and off, you don’t need to know what the device is… you just need to know that it’s using that face plate.

So let’s make the interface. Right click in the content browser and create a new interface blueprint. You can call it whatever you want, but it can be as general as “InteractionInterface” and you can use it for a large number of objects. Add one function, can call it like “Activate”. I would also add a single input with the type of “Character”. So now this interface can be used with any object you want the player to be able to interact with, just like the face plate on the electronic devices.

There’s two parts that need to be done. We need to tell the character how to use the interface (what does the character need to do in order to access the “button”?) and we need to put the interface on the object.

So lets go into the character blueprint. Here, you can add your “F” event.
This part is a little tricky because we need to find the generator. I would personally do this in a Single Line Trace from the camera to activate the object the player is facing, but your current implementation simply activates the object the player is near (within the collision box). So in order to replicate how yours works, we need a node that I believe is called something like “Get Colliding Actors”. I’m at work and don’t have access to the editor at the moment, so that name might not be 100% accurate, but if you search for “collide” or “collision”, you should find it pretty easily.

Anyways, this function returns an array, a list of all actors the player is colliding with. If the player is within the collision box of the generator, the generator will be in this array. The reason I’d use a single line trace instead of this method is it ensures you will only get one object instead of an array of all objects near the player, but … it is what it is.

Ok, so you have your array of objects. I would attach a “For Each” loop to the array, then for each object, call the “Activate” function defined in the interface. You should also create a reference to “self” and pass it into the character input pin on the function call. So this basically says “Try to press the button on the interface and tell the object I’m the one that pressed it”. It will try even if the interface doesn’t exist for that object, but that’s just fine. It will only work when you intended it to.

So far, we have the character, presses “F” and runs the “Activate” function for all actors he is colliding with. We don’t have any actors actually using this interface yet though, so that’s coming up next. One note here, if you had more than 1 generator next to each other to the point where their collision boxes overlapped and the player could be within both at the same time, it will call the “Activate” on both of them. To prevent this, you could break that “For Each” loop after the first “Activate” is called, or change it to a single line trace.

The next step is to put the face plate on the device. So go to the generator blueprint, open the blueprint properties (button on the top bar somewhere), scroll down to where you see “Interfaces” and add the “InteractionInterface”. Compile and save. Now if you right click in the event graph, you should be able to add the “Activate” event. This is what is called when the character presses “F”. You should also see the character output pin on that function. That will contain the character that activated the generator. You can now treat this exactly like you did with the fuel can and the OnComponentHit event. You can access the character’s current fuel and reduce it to 0.

Phew, that took longer than I had planned. Anyways, let me know if you have any questions.

Edit: If you don’t want to use an interface, you would do the “Get Colliding Actors”, run the “For Each” loop, cast the actor to your Generator and execute a custom event you create on the Generator blueprint. It’s much less dynamic, but if you’re sure this is the only place you’ll be using F to activate stuff it might just be easier.