Page 1 of 1
[game programmers] Event systems.
Posted: Sat May 27, 2006 2:27 pm
by Flamie
Hey guys, I'm trying to find new ways of programming event systems in games, the way I've always done it was pretty direct and "hardcoded" where I would have a variable called "event" which held a string of numbers such as:
"-2-53-123-532-623-6346-123-"
And each of those numbers corresponded to a certain event that the player had completed, and I would have on a piece of paper which number was for which event and I can then do the approriate checks where needed, like:
if event #53 was done
this character says "welll done"
else
this cahracter says "go do the job"
So, have you ever coded an event system? If so did you do it the same way or do you have a more convenient way of doing it?
Posted: Sat May 27, 2006 2:32 pm
by Todd_Z
Use a class with an array and use push and pop functions to that array.
Also, use constants [define()] to determine what event is happening, instead of saying its event #52, you could say CHARACTER_DIE.
Posted: Sat May 27, 2006 3:07 pm
by feyd
I usually use state machines for events.
Posted: Sat May 27, 2006 4:26 pm
by Soogn
I wish i could be more help, but this is something I've been wondering about too, for quite a long time as well!
Posted: Sat May 27, 2006 6:26 pm
by Flamie
feyd wrote:I usually use state machines for events.
I'm not sure of a state machine would be the best pick for an event system.
State machines are usually for things that can go back and forth, and forth from 1 state and not from the other etc
At least thats how I was taught:
[walkright]<---------------->[idlestate]<-------------->[walkleft]
that would be the most basic state machine, and probably the 1st one everyone learns.
But I might be completly wrong, could you elaborate what kind of state machine you would use?
Posted: Sat May 27, 2006 9:29 pm
by feyd
Most of my event work is, in reality, state changes. The individual states choose what state (if any) they move to after completing. That's also how we have done all our animations in the past.
Even when they aren't state machines, I often employ a cross between a state machine and message pump like set up for handling them. In roughest terms, it's a switch statement, a simple single point of entry, for the handling and dispatching of the events. If can easily be handled by objects and arrays thanks to some of the array functions php brings to the table.
Posted: Sat May 27, 2006 10:02 pm
by Flamie
ok great, so I wasnt too off with my idea, I gotta admit ToddZ's idea is really good, being able to associate a string instead of numbers would make things a little more clear and easier to manage. And managing the whole thing thru a class, I'm gonna sleep on it for a few days try to figure out the most efficient way for me to implement it in the current engine I have

Posted: Sun May 28, 2006 12:47 pm
by Christopher
How about something like:
Code: Select all
class Event {
var $states;
function __construct ($player_states=array()) {
$this->states = array(
2 => array(
'done' => 0,
'name' => 'Hit on head. ',
'init_text' => 'Look ... a penny. ',
'done_text' => 'Wham! Well done! ',
),
53 => array(
'done' => 0,
'name' => 'Kick in rear. ',
'init_text' => 'Look ... a pence. ',
'done_text' => 'Fwoop! Well done! ',
),
);
foreach ($player_states as $id) {
$this->states[$id]['done'] = 1;
}
}
function hasState($id) {
if (isset($this->states[$id])) {
return true;
} else {
return false;
}
}
function setAsDone($id) {
if (isset($this->states[$id])) {
$this->states[$id]['done'] = 1;
return true;
} else {
return false;
}
}
function getMessage($id) {
if (isset($this->states[$id])) {
if ($this->states[$id]['done']) {;
return $this->states[$id]['done_text'];
} else {
return $this->states[$id]['init_text'];
}
} else {
return '';
}
}
}