I am currently using variables in the gameinstance to store references to multiple actors in the level. I then access those variables in other blueprints to manipulate them.
I use the level blueprint to set the variables on Begin Play.
The problem I am having is that ALL the blueprints Begin Play fire almost instantly when the level starts running. This has caused a timing issue with the variables being set. Some blueprints try and access those variables before they have any content in them, Resulting in blueprint failure.
How is this done properly? What is the magic sauce I am missing. Thanks in advance.
here is my hack attempt at this issue. note that I have no idea what I’m doing and there may be some obvious setup that I’m missing. I didn’t found it so here is what I’ve come up with.
if you have pieces of code that depends of other variable being set, one way you can do is to use events.
so, when a specific variable had been set you send a message/event saying that the variable is ready. then, another part is listening and will run a piece of code knowing that the variable is set.
it is mandatory to understand how to send and listen to events in the same blueprint or in other blueprints or on different objects in the scene. spend some time trying to figure out how all this works. don’t forget UIs objects while you are at this step.
then even inside a blueprint chain (one node after another) some nodes will not wait for the task to be executed and will allow the execution to move ahead. so it is important to know which one of them behave like that (for example the nodes that have a watch icon) and again plan breaking the execution in steps that will wait for an event to be fired when everything is ready.
this really gets complicated fast so write everything down because after a couple of weeks you will have no idea what everything does. True story.
The issue you asking about could be minimized by unreal if they will ever introduce State Machines like unity visual scripting has. I will not go in details but unity has this way of breaking functionality in pieces using these state machines that will jump from one part of blueprints to another similar with how the animation nodes behave in unreal (and unity) But most never use it from what I’m seeing from various tutorials.
Once I’ve figured out that part from unity I was really disappointed unreal doesn’t have it. I was convinced unreal blueprints node were far superior to unity way of doing things but in the end it was not so clear cut.
Thanks for prompt reply.
Yeah I am aware of and use custom events, interfaces, passing events et al.
I already use those in other parts of the project.
I was just hoping there was a simpler way of doing it. eg if there was a “prepare level” event, that I had missed, that could be called and was guaranteed to be completed before any begin play event fired, or a you say a “state machine”.
It’s not a good idea to store actors from a level in a game instance because, after changing a level, invalid references will remain in the game instance.
However, you could use the following solution to make it work:
- Game instance:
- Level Blueprint:
- Some Actor:
In the example, the actor will send requests until the Game Instance is initialized.
I hope it’ll be helpful to you.
Thank you for the prompt response.
Agreed, re storing actors in the game instance is not good. However this project is a single level project, so will (hopefully) not be an issue.
Reviewing the code, it all seems to make sense to be, however I am not sure how your example would solve the issue of other "being play"s firing and failing because the custom event “Trytogetactors” has not completed yet. Thanks.
You can’t rely on BeginPlay in one actor being executed before any other BeginPlay. This is not solved by adding a delay or timer and assuming another actor is ready, because it might not be. What I’d do is let the actors “register” themselves to the game instance on their BeginPlay, one by one. The game instance does not really need to do anything, it can just check which actors registered and perform actions on only those. You can also add an “unregister” method to the game instance which actors themselves can call to unregister themselves before they are removed.
One method i would use is sqeuences and booleans, after all the variables are set they also set a boolean if it has done so correctly(you would have a branch at the begining of each sequence checking the booleans state, initially false)
The last part of your sequence would we an AND gate and a branch if any are missing loop that graph back to the start of the sequence, you could add a 1 second delay at the end if it needs more time.
And if you want the long and proper way to do things, add a couple of print text nodes and hook one up to the delta tick from event tick and the other the name of the node your checking and find which one is going to fast and add an appropriate delay.
Sometimes a combo of both is great.