I moved to a new URL! Check it out!

Dev Log: Event Queues Everywhere

Dev Log: Event Queues Everywhere
I've been working on a little prototype involving turn based combat. I'm not quite 100% ready to dive back into Stratoforce yet, and I had this idea gnawing at me and I just have to get it out of my system. What exactly is the idea? Well I can't say what it is yet, but if it works out then I'll talk more about it!

Since it is based around turn based combat I had to figure out how exactly that works. I'm so used to programming things that happen all in real time that suddenly going into a turn based game makes absolutely no sense to my brain. At first I had these huge state machines that were incredibly clunky, then I transformed it all into events that can be placed into a magical event queue.

Here's a simple class that can be dropped into Otter that is an event queue manager. It was whipped up very quickly so there might be some slight issues to debug. Check it out:
/// <summary>
/// A Component to manage and process queue of events
/// </summary>
class EventQueue : Component {

public List<EventQueueEvent> Events = new List<EventQueueEvent>();
public EventQueueEvent CurrentEvent { get; private set; }

bool isFreshEvent = true;

public EventQueue() {

}

public bool HasEvents {
get {
return Events.Count > 0;
}
}

public void Add(params EventQueueEvent[] evt) {
Events.AddRange(evt);
}

public void Push(params EventQueueEvent[] evt) {
Events.InsertRange(0, evt);
}


public override void Update() {
base.Update();

if (CurrentEvent == null) {
NextEvent();
}

while (CurrentEvent != null) {
if (isFreshEvent) {
isFreshEvent = false;
CurrentEvent.EventQueue = this;
CurrentEvent.Start();
CurrentEvent.Enter();
}

CurrentEvent.Update();
CurrentEvent.Timer++;

if (CurrentEvent.IsFinished) {
isFreshEvent = true;
CurrentEvent.Exit();
CurrentEvent.EventQueue = null;
Events.Remove(CurrentEvent);
NextEvent();
}
else {
break;
}
}

}

void NextEvent() {
if (Events.Count > 0) {
CurrentEvent = Events[0];
}
else {
CurrentEvent = null;
}
}
}

class EventQueueEvent {

public EventQueue EventQueue;
public int Timer = 0;
public bool IsFinished { get; private set; }

public void Start() {
IsFinished = false;
Timer = 0;
}

public virtual void Enter() {

}

public virtual void Update() {

}

public virtual void Exit() {

}

public void Finish() {
IsFinished = true;
}
}

This is sort of similar to just a very simple behavior tree (see Kodo for more advanced stuff!) Basically what happens is there's a list of events that all need to be run. The current event is run during the Update, and if it's finished then it's removed and now the next one starts up. You can both Add and Push events into the Queue. Push will insert an event at index zero so it will be the next thing to run. To make events just make classes that extend EventQueueEvent!

So I have events like "Damage" "ActorDefeated" "ChooseTarget" "TakeActions" and more. It becomes very easy to manage when things are broken up into separate classes like this! This is actually also giving me ideas on how to run cutscenes in more real time action type games as well. Maybe it will be of some use to you!
new comment!

Post your comment!

Name
Email
Comment