How to code the sequence of states in a game/app

Having come from a Flash AS2 background with a significant amount of my work being educational games that contain significant elements of voice over sequences, I could do with advice on how to structure my code without the use of a timeline (apologies to those not familiar with a timeline).

For example, one such game I am porting has a sequence as follows:

**Step 1:
Play audio file 0010
Character wave hands
Display another character in a tree

Step 2: to be actioned when audio file 0010 has finished playing
Play audio file 0020
Character stamp feet
Hide character in tree
Play sound effect 1

Step 3: to be actioned when audio file 0020 has finished playing

Play audio file 0030
Character point
Display button**

This sequence continues for another 12 steps before we get to the actual game interaction and at any point the user can click a help button to return to the beginning of the sequence.

So my question is basically how would you recommend coding such a flow of events.

I am thinking it could be done with a large switch statement that branches based on a state variable whereby I pass the next state to the play audio function so it can change the state when the audio is complete.

if state == "0010" then  
  
 playAudio("0010", "0020")  
 animateCharacter("character1", "wave")  
 animateCharacter("character2", "show")  
   
elseif state == "0020"  
end  

I suspect there may be a standard way of dealing with this that those from a non-timeline based background may be able to enlighten me about?

Thanks

Paul
[import]uid: 7863 topic_id: 15960 reply_id: 315960[/import]

@mediakitchen, glad that you have a very good presented question, unfortunately…

you have answered your own question, read up on OpenAL audio.play

you will find callbacks that you can use that can be triggered on completion of playing a sound file, that will be the trigger for your next sound file and so on…

You *can* have a state engine that changes state on completing certain tasks. This is the best way you can achieve a series of things. You will also have to determine the element/point that triggers the change of state.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15960 reply_id: 59102[/import]

Thanks JayantV

I am pleased that I was going in the right direction - it just seemed quite lengthy to have such a huge if else statement. I suppose I could use an associative array with the state ID as the index too.

I did try researching this and came across alot of articles about stuff like finite state machines and MVC design patterns so wanted to be sure I wasn’t missing out on some tricks with regards setting off on the right footing.

Thanks for the reply :slight_smile: [import]uid: 7863 topic_id: 15960 reply_id: 59109[/import]

there are many ways of dealing with that. Now I cannot recommend one over the other as it is a matter of preference.

You definitely do not need a MVC, but I can give you a hint on what I would do…

local function performNextStep(stepIndex)  
 if stepIndex == 1 then  
 doStep1()  
 else  
 doStep2()  
 end  
end  

I could have more optimised code, but then I am not aware of the entirety of your project.

this performNextStep would be called when the end is triggered.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15960 reply_id: 59111[/import]

Thanks a million JayantV - that looks like a good starting point.

Thank you for the swift assistance:) [import]uid: 7863 topic_id: 15960 reply_id: 59113[/import]