[game programmers] Event systems.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

[game programmers] Event systems.

Post 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?
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I usually use state machines for events.
Soogn
Forum Newbie
Posts: 11
Joined: Sat May 27, 2006 12:53 pm

Post 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!
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post 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 :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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 '';
        }
    }

}
(#10850)
Post Reply