PDA

View Full Version : Event processing (in a game)



marmin
09-12-2007, 02:19 PM
Hello.
On a fundamentakl level, how is event processing generally implemented.
With this I mean, on a very basic level of course sprite goes to top, needs to stay there for 5 seconds, goes to right for 3 seconds, etc.
I have made a very fine system with counters. Which work ok.

If I think about it logically, I can use the system timer, or I can make counters.I am reluctant to use the system timer because I want to control and manipulate if necessary (what an ugly word arhgg).
Is there another way to make it efficient if i don't want to use a system timer (wich is a counter in itself, hehe).

I am now pondering making a main system timer in the game, and do it''relative' , i mean every time a sprite is created an interval is put on the heap and when the interval is gone the sprite needs to be gone. Simply put. This avoids having numerous counters.


regards.

jdarling
09-12-2007, 03:38 PM
Typically messages are of two types; global and local. In either case they are handled in the same manor with the only difference being that the local messages require a local message stack that is not visible to the outside world to be in place while the global ones are all held in a single stack that anyone can view.

The problem that you describe, moving along a set path over a given time, really isn't something that you would implement using a message system. Instead it requires a State Machine or process script that "tells the actor what to do" and "says what the actor is currently doing".

I'll try to cover both topics here for a minute, but know there are 1000's of articles on this and a bit of time with google would be your best bet.

At the basic level a message stack is a FIFO (first in first out) stack that has the ability to push a message onto the bottom and pop a message from the top. This allows messages to be processed in the order that they were presented to the target (typically called an actor).

Basic psudo code for the stack:
AStack = object
Items = collection
method Push(What)
id = Items.Count
Items.Count++
Items[id] = What
end Push
method Pop() -> Top
if Items.Count > 0 then
return Items[0]
Items.Remove(0)
else
return nil
end
end Pop
end

Now, you could give priority and timing to messages (such as windows and some games do) but this is really a waste of processing time since you have to iterate the stack looking for the next valid message.

Instead you setup a state machine that knows what to do and when. You build a state machine by building rules and then implementing those rules in code.

Here is an example set of rules to make an actor move in a rectangle:
If the actor is at 0, 0 then walk right to 10, 0
If the actor is at 10, 0 then walk down to 10, 10
If the actor is at 10, 10 then walk left to 0, 10
If the actor is at 0, 10 then walk up to 0, 0

This would be a very bad thing to use exactly, but its a good example. Remember that your rules should take into account the unexpected happening (in our example above the actor manages to walk to 11, 10 before the next process loop).

Here is psudo code again:

case Actor.Position of
0, 0 : Stop; Start Walk Right
10, 0 : Stop; Start Walk Down
10, 10 : Stop; Start Walk Left
0, 10 : Stop; Start Walk up
else
Continue Walk
end

I hope that gives you a very basic starting point and at least gives you some good search terms for google :).

marmin
09-12-2007, 05:24 PM
maybe my first question was too general..
but, whst time base do you use to trigger an event. (so, an event that is time based). I don't want an event to happen at a position but at a certain time.

EricLang
09-12-2007, 11:58 PM
Probably you have a game loop in your game which keeps track of the time.
I think a good way to implement this is to have an object (or objects in a list) check the time during the game loop and fire the event at the appropriate time.
Adding a procedure to your gameloop could do it:
(I hope it's helpful)

procedure TGameEngine.GameLoop;
begin
repeat
CheckEvents; // add this method call
GameTime := GetYourTimeSomewhere;
// Do stuff
until TheSunExplodes
end;

procedure TGameEngine.CheckEvents;
var
i: Integer;
EventObject: TEventObject;
begin
for i := 0 to EventObjectList.Count - 1 do
begin
EventObject := EventObjectList[i];
EventObject.Check;
end;
end;

procedure TEventObject.Check; // virtual method
begin
if fGame.GameTime = 666 then
FireEvent; // concrete implementation
end;

marmin
10-12-2007, 12:22 AM
Thanks. I think, i am gonna make a separate routine which checks if the time interval of any event , of any object has passed.. this will make it much more complicated but that's life.

WILL
10-12-2007, 01:49 AM
Well any event-driven game or sub-system requires an update function to actively check for active events and their conditions. If one of those is time then it would be best to make an event timer that can be manipulated by other events or script depending on your game engine.

Such a timer at minimum would require a means of (re)setting, starting and stopping it. You can then, depending on how you want to use it, use it to check for event conditions to trigger as they are set off.

If you wanted you could have more than one timer so that for example you had one for the entire game time and then one that was stopped and started for other in-game scenarios.

Sort of depends on what you are planning on doing.

Generally what you are looking at doing is creating your 'event' object that contains a 'trigger' or series of triggers and it's 'action' or result. Which it's self could just be activating another event, it's up to you how flexible you want your event system to be.