Hi, i am new to ue so i am a bit lost, someone say casting is bad, but i don’t know how to use interface to solve my problem. I have PlayerBp and Bp of an object, and i need to change 1 variable in ObjectBp from my PlayerBp, so how can i do it?
Casting is neither good or bad. Casting is casting. You do not have an issue with casting here, you have an issue with obtaining a reference to the actor.
You’d have the exact same issue if you used an interface.
How does the player interact with the anomaly?
• do we look at it?
• do we get close to it?
• do we click on on it?
• combination of the above?
• some other way…?
What is the scenario? What’s happening?
Actually, could you explain what to put in all of these situations?
Casting is still somewhat elusive for me. I always figure out what to put… but it never feels like I did the right things because I KNEW how to do it.
Yeah, i agree with Leomerya12, in my situation when i am interracting with anomaly it sets visibylity to another object.
Again. How are you doing it?
So i have event tick and id checks for my variable (you can see on skreenshot), this variable changes when i have certain item in inventory.
This does not help, sadly. Could you explain the scenario? I understand that we are not interacting with the BP you’re trying to cast to directly. Something else is the trigger, right? Half the issues with referencing stem from organisation and approaching things from the incorrect angle.
So i have event tick and id checks for my variable
This immediately sounds like a bad approach. Is there a reason why you’d want to query something every single frame of the game? This is also the reason why people complain about Tick
being slow - it’s often forced to run things it should not.
this variable changes when i have certain item in inventory.
This may be fine but why not do it just once?
To me it seems that picking up the item is the trigger - you should use this moment as a trigger. Player picks up the item → something happens. There is no need to check 60 times per second if the player has the item.
Without you explaining how it’s supposed to work, the only solution is to use Get Actor of Class
/ Tag
. But doing it would be the first step to bad code practice that will get you in trouble later on since you will always have issues with referencing, especially when there is more than 1 actor instance.
Perhaps you could simplify it for us:
- there is a
player
in the world - there is an
item
actor in the world - there is an
anomaly
actor in the world - player picks up the
item
andanomaly
reacts to it
How close am I? Is this all or is the interaction more complicated?
Casting is a test + conversion. Are you the data type I think you are? If you are or can be converted, let me treat you in some special way. Casting is often automated or hidden - especially in BPs.
This is also casting:
The top one makes sense, the bottom one will fail but it will gracefully return 0 rather than crash & burn.
- if you take a Pawn and cast it to Character - it will work because deep down every Character is a Pawn
- if you take a Chaos Wheeled Vehicle and cast it to a Camera Component - that would not make much sense.
But, as mentioned above, people think they have an issue with actor Casting but, in reality, they have an issue with obtaining a reference!
Once you have your Object / Actor:
The casting part itself is trivial - it’s already done correctly above - it’s just a node to convert one thing into another. The actual question is: where the heck is the object, where is the thing we want to convert?
Where is the string so I convert it to an integer?
It’s all about finding the reference first. Actors do not know other actors exist, and rightly so! That’d would be pure chaos.
do we look at it?
- Line Trace → Get Hit Results → Actor ref → Cast / Interface
do we get close to it?
- Begin Overlap → Other Actor ref → Cast / Interface
do we click on on it?
- Get Hit Under Cursor (player controller) → Get Hit Results → Actor ref → Cast / Interface
or
- deproject mouse to world → Line Trace → Get Hit Results → Actor ref → Cast / Interface
or
- override
onClicked
in the clickable actor
Also casting is not slow. Interface is slower. Interface also casts but it hides it from you. Using it all the wrong way will be slow. Don’t put stuff on Tick that does not belong there.
This is too cryptic. Here’s a whimsical scenario:
- this is the tape, It can point to any instance of an anomaly:
- the orange thing is the AnomalyBlob that is ruining my boxes:
- the tape knows which anomaly we need to fix:
Practice with this kind of communication / interaction - it is crucial to get it right early on so.
Thank you so much, you are a legend!
I’m not kissing your rear: that is the CLEAREST someone has explained casting to me.
Thank you!
It’s not completely demystified, but I definitely feel like I can approach it more confidently now. I’ll get used to it in time.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.