Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Chat / Flawed State Machine Code?!

Author
Message
GunnerJnr
AGK Developer
2
Years of Service
User Offline
Joined: 30th Nov 2015
Location: Bridgwater, UK
Posted: 10th Jan 2018 13:09 Edited at: 10th Jan 2018 13:17


I think my logic for this StateMachineHandler() function is Flawed. Going back to an old thread where @PartTimeCoder had made a suggestion to me about using state modes along with game states, (the original thread can be found here but is locked which is the only reason I am making a new thread). Anyway, it seems to be that the code leaves the function and then is recalled as part of the top level loop, on recalling, the state has been updated and it drops into that next state! I am wondering if it doesn't do anything about checking for new states (i.e. it's not interested on if that variable was updated within the switch, it just knows it found the one it wanted, and will now go past anything remaining).

Any advice, help, pointers, examples would be great.

Also I am assuming it doesn't matter I took it out of main.agc and placed it into its own code file, I gather it doesn't need some additional calls to Sync() ?? for clarity I have also included main.agc code below (and before we point out there no includes for game-state-handler.agc all includes are in a separate file called pch.agc)

Kind Regards
GunnerJnr

****EDIT****
I have also attached a small gif of the debugging stepping through the code for a bit better clarity of what is happening inside the Select/Case checking..

PS - Thanks to @Baxslash for the Loading bar code, I have it commented out for now, I will most likely modify it to suit my needs, but for now, it is not needed. I see there is a great community sharing knowledge, @PTC also has some great links in his signature which I aim to have a look at in the near future.

GameStateHandler.agc



Main.agc



Here is the gif showing the debugging stepping through..

Attachments

Login to view attachments
Markus
Valued Member
14
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 10th Jan 2018 14:01
you think if you change select value in case ... the next case is processed but it is not.
AGK (Steam) V2017.12.12 : Windows 10 Pro 64 Bit : AMD (17.12.1) Radeon R7 265 : Mac mini OS High Sierra (10.13)
MadBit
AGK Tool Maker
8
Years of Service
User Offline
Joined: 25th Jun 2009
Location: Germany
Posted: 10th Jan 2018 14:10 Edited at: 10th Jan 2018 14:11
Ok,
your initial state is 'gGAME_LOAD'.
In the first Switch and case 'gGAME_LOAD' you change it in 'gMODE_BEGIN'. This one Change in the second Switch in case 'gMODE_BEGIN' to 'gMODE_PROCESS'.
This all happens in the first gameloop. If the second gameLoop is executed now, your statevariable is still set to' gMODE_PROCESS' and cannot access case' gGAME_LOAD' in the first switch.

Solution would be to work with a kind of substates.
Or with a loop in the first switch/case.

switch / case always deals with only one state per loop. It's the same as if you want to write if... else if... else if.... else if.... else if.... else if.... endif .

thereabout


EDIT:
uups to slow!
Share your knowledge. It\'s a way to achieve immortality. (Tenzin Gyatso)
Mobiius
Valued Member
15
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 10th Jan 2018 14:12
Once in the select block, changing the state will not be reflected within the block until you enter it again. This makes sense as you only check the value at the start of the select block. If you want this kind of effect, perhaps use a collection of If statements.

GunnerJnr
AGK Developer
2
Years of Service
User Offline
Joined: 30th Nov 2015
Location: Bridgwater, UK
Posted: 10th Jan 2018 14:32
Wow thanks @Markus, @Madbit, @Mobiius I hadn't spotted this before now and it all makes sense, I shall go back to the drawing board and rethink its design, I mean probably in a real world example the state modes or games states would likely be called inside some function call to change to relate whatever its needs are, but I was just trying to get a simple example up and running. Appreciate the advice.

Login to post a reply

Server time is: 2018-04-24 13:40:03
Your offset time is: 2018-04-24 13:40:03