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?
[game programmers] Event systems.
Moderator: General Moderators
I'm not sure of a state machine would be the best pick for an event system.feyd wrote:I usually use state machines for events.
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?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.
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 
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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 '';
}
}
}(#10850)