How to handle timing of events

Started by
2 comments, last by AbandonedAccount 10 years, 2 months ago
I'm working on a data-driven 2D turn-based combat game (which will eventually be a component of a 4X game) inspired by classics like Space Empires II/III and I'm making pretty good progress, but I've run into a problem with the timing of execution of certain events. I know it's probably making my life a lot harder than it needs to be, but I'm working in WPF currently as I'm trying to avoid involving any frameworks like XNA or Unity (since they seem like they'd be overkill for what I'm trying to accomplish, a crutch that would let me blindly ignore important systems and processes I want to understand, and would take me away from my original reason for starting this project which is learning new technologies, methodologies, practices, and patterns that could apply to my Day Job as a C# applications developer).

In my game, a Ship object is given orders (ShipOrder object), and as part of the Start-Of-Turn process all of the orders on a ship are executed (method on the Ship loops through all orders calling their ExecuteOrder method). These orders include movement (e.g. MoveToLocation), which I built first, and firing weapons (e.g. FireAtTargetShip) which I built later. Each order, when executed, raises an event and the main program subscribes to these events and resolves them.

The problem I'm facing is due to a decision I made when building the movement orders: I wanted the movement of the ship icon on the screen to hesitate between squares (for now, each ship can move 3 squares per turn), so I found and implemented a delayed execution service mechanism (basically, a static method that gets passed an action plus parameters for the action, then creates a timer that will trigger an event to execute the action, thus creating a delay in the action). Link to my code where the delayed execution is called. Unfortunately (and what I think is the crux of my problem), the (delayed) action includes not only moving the image of the ship on the screen, but changing the actual position of the ship internally in the game state.

The issue I can't figure out how to resolve is the fact that while the delayed timer is off doing its thing, the other orders continue to get executed, so I can't figure out how to force the FireAtTargetShip orders to wait until movement is complete before they trigger off, so they're going off almost instantly so their range calculations are done from the ship's position at the beginning of the turn, not where it is when it finishes movement.

I almost need a stack-like or pipeline-like system to pass the results of the ExecuteOrder methods into, where I can add things to the process and resolve them only when the previous item is completely resolved.

So my question is, is it best at this point to implement a pipeline system (e.g ExecuteOrder adds an item to an ordered array, resolution of an order raises an event that triggers the beginning of the next item in the array, etc) and will a pipeline resolve my problem, or is there either an easier/better way to accomplish this (or some fundamental flaw in what I'm attempting to do that will bite me later on)?
Advertisement

I know some turn-based games solve this by having a MovePhaseAtStartOfTurn followed by a AttackPhaseAtStartOfTurn. You can sometimes see this reflected by the animations in commercially available games. Basically, you just need a sync on starting the attack phase to completion of movement. Technically, you could even just add a wait equal to the maximum animation/movement time required if you don't have the ability to sync. That's hacky since it might break if you add a longer animation/movement later, so definitely better to sync if possible.

You need only a small modification to your Order class, most likely.

Just stop executing Orders if one returns a code that it is going to take time. Keep trying to drain the Order queue for the unit each frame until it's empty and the game state can advance. The Order object can then be in charge of knowing if it has to wait for an animation to complete or some wait timer to complete. There's not necessarily any reason to have one queue of Orders feed another queue.

Sean Middleditch – Game Systems Engineer – Join my team!

You need only a small modification to your Order class, most likely.

Just stop executing Orders if one returns a code that it is going to take time. Keep trying to drain the Order queue for the unit each frame until it's empty and the game state can advance. The Order object can then be in charge of knowing if it has to wait for an animation to complete or some wait timer to complete. There's not necessarily any reason to have one queue of Orders feed another queue.

There are a few limitations to keep in mind with this approach. In multi-threading execution of orders, "wait for me" status becomes a lock and that's an awful lot of locks. Order of execution becomes important (unit A moves and fires at B, unit B fires at A. What distance was B at when firing at A?). And you always cause a delay equal to the longest dependency chain's animations, which can be really bad if you allow move-fire-move orders.

None of this is to say the approach should be avoided as long as you understand the limitations. If you do not have actions interacting with one another and are single-threaded, it probably works fine. While I think having a move phase where you process movements and requeue other orders is easier, other programmers will certainly disagree. That's the art aspect of programming.

This topic is closed to new replies.

Advertisement