Download

Casting, casting, casting. How does it work from Game Mode/ Game State?

I watched tutorials all day but bottom line is I just cannot understand actor referencing. I can cast to characters, but when it comes to actor blue prints I am stuck.

I have this Altar actor that checks if any Rune has been delivered by the character and stores the answer in an Integer (how many Runes have been delivered so far). I want my GameState to look for this score value inside the Altar blueprint and set the score. But I really, really cannot make GameState get my Altar (without searching through all actors). The same problem when referencing from GameMode.

How does referencing work in a situation like this?

Thanks a lot for taking the time to help a new guy like me!

Is the Altar an object that you’ve placed in the level manually? Sending information to the actors placed manually in the world is kind of annoying.

But you can always have it the other way round - have the Altar talk to the GameState/Mode instead - make it event based, as soon as the counter is increased in the altar, get game state -> cast to game state -> sends altar’s int

Hello! The Altar is an actor blueprint placed in the world. Inside the blueprint it has the logic of detecting the player and detecting when a rune is being delivered. I do not like to use the level blueprint for stuff.

No one asks you to. I was merely asking whether the actor was spawned dynamically or not, not the best choice of words on my part. Anyway, the solution is up there.

Hehe, sorry for sounding aggressive in my wording there, english is not my first language. You solution is great, and it’s a different way of thinking of things. However I still feel handicapped that I can’t properly cast when I want to. It’s like there is something I am missing.

For instance I have a variable in my hud widget that I want to change when another variable from my player state changes. So I cast to my custom player state from my widget. But I do not know what to put in the Object slot inside the casting. Or maybe this is the wrong way of doing it, but essentially I want to know how make variables talk to each other. I’ve read a lot on the subject and I feel I am getting dumber the more I read :(.

tl;dr: Get Game State [HR][/HR]
There’s a bunch of framework classes in the engine - game instance / mode / state, player controller and so on. You can choose to override one or more of them. If you do, you can still use the Get Game Instance node or the *Game Game State *node - this will give you access to the base class. But if you need the access to the custom overridden one you extended yourself, you need to cast - you need to tell the engine which particular class you need since there can be more than one!

Let’s say you have 3 player controllers (because you have 3 vehicle types, for example), you can always *Get Player Controller *from anywhere in the game, this gives you the base object, and now you need to cast because you need a particular one. In this level we’re flying a jet, which handles quite differently to the footsoldier.

Casting has little to do with communication, it just ensures you’re referring to the correct object type. Also, you will find as many explanations of this as there are people so hold tight!

Keep the questions coming if things are unclear.

Thanks for the great answers and for taking your time to help me. I made it work going from your hint and then searching the internet but I must be honest I am not sure if I understand why it’s working. So let me see if I understand this piece of code:

1 - I call for the Game State parent

2 - From there I ask for a player array meaning all players in the game (and I guess getting each player means getting their controllers and states???)

3 - Run through all players in the array

4 - See if I get the one I need that has the variable I am looking for

Since you said I could ask you a bit more, I am a bit confused about the Event BeginPlay and Event Tick. Of course, they sound pretty self explanatory but I’ve heard that it’s not good to check every frame for something. My question is more about Event BeginPlay since I just fixed a piece of code by switching from this to Event Tick and got me confused.

Let’s say I set a Light to be checked if it’s on or off and I set it with an EventBeginPlay. Does this mean that the code checks only once when we begin play? Do I have to check the door with an EventTick?

I don’t understand your example with the villager - is every villager a different player, is this for multiplayer? [HR][/HR]Event Tick is indispensable. You need to use it, more precisely, you need to use where it’s needed - any bit of code that needs to be called every frame.

For things like these, it is much better to use an event based approach. Player enters the room - check if the light is on, once, not during tick. You click on the door - check if it’s open. You want to open the door and see it swing open - this will need a Tick or a Timeline, since you want to update the animation every frame. You want a piece of text displayed on the screen - set the widget’s text once, no need to get Tick involved here.

Have a look here for more info:

Hope it makes sense.

Good read, thank! Very informative.

Regarding my example there, yes, it’s a multiplayer game and yes, each player is a villager.

In the case of event tick: if there’s something that needs to be updated every frame, but not constantly (ex: for 10 seconds, but stops otherwise), you can start/stop a loop-enabled timeline instead of using tick.