View Full Version : Queuez
therobot
March 10th, 2008, 06:26 AM
So I've started to make a game in flash (As3 this time). It's tile based.
I want to implement a queue of interactions. I'm just looking for some advice on the best methods for this. So far it works like this:
All tiles and other objects are placed, and they each contain an array of possible interactions ["Walk To", "Smite", "Nevermind, etc]. When you click on any tile or object, a menu pops up with the different choices available for that particular object (targetedObject), based on that array of actions. The user chooses an action, the action gets pumped into a queue. What I'm looking for here is some suggestions for framing the queue up.
One of my better ideas was to create a new object:
var sanitizedObject = new Object();
sanitizedObject.object = targetedObject;
//whereas x is determined by which menu button was clicked on.
sanitizedObject.action = targetedObject.actions[x];
From there, the sanitizedObject gets added to the queue. Whenever it was brought to the front of the queue, I'd have a function interpret what to do based on the object and the action pulled from the array. Thoughts? I'm getting better with OOP, but I feel like some of this stuff is still trial by fire for me.
Let me know if this doesn't make sense.
therobot
March 10th, 2008, 04:30 PM
Anyonez? I want make th3 guy shoot and come closer to the hero but how
ArmoredSandwich
March 10th, 2008, 04:40 PM
TBH, I have no idea what you are trying to do.
Can't you just do something as easy as
guy.do(guy.currentAction[0]);
if(guy.currentAction[0].done) {
guy.currentAction.splice(0, 1); }
Or.. something like that.
therobot
March 10th, 2008, 04:54 PM
TBH, I have no idea what you are trying to do.
Can't you just do something as easy as
guy.do(guy.currentAction[0]);
if(guy.currentAction[0].done) {
guy.currentAction.splice(0, 1); }
Or.. something like that.
right, that's more or less what will happen, but what i was asking was, in relation to your code, what is currentAction[0]? I had my "sanitizedObject" because when the character would get to the next action to perform in his queue, he'd need to know a few things: what object/tile he's interacting with (xtile, ytile, etc), and the specific action he'd be doing with it (say, a water tile he could walk to, swim in, fish, etc). I think i've got the right idea here, I guess I was looking for a second opinion.
ArmoredSandwich
March 10th, 2008, 05:17 PM
right, that's more or less what will happen, but what i was asking was, in relation to your code, what is currentAction[0]? I had my "sanitizedObject" because when the character would get to the next action to perform in his queue, he'd need to know a few things: what object/tile he's interacting with (xtile, ytile, etc), and the specific action he'd be doing with it (say, a water tile he could walk to, swim in, fish, etc). I think i've got the right idea here, I guess I was looking for a second opinion.
You can push anything into an array, that would include objects with whatever properties and methods it has. It's probably the best way to accomplish what you want.
therobot
March 11th, 2008, 12:01 AM
You can push anything into an array, that would include objects with whatever properties and methods it has. It's probably the best way to accomplish what you want.
Right, I know that. But I eventually plan on having multiple characters interacting with the same objects and tiles simultaneously, so I think it could get pretty messy. it seems a little roundabout to put the selected objects and tiles into a new object, but I think it might be a little more organized in the long run.
But yeah, the new objects i'm creating are what will be pushed into my queue array.
var actionObject:Object = new Object();
actionObject.objectReferenced = targetedObject;
actionObject.action = "Walk To";
queueArray.push(actionObject);
Makes more sense to me tonight than it did this morning.
Charleh
March 11th, 2008, 07:00 AM
I use a message queuing system for my game, all that I do is this:
I have a MessageQueue class with 2 arrays in it running on an engine loop
1 - For holding the messages/actions which are to be processed and
2 - For holding things which were added during this engine loop which will get processed next engine loop
I have an interface called EventConsumer, which all objects which are to be handling messages need to implement. It simply contains a method which receives the event type and event arguments as parameters
When a message needs to be added to the queue a method gets called on the MessageQueue object passing in the arguments as an instance of a custom type which has the event name, event target and the event arguments. The event arguments can be anything really but I tend to use a custom type or an array. The new message gets pushed onto the temp queue.
The engine will parse the active queue first then move any messages it has in the temp queue to the active queue. This ensures that events don't 'jump the queue' and sorts out some syncronisation errors I had with messages which I was using to set up the levels.
When the queue gets processed the events are checked from earliest to latest and when handled the event is spliced from the queue. The event engine dispatches the message to the target - or if the target is NULL it dispatches the message to all objects. There are some special messages which there is more logic for here, but not important.
The objects implement a listener function which intercepts these messages and handles the event based on the message type/content
It's quite simple but it can be used to do level setup, schedule timed events, broadcast messages to objects, target specific instances, etc etc.
For instance, in a game say I wanted all the bad guys to die I would just push a 'KillEnemies' message onto the stack and give it an event target of NULL which in my engine targets all objects on the stage. Any that receive this message consume it and act accordingly - which means that the buildings (which can all be destroyed) won't get wrecked, but all the bad guys running about will.
You should be able to get relatively simultaneous interaction in your game using that method, but bear in mind, it depends on how complicated you want the reactions - code has an order of events
Imagine the scenario that two players bullets touch a bad guy at exactly the same moment - who gets the points? The computer will give the points to whichever bullet it runs code for first. If that's an acceptable scenario then just use this simple method.
ArmoredSandwich
March 11th, 2008, 08:52 AM
I have nothing more to say..
Or do I? Because I wonder if this is what he's looking for 8)
Charleh
March 11th, 2008, 09:57 AM
Lol, well it does sound like hes looking for something with a little more of a delay like a turn based thingy, but the queue can still serve the same purpose, it just doesn't need to be processed every single frame, just at the times when the 'turn' ends.
:P
therobot
March 11th, 2008, 02:45 PM
charleh - your system seems a little more sophisticated than what i need right now, though i can forsee problems down the line with the way I'm currently doing things, especially since you raised the point of two bullets hitting the same target at the same time. That is interesting, and I figured I'd need to address that at some point, so I may as well get cracking.
My system is currently real-time, not turn-based. Each character has their own queue of actions. AI characters probably wouldn't add much to the queue, maybe an action at a time. The purpose of the queue is really so that the user can stack on a bunch of duties for the character to do, and then focus on something else while the character runs on auto-pilot. Synchronization might be an issue. The game is real-time, but not so time-heavy as a FPS or a Fighting game. We'll see. I figured every object would have some Boolean isBusy, which would probably make things just "work".
Thanks for the input charleh.
Charleh
March 12th, 2008, 05:34 AM
Ah I see, so you want a queue per character - well it's a little different in implementation but the same concept. You probably want to create a base class which the characters will extend or an interface which implements the queue.
The queue would be an array holding the list of character actions probably as strings or event identifiers. Once the game engine loops, if the character is not currently performing an action and is idle they would pick the next action from the queue and go to perform it. Can you explain to me the problem behind 2 characters going to use the same object on the same frame? The key thing to remember is that everything in code has an order of execution, so one of the characters will in reality begin to use the item first. If that means that you have to block out the other character from using it and they have to wait isn't that good enough for you or do you need something more complex?
therobot
March 12th, 2008, 05:47 AM
isn't that good enough for you or do you need something more complex?
That is what I already set up, so hopefully things should work. I don't think it should matter if one character uses an object before another. They can wait. I think it'd end up being more realistic: deciding you need something (from your queue), walking over to the desired object (from your queue), checking if the object isBusy or inUse or whatever, getting frustrated (object removed from queue), and finally choosing to do the next thing on the queue (or wait, if it's urgent).
Yes, I think this should work.
Charleh
March 13th, 2008, 10:17 AM
Yes you could bump objects to the back of the queue or back one place if the object is in use and then when they come back to it they can try again. If there's only 1 thing in the queue have them sit there waiting :P
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.