Jump to content
  • Advertisement
Sign in to follow this  
ghetalion

Event Handlers!? ARGLEARGLEARGLE

This topic is 4726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm coding a MUD In PHP And I've built enough helper functions to truly give me the flexible design tools I need to accomplish the task, from referenced cell search returns to a dynamic object container that saves its own data to a file automatically... everything is going smooth! The entire package so far is 90k in solid, well-documented, modular code. But data flow is killing me... - A SOCKET is socket/server data about the actual connection. A connection holds the sock_id, the IP, the input buffer, a temporary cache, and the account id it is logged in on. - A PLAYER is human data about the CONNECTION as well as a list of characters the CONNECTION has at his disposal. - A CHARACTER is server data about the virtual character within the game.
Game Loop Core
{
	Update gametime
	(CHECK) Socket Activity
	{
		(CHECK) Activity on listening port
		{
			Create a new connection and set’s state to new_user
			Show menu;
		}
		Else activity is from an established socket
		{
			(CHECK) To see if not link dead
			{
				(CHECK) Activity for return carriage:
					Parse input;
					Appent input to the Event List;
			}
			Else player is link dead
			{
				Remove connection;
			}
		}
	}
	(CHECK) AI Activity
	{
		Add weather, map, monster, and other automated activity to 
		the Event List.
	}
	Run Event Handler;
}
The EVENT LIST contains a massive array of all changes that have taken place since the last game loop. The EVENT HANDLER navigates the event list and determines how and what events are executed. All events are timestamped in gametime for when they are to be processed. Player inputs are timestamped at 0 for immediate execution.
Event Handler
{
	(CHECK) Events that have already expired
	{
		(CHECK) Player Event
		{
			Based on connection status, send the player to the appropriate area.
			If the player is currently playing: Execute Event; 
		}
		Else it is a different kind of event (combat, item, spell, etc.)
		{
			And do it!;
		}
	}
}

A problem I just realized is that since my event handler is using timestamps... I've hit what appears to be equivilent to red/blueshifting. When the game loop runs, it checks to see what tick the game is at. (I've set it to 1 tick being equal to three seconds, but this can be changed.) The game loops once and it updates the time at 1 tick... it runs though 1000 playerss input and 10000000 AI event appending and then proceeds to check to see what events are executed based on the clock. Now given that when the timer was updated way before the original loops, the real-time has progressed significantly at this point. When I run the game loop again, I appear to hit a situation where upon calculation, it is possible for the tick sequence to skip numbers. One solution is to not base it off the timer, but the actual loop itself. I disagree with this because the faster yoru CPU is, the faster the game loop will go, and thus, the entire game will move at a ridiculous pace. MY question is: is the tick skipping detrimental to an event handler that triggers based on tick-count? Events only are executed when its timestamp is less then the current tick. This would mean I need some sort of compensator to determine event priority then, correct?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
What is the problem? What is a tick? What is the problem with skipping ticks?

Why not make the event list a priority queue so that you can easily look at earlier events before later events?

Share this post


Link to post
Share on other sites
If your "current time" tick count > any event timestamp, run the event. However, run the events with lowest timestamp first, and run events with the same timestamp in order of insertion into your event list. So it's just like the AP said - you need a priority queue instead of a simple list.

Share this post


Link to post
Share on other sites
That is basically what this is. A priority queue.

However, there are events that occur that are outside of the instant realm. Say, doing a skill, casting a spell, etc. I need these events to go off at specific times, not as soon as all the other events above them have gone off.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It is entirely unclear that a problem even exists. If all the events have a timestamp for when they are supposed to occur, they are placed in a priority queue, and events are dequeued and executed up until an event is found that is not supposed to occur yet, then where is the problem?

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
It is entirely unclear that a problem even exists. If all the events have a timestamp for when they are supposed to occur, they are placed in a priority queue, and events are dequeued and executed up until an event is found that is not supposed to occur yet, then where is the problem?


That's what I wanna know. :O

3000 Events goes of at tick 8
120 Events goes of at tick 9
1000 Events goes of at tick 10

It is now tick 8 and the tick is still at 8. When the events have been done, the tick is now at 10. So in theory, events at tick 9 would go first, then events at tick 10. Doesn't this keep my tick timer significantly trailing behind the real-time?

Share this post


Link to post
Share on other sites
Why are you defining events in real-time? Because you can't guarantee it, as you've discovered.

So would there be a problem if you only constrained a minimum time for the loop to run? ie. a tick would happen after each game loop, and if the loop takes under 2 seconds, then you pad it out to 2 seconds. EDIT: And if it takes more than 2 seconds, then the tick just turns over when the loop ends.

There should be no time-critical events in your game engine, especially in a MUD. If my computer isn't fast enough to run an RTS game, the game just slows down - its internal state doesn't go barmy. It should not be any different for your game.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!