How to ensure a function runs after GameMode "BeginPlay" code completes?

This is a more detailed inquiry following up on my first post after some further debugging.

To summarize, there are two pieces of code in a project I am working with that are not executed in sequence despite the design clearly intends it to. The base idea is that for “BeginPlay()”, my GameMode Class includes C++ code that reads in data from a json file and stores the strings from each category in public member variables/UProperties of the GameMode Class (which are themselves String Arrays to store the Text lines from said JSON entries)

Subsequently, the Blueprint Function pictured is on a Widget used for one of the Rounds of the Game. Its purpose is to retrieve the RoundTwoQuestions from the GameMode Actor, and copy each index’s value into the Board’s own “Questions” array so they can be loaded during its round.

This “Set Questions” function is called on the Round 2 Board at “Event BeginPlay” by the Player Controller, which includes the BP for creating the various other Widgets needed for Scoring and displaying the other round’s questions and contains a reference to the Round2 Board instance as a Variable.

The issue that I am finding when I check output Logs is that the two functions do not execute in a consistent order between running the project in the Editor and running a Packaged version of the game.

  • When run in the Editor, Log functions I included in the C++ code and the “Set Questions” BP indicate that the functions run in the expected order: “MyGameMode” executes its “BeginPlay()” code which Loads the questions from JSON into the corresponding Member Variables, and THEN it runs the “Set Questions” function from “BeginPlay()” in the Blueprints, resulting in the Round 2 Questions being populated to match the JSON file entries.
  • Whereas in the Packaged version, the “Set Questions” Log statements appear first, setting the Game Mode’s Round 2 Questions as the “DefaultGame.ini” values, and assigning those to the Round 2 Board Questions array. The “Loading Questions from JSON” statements display on the immediate next Log Lines, suggesting they are executed simultaneously or immediately after, but as a result, the Game Mode loads the JSON questions AFTER it has already assign the defaults to the Board variable and thus they go unused.

What I am thus seeking is a way to ensure the C++ code for the Game Mode’s “BeginPlay()” function executes before the “Event BeginPlay” for the Blueprints (be it the Player Controller or simply the entire project). Anything like an “on event complete” or a project setting that ensures the C++ Code or order to Objects executing such function?

A significant amount of this project and code was built before I began work on it, so ideally I would also like to minimize complex refactoring (changing Blueprints to C++ or vice versa) as opposed to tinkering with Project settings or code within the Blueprints/Editor, to minimize collateral breakage or additional issues that may need fixing later.

by calling the function on the end of the Gamemode’s BeginPlay?
you can also make some call dispatched on your gamemode and call it at the end of beginplay and in your blueprint where you want to run the function after gamemode beginplay you can bind that call.

I apologize as I’m very new to UE so I may be learning of and adding additional details in the process of seeking this help. If there are any details that might be helpful to discuss or try to post screen caps of, don’t hesitate to suggest them and I’ll see if I can locate the info.

Is there a way to specify that it should occur at the end of the functions that are invoked by the “BeginPlay” event? My understanding was that the event prompts the Function/Nodes to begin their execution, but I would need to be able to hook the nodes into a sequence in order to ensure one function completes before another one executes.

The Blueprint for “bpGameMode” in my project does not contain any nodes in its Event Graph. The Event Graph itself is blank, but its parent class is “My Game Game Mode”, which in turn contains a function “void AMyGameGameMode::BeginPlay()”. In light of this, do I need to listen for a different event or function in order to ensure the parent class’s “BeginPlay()” completes and THEN fire the event that will call “Set Questions”? I am not familiar with how/if there is a UProperty or anything I can place on a C++ Function (in the header I presume) to allow it to be utilized in a Blueprint if it does not currently appear.

maybe this helps you understand the order things run

1 Like

I have tried setting up an event delegate as follows to Call the “Set Questions” function of Round 2 Board (at the moment Set Questions just has a “Hello World” Print String so I can verify the flow). As constructed here, the Log String does not appear so I do not think this event even first in the Editor. Are there any steps I am missing just at this point of trying to make the Function fire at all from an Event Dispatcher?

bpGameMode

Phase2Board

I managed to find a different solution by accessing the GameMode questions directly as handled in another round’s code, which seemed the ideal solution for the moment to keep similar behaviors in line with each other.

Despite this, I appreciate the replies and direction as they have given me some important topics to look at as I continue learning UE4