Variables are not taken properly from other BP via "Get All Actors Of Class"

Hello guys,

I am creating a survival horror game and now I’ve been stuck for a while. I am relatively new to Unreal Engine, so it might be a basic issue. I’ve created a system where player can examine objects by clicking on them. Tracing objects is working fine, as well as displaying “Click to Examine” text. I set variables to Event Tick in FirstPersonCharacter BP to check if Widget (Click to Examine text) has been created. If true, set Boolean Variable to true. If False, set Boolean Variable to false, and so on. Problem is, in the traced object’s blueprint I’ve linked Construction Script to FirstPersonCharacter via Get All Actors of Class. When I try to get it and check if Widget has been created, it doesn’t return expected value, it always reads it as “false”. I am probably doing something wrong with reading other BP’s variables.

I am attaching pictures so you can see the problem.
Ignore the battery part. It’s just part of flashlight.

Thanks in advance!

Tracing object and creating “Click to Examine” Widget in FirstPersonCharacter BP; Part 1:

Tracing object and creating “Click to Examine” Widget in FirstPersonCharacter BP; Part 2:

Getting variables from FirstPersonCharacter to the traced object:

Testing if widget “Click to Examine” has been created. If yes, Hello String. If no, Goodbye String:

In game. Widget not created - Goodbye String:

In game. Widget created - Goodbye String anyway:

Enable debugging so you can see the control flow.

Does your FirstPersonCharacter exist when you placed your PhotoBP actor or is it spawned with on BeginPlay using a PlayerStart? The construction script doesn’t run on BeginPlay but only whenever your PhotoBP actor is compiled / moved in editor. Do you get “Accessed None trying to read property …” errors after playing in viewport? Can you please print the DisplayName of your GetFromChar? If it’s not something like “FirstPersonCharacter_1” this is your problem.

Anyway, I don’t like that you’re reading a variable from your character on EventTick in each actor you can interact with. If those aren’t many this might be ok but it would be a more beautiful solution that the character tells the interactable actor that it’s currently looked at. I would use an interface for this so this will work with many blueprints without a cast for each one.

And whatever you’re planning to do with your DeltaSeconds == 0.1 nodes, I doubt it will work :smiley:

You’re setting something named Get from Char, where are you setting that? Quick trick, make that Get from char thing a visible variable so that you can see it in editor.

The print string node is your friend too.

Read about interfaces, dispatxhers and blueprint communication. Get all actors of class then get(0) at first glance looks like easiest way to communicate, but it is not unless you know exactly what are you doing.
There is load order (and worst part of it that you cannot be sure it always loads in same order). So when something with getallactors of class works in editor it is not guaranteed it will work in cooked game.

I found out (trough some experimenting) that GameState blueprint is always one of first spawned (usually before anything loaded into level). So my way of doing all those references and storing them as variables is by using GameState blueprint.
I simply create variables for all pointers (references) that i need in my game, then when actor spawns it sets its OWN reference inside GameState.

For eg:

  • i made REF_MyPlayerPawn inside BP_GameState
  • when MyPlayerPawn spawns, it finds BP_GameState (get game state node),
  • then it sets REF_MyPlayerPAwn variable to pointer that is SELF

This way i can do two checks with one stone. If reference is not valid it means that bleuprint is not spawned yet, and i can get reference to it without any GetAllActorsOfClass.

Nice trick, Nawrot! I was aware that game state is awesome for storing that kind of variables, but I forgot I can use get game state to set the references…