Timeline challenge


I am in the midst of coding an interactive installation. The installation has a number of elements that interact over time.

I have coded all the ‘strings on the puppets’, i.e. the skeleton to manipulate the on-screen elements.

However, I am stumped on a good programmatic approach to implement a time line. The installation has a number of phases over time. Each demanding different behaviours by the elements.

The requirements are that the timeline structure can

  • kick off events at points along a timeline.
  • run cyclical events/motions during certain phases of the timeline

I started to code a timeline structure from scratch but after 2 hours I thought I must be re-inventing the wheel. A search of the web is not delivering much fruit for me.

Does anyone know of any existing structures I can integrate, or methodologies I could code?

Any help would be greatly appreciated.



It sounds to me like you can use some finite state machines to define the behaviors, the states define what each element should do at that moment, then in each state there’s some possible transitions, triggered by the events, an event can be here that a certain amount of time has passed.

To implement the state machine, you only need some switch case:

void update(){  
void mousePressed(int x,int y, int button){  
    else if(button==1)  
void stateMachine(int transition){  
switch state:  
    case STATE_0:  

and then in your object you define it’s behavior based in it’s state. If you have different objects with different behaviors, you should have a different stateMachine for each of them although the event triggering functions can be the same for all of them: if an object receive an event for which it has no transition at the state it is, it just remains at that state, so nothing happens.

1 Like

hey! another kiwi! wuhkkuhd! chur! bro! etc. (this is choice - when i left NZ two years ago no-one had even heard of this stuff, now there’s folks making it…)

i think you want to veer away from the concept of a ‘timeline’. this is something that only remainsl in Flash/ActionScript because of the legacy of Flash as an animation tool rather than a programming environment, and IMO the timeline should’ve disappeared a long time ago. if you want things to happen after a specific amount of time, have a timer attached to all of this somewhere and change things when it fires… but don’t try and do a full-on Flash style timeline.

basically, i’d do something along the lines of what Arturo said, but i wouldn’t define the state machine as an external object like that, i’d just make it part of the code for the class itself. when i worked for Sidhe i picked up the way they did this, and have been doing it ever since. their approach was to have an enum of states and then do different updates based on the state. eg for a cow, a cow can be standing, walking, running, eating grass, or chewing cud, so you’d have

class Cow {  
CowState state;  

then in the constructor or setup() method:

state = COW_STATE_IDLE;  

then in the update:

if ( state == COW_STATE_IDLE )  
  if ( check if we want to be walking now )  
    state = COW_STATE_WALKING;  
  else if ( check if we want to be eating now )  
    state = COW_STATE_EATING;  
  else ....  
else if ( state == COW_STATE_EATING )  
  // do eating update: make belly more full of cud,   
  // reduce amount of grass, etc  
  if ( finished eating )  
    state = COW_STATE_IDLE;  
else ...  

this is kind of cool because it becomes quite easy to come up with emergent behaviours just by coding up simple rules like this…

anyways, hope this helps.
d from Wellies :slight_smile:

Oh yes, I didn’t meant to make a different class just to control the states, only a function in each class that needs to maintain it’s state. The important thing is the state control is not scattered among different parts of your code, if not when you try to modify the behavior everything gets really complicated.

Also making a drawing of the states and transitions between that states is really helpful.

Fantastic! I’ve implemented this morning and so far it is going great.

Thanks for opening my eyes to the world of FSMs. I had a good read up on the concepts today and can see the magic.

Unsurprisingly I’ve used a hybrid of both of your code variations.

damian: I took your advice and scrubbed the timeline approach and each ‘scene’ is now an entity in its own right running on a timer. So far so good.

arturo: I found separating off the stateMachine function helped with the logic in my head. Using the update() function of my object to fire ‘events’ into the stateMachine creates a good delineation when things get complex.

damian: Joel Gethin Lewis introduced me to OF when he was down in NZ a few months back. Some of explorations possible with OF are blowing my socks off. For this latest project I am using Box2D (based on christopher baker’s example) + OF + utilisation of the openCV addon.

Much thanks to you both, a very educating morning. Chur. :wink:

Hi there

I’m looking into the possibility of maybe using a finite state machine for a project I’m working on, and I stumbled across this topic (your code example has been really helpful btw thanks) - I’m pretty much trying to simulate a fish tank with food triggering behaviours etc. I was wondering how / where you would define your states (COW_STATE_WALKING, etc)?



i’m not sure exactly what you’re asking, but: the state names (eg COW_STATE_WALKING) are declared in the enum {} statement, and the behaviour for each state is only implicitly defined in the update() or whatever methods.

Hi damian, thanks for your quick reply…

I think what I’m trying to say is that I’m having trouble with the placement of my behaviours for different states. I want to have some fish which are either hungry, excited, sleepy, or content.

I’m using a simple flocking method for their movement and all I want to do is change some of the strengths of this flocking depending on the differing states.
I know how to do this but my problem lies in not knowing how / where to define the states - FISH_STATE_HUNGRY, FISH_STATE_EXCITED, FISH_STATE_SLEEPY, FISH_STATE_CONTENT.

Do I need to define everything in complete detail in the update() of the class or should it be added to the .cpp of the class - and if so how?

Really sorry if this sounds basic, I’m new to c++, I’ve read up on state machines, uhnderstand the concepts but just can’t get my head around applying the theory to this piece of work.

Any pointers or help is greatly appreciated.


hey, no problem

say you have three variables eg speed, tightness, energy for the flocking engine. then in your update(), at the top, just go like this:

if ( state == FISH_STATE_HUNGRY )  
  energy = high;  
  speed = low;  
  if ( /*not hungry any more*/ )  
    state = FISH_STATE_CONTENT;  
else if ( state == FISH_STATE_EXCITED )  
  energy = high ;  
  speed = fast;  
  if ( /* not excited any more */  
    state = ...  
else if ....  
// then   
// put the flocking engine code here  

Hi Damian

A few days to let it sink into my head and it’s all ‘clicked’! I was overcomplicating things. I have now split the behaviours up to create a sort of fake state machine - webcam movement controls some of states and time elapsed / time of day controls the others.

Thanks again!