Announcement

Collapse
No announcement yet.

GameState and EventDispatcher Query

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    GameState and EventDispatcher Query

    Hi all,

    I'm starting to branch out into unfamiliar territories, not that many of the existing territories have been overly familiar just yet, but I'm plugging away at Blueprint slowly...

    So, I have a number of NPCs in my level. I wanted to make this an objective for the player, e.g. destroy all of the NPCs. As part of this I wish to get the number of NPCs in the level and then display it as a remaining/total on the HUD.

    I have working code for getting the NPCs, e.g. the count. I have the HUD/display all sorted from other aspects of the game (e.g. ammo).

    What I became aware of was that whilst I was initially dumping this into the Level Blueprint, I would then need to replicate all of that for each level. So I set out trying to find out how to have a "count" of the NPCs for the objective at a "higher" level so to speak, which could then just be set.

    I'm not familiar at all with the GameMode/GameInstance/GameState stuff at all at this time, however based on a little reading online (and the docs) it seemed like GameState might be the best place to hold this information.

    I have that all setup and the only part which doesn't seem to be working is the calling of, or the receiving of the event dispatcher. The code gets the count and that's fine, but even adding a simple print node to the receiving bound function just doesn't produce anything, and at the same, I'm not getting any errors.

    I have used the same approach between actors and the HUD and that all worked fine, so I can only assume there is a difference with regards to the GameState?

    Not entirely sure how helpful these screenshots will be, but this is what I have..


    In the HUD Blueprint;
    Click image for larger version  Name:	GetGameStateReference.png Views:	0 Size:	59.6 KB ID:	1695341

    Click image for larger version  Name:	BindEvent.png Views:	0 Size:	106.1 KB ID:	1695342

    From the GameState Blueprint;
    Click image for larger version  Name:	CallEvent.png Views:	0 Size:	118.2 KB ID:	1695343

    As a quick summary of how its setup...

    * I created a Blueprint class based on GameState, I set its parent to GameStateBase (a lot of my game didn't work before I did this)
    * Within the BP_GameState I use the EventConstruct node to call my custom event which returns the length of the array for the NPCs in the level, populates a struct named NPCObjective
    * The OnNPCObjectiveChanged event dispatcher is then called

    Within the HUD...

    * I get a reference to the GameState and set a variable to reference it
    * I create a binding to the OnNPCObjectiveChanged event
    * I would then call my event UpdateNPCObjective which would update the text on the HUD

    The last step currently has a Print node wedged into it to just test, and nothing is output. If I however pop another print statement out on the exec flow after the binding and just try to print out the values from the struct, I can see the numbers, so, the data is there, that part has worked, but for some reason the event dispatcher aspect doesn't seem to be working.

    I didn't encounter this issue when I was communicating between just actors, so my gut feeling is that the GameState aspect of this is perhaps the focal point of my issue, but I don't know.

    Any help would be enormously appreciated

    I wasn't able to find a lot online, but I did encounter someone else who had posted a couple of years ago with a very similar sounding problem, sadly the responses trailed off and there was no solution given;

    https://www.reddit.com/r/unrealengin...for_gamestate/

    #2
    I think I may have found the issue, but I'm not sure how to resolve it...

    I dropped the following into the Level Blueprint;
    Click image for larger version

Name:	LevelBlueprint.png
Views:	75
Size:	69.5 KB
ID:	1695348
    I ran the game and "w00p w00p" appeared on the screen. The nodes are all setup the same as in the above post.

    What I think is happening is an ordering of events issue. In my first post I think the event is being called before the HUD has bound itself to it, so the first one gets missed.

    That would seem to make sense and explain why what is happening is happening, of course it leaves me wondering now how to resolve it.

    Thoughts?

    Comment


      #3
      So this is definitely down to the execution order of things.. I've changed things around a bit so that the GameState now only records the number of NPCs but doesn't try to do anything to update the display at that point.

      The PlayerHUD, after setting up all the bindings, then runs a "ResetPlayerHUD" which calls all of the events to get the information and update the display, seemed the only way to be sure that everything would have executed by the time it was needed. After this point, NPCs being destroyed should be reflected within the counter without issue.

      Is there any documentation/diagrams which can help ascertain what is executed in what order? Unity had an Execution of Event Functions diagram which I always found really useful in cases like this, it was also possible to specify the execution order of specific scripts so that things would run in the expected order. I've not yet seen anything similar within Unreal, although did start using the Construction Scripts based on a loose knowledge of that executing before Begin Play events...

      Thanks again for any info

      Comment


        #4
        Here:

        https://docs.unrealengine.com/en-US/...low/index.html

        Comment


          #5
          Hi Everynone, thanks, I had actually seen that, but its a bit too "high level", e.g. it ends at the spawning of actors... it doesn't go into detail regarding those actors, and the order in which they are constructed. Doesn't cover GameState either.

          Thanks though.

          Comment


            #6
            I would read this and this for some useful background on Startup-Order & UE4's Game-Framework... But overall, I can't think of any good reason to use Gamestate in Single-Player tbh. Its crucial for Multiplayer, with GameMode being even more critical for both SP & MP. Things will probably go easier though, if you just focus on Pawn / PlayerController / Gamemode for now (assuming your game is single-player). You can even just focus on Pawn exclusively, or almost exclusively in the early days of prototyping. With GameInstance being the one exemption (should your game need to open multiple levels and store vars etc).

            As regards execution order, lots of errors arise out of that, especially 'accessed none' errors. So its a good idea to not take any chances with unpredictable execution flow, but instead expect it and plan accordingly. For example, devs often have circuit-breaker like checks to ensure initialization of values, or in this case reliable data dependencies (as you mentioned Bindings). I've seen more technical warnings about using Bindings than there are key advantages. Instead, consider if your Pawn should just manually update the HUD / Widget through its own Tick or Timer from a Cast / Event Dispatcher / Interface call etc...
            Last edited by UnrealEnterprise; 12-08-2019, 12:57 AM.
            ----------------------------------------------------------------------------------------------------------
            What are the BEST Unreal Tutorials / Docs? There are none tbh... Here's why
            Instead its better to just take projects apart (see the free 'creators' listed here)

            Comment


              #7
              Hi,

              Thanks for this, really appreciate you taking the time for the detailed response. I'll check those links out now as well, thanks again

              Comment

              Working...
              X