Game Mode, replication, and running on clients

Ok, I get it, Game Mode only exists on the server, so a client can not cast to one (and thus not access variables or functions). make sense.

What is killing me, is it seems the only OnPostLogin is in the game mode, so when a person connects (Multiplayer), it appears it should run that in the game mode, that’s fine. From that I want to pop up a menu (widget)… so I have a custom event made in my player controller, I cast to that and call that, and ahve it set to RELIABLE Replicated to Owning Client. Sounds find, sounds like it should work.

However, what happens is I host in one window… menu comes up, works, go into game fine. Then I open another window and connect to the host, it pops that menu up ON THE SERVER. How is that replicated to owning client?

This is driving me crazy.

All I am trying to do is when you enter a map, have it pull up a menu to select a character class. Works fine the way I had it IF the client connects in a predefined ‘pregame’ time that is counted down by a timer… if the state has already changed to ‘ingame’ though (this a variable that is replicated), no dice… it skips the onrep stuff I have for the state for ‘pregame’… as it should (duh)… so I tried to set the class selection to pop up via the Event OnPostLogin … but as I mentioned above, no dice.

What am I missing?


show me full way from post login to add to viewport.


Now that I am looking at it, seems I should attach the new player pin to the target on the cast… not the ‘get player controller’… will try that.

argh… that was it. was just so used to using ‘getplayercontroller’ all over…

Well, that fixes that, however now it always set’s the current game state so it get’s reset… always something.

easiest answer in my life shaytan.gif

LOL. Perhaps you can answer this one… I set event begin play to actually set the ‘currentgamestate’. It works, prints out the string I added to check.

What you do not see is at the beginning of ‘topregame’ it checks that variable, and I have a printstring there as well for testing, that prints out that we are NOT in ‘preGame’…

So how can ‘toPreGame’ be called BEFORE the stuff from Event Begin Play?

construction script?

I put it there (to set current game state) just to try, no difference. Somehow the topregame is getting fired off before anything. The above image is the ONLY place it is called in the BP.

And the event itself has this to monitor:

And the output of all my print strings when you run the game:

Which ofcourse first msg is at bottom, it prints that in ‘show selections’, so that is called before the stuff in BeginPlay somehow… then the red one is from topregame print string (i added append with CGS: in front just to make sure that was what I was seeing)… then the purple is from Event OnMatchState … after the first one, THEN the state get’s set to preGame …

Mind boggling.

so, begin play triggers before construction script, that’s what you saying?

Well, I put printstrings at the start of construction script, onpostlogin and begin play… this is what I got:


So construction script fires first, then onpostlogin THEN begin play … ??? seems strange to me.

So I just added this to the construction script:

And I get the same output as above… currentgamestate is still not set… (even went back and changed the cast to an impure and hooked it up, same thing.

Now I wonder, that variable is stored in my GameState, is it possible that the Game state is not yet created when the Game Mode fires off?

added more print strings, one in the game state constructor, that fires early… the other in the Tick in the game state, to print out what the current game state is:

So while the game state is made, it appears perhaps the variables do not exist until after the match state hits ‘inprogress’ … good to know I guess… how to deal with it is a different question. Since clearly the OnPostLogin is useless as it fires before the rest of the system is ready.

Well, i set a boolean based on the return from OnSetMatchState … so it only is set to true once we get to ‘InProgress’… that and this hack and the OnPostLogin is delayed until things are actually running, so things work right (who knows what else I may find in all this in the morning since I am heading to bed)…

The real interesting thing to me though, is while OnPostLogin fires and things work right for now… once you hit the menu that pops up, and it clears… OnPostLogin is fired AGAIN… this is with one ‘player’, one window… so would be good to know what else fires that one…

I think tomorrow I want to throw together a blank project and just print from all the constructors and begin plays and such and actually record what order game instance, mode, state and player controller, character and state all fire off.

Well made a game mode, game instance, game state, controller, playerstate, pawn, hud… set them in the project defaults. All construction scripts run first.

Then the game mode onpostlogin runs (which still doesn’t make sense, as who logged in, or why before things are ready?

Then the match state goes to WaitingToStart (yeah, after onpostlogin)…

then pawn is constructed and possessed.

Then begin play for controller, pawn, hud, gm , gs, ps.

Then match state goes to InProgress

Then everything beings ticking, gm, gs, controller, ps, pawn, hud… in that order.

Interesting stuff, clearly one must build in a delay in onpostlogin (if you want to use it) to wait until you hit match state InProgress… and in the beginplays as well to ensure everything is properly initialized.

iYOXpB3.gif saved to bookmarks

Now that I have that figured out and use the MatchState to drive things… I need to figure out how to have a loading screen show the whole time the map is loading… if you show one, then Open Level, or Join Session… once the new map loads it wipes out the widgets (or seems to), so you get a flash of the map… because the first MatchState the machine is running to catch is WaitingToStart… and if you pull up the loading screen elsewhere, that stuff isn’t loaded and ticking yet. hmm…

Loading Screen is not possible in the current versions of UE4 using BPs only. The widgets are killed when unloading the old map. ):

Also, the OnPostLogin is called for Client and Server. So when you start the Game, the Server is also calling “OnPostLogin” and will check the State.
By delaying it, you give the BeginPlay some time to call and then check the State.

You also need to remember, that data needs time to cross the Internet/LAN. If a client joins, the replicated value of your State isn’t directly there. It needs
a few seconds to get there. I have a project called “Ball Bump” on my free project thread that shows how to handle game states in multiplayer games.
Check my signature to see if you can learn something from it (:

How does one do it in C++ then? Perhaps we are overdue for a plugin since there are so many things ue4 has left out of BP’s that are kinda important.

For C++ you can check the ShooterGame. I have not looked into it for this myself, but people tell me to look into it when wanting a Loading Screen in C++.

Had to comment because of this comment (Y) :smiley: