cast to function doesnt work

I’ve created two blueprints and wanted to pass a global variable from one of them to the other. I wanted to do that with a cast but always I run it, the cast fails.

Try instead of “cast to” node this one and choose your actor bp you want there get the variable from the return value. It may work but not 100% sure worth a try
bandicam 2024-03-10 22-09-07-751

1 Like

I think there’s a bit of missing information about what you want to achieve. How do those two blueprints interact (if they do)? Initially, it would be best to create a function like GetGlobalVariable() in the blueprint that holds that global variable (for programming best practices), which you can mark as ‘pure’, but depending on what you want to do, you might need, for example, to create an interface that serves as a bridge between both blueprints.

1 Like

so I’ve gotten the following logic:
1.

with the following output:

It just fails is not a Actor but a Skeletal Mesh Component, so I cant use that there.
How can I do it (like you said), with an interface?

(Oh and sorry for so many individual screenshots but I am new and I can only post one at a time)

Looking at your screenshots, I think the issue might stem from how you’re passing target in the weaponmode widget. Could you share a screenshot of where you’re creating the widget, and how and where you’re defining target?

1 Like

from here:

Sorry, maybe I didn’t express myself properly. In some part of your project, you must have had to do something similar to this:

image

Additionally, the “Target” variable (the one you’re trying to cast) must have been given a value at some point (“Set Target” with a reference to the Skeletal Mesh).

image

If you haven’t done this last part, that’s the issue.

1 Like

well I’ve changed a bit because my code just went missing dont know why. So I set up another Game.



so I want to change the Variable Weaponmodetruefalse (inside of the NewHud) to the state of Weaponmode in BP_Weapon_Component. I dont exactly know where I should put the set you said to put?

Just add an input boolean parameter in the UpdateText function inside WB_UI. Then, anytime you change the value of WeaponMode you just need to get the reference of WB_UI and call UpdateText with the new value.

Of course, you can do this through an event dispatcher if you want to.

This should work, as I’ve mentioned. Additionally, I’d like to offer you some advice (always from a constructive criticism perspective and with the best intentions):

  1. Name all variables with meaningful names. Now that you’re working on that blueprint, you understand what REF or TextBlock_0 means, but in two months, you won’t.

  2. Try to always follow Unreal’s naming standards for variables. Unreal typically uses PascalCase, meaning the first letter of each word should be capitalized. Avoid using ? in booleans; the correct way to describe them is by adding a lowercase “b” before the name (bWeaponMode).

  3. Be cautious with loops when mapping out function flows. I refer back to the first point: today, you can easily follow the flow because you’re actively working on it, but in a month, you might forget where everything goes and end up spending more time tracing the flow than actually working. Use reroute nodes; they’re quite useful for visually cleaning up the blueprint.

  4. Be very careful with those 0.1-second delays or random values that, at first glance, don’t seem to make sense. They weaken your code and make it much less efficient. It’s better to use Timers. Additionally, randomness in values can drive you crazy later on, especially if you need to incorporate multiple delays into the same blueprint.

1 Like

thank you so much for you help. But whats an input boolean parameter? Sorry for asking so much but could you send like a picture or something? The other problem is, that I dont know, what I should connect to the Target pin of Update Text that I’ve just created inside BP_Weapon_Component?

I’m sorry for my recent absence. Honestly, I’ve noticed there are some basic concepts missing both in your understanding of Unreal and programming in general. But that’s OK! Nobody starts off knowing everything.

Maybe (in my opinion) you should take a break from your current project and step back a bit. If you’re serious about pursuing this, it’s crucial to have a solid programming foundation. That way, you’ll at least grasp the logic you’re aiming for in your blueprints (I assume you’ve been following YouTube tutorials up to this point). Don’t rush into things just yet. Even though it might not be the most thrilling, spend some time learning programming outside of Unreal. You’ll definitely appreciate it later on!

I’ve already programmed projects outside of UE5 with python, C++ (just very little) HTML, CSS and Javascript. But thanks