Archived

This topic is now archived and is closed to further replies.

Getting multiple units to work at the same time

This topic is 5235 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 working on an RTS game. On each pass of the game loop, I use a for loop to scroll thtough all of my objects and act upon their current states. However, I''ve realized that in say pathfinding, if a unit is to be moved to a new location, I need to calculate that unit''s path before I can do any other action on any other unit. Isn''t there a better way to do this? I have a state machine set up within my unit classes, but I don''t see how that would work when scrolling through the game loop.

Share this post


Link to post
Share on other sites
I''m also workin on an rts and I in fact have that figured out In exchange, can you tell me how you set up your map with regard to treating the problem of variable unit size?

Share this post


Link to post
Share on other sites
SFPiano: you could have the pathfinding run in a separate thread, but that leads to synchronization issues. Perhaps better is to have anything that needs AI to add itself to a list, and then, after the main update loop, have an AI loop that runs through that list and does all of your calculations. This would let you do some funky stuff, like assigning different AI tasks different priorities (so, combat AI beats out pathfinding AI, etc.), and making your AI loop only process what it can in a given amount of time. Of course, there are other solutions (and an entire AI forum...)

Cyril: the simple solution is to figure out the smallest common denominator to your unit sizes (in Warcraft, the foot soldiers & peasants could be viewed as 1x1, while catapults, farms, etc. were 2x2, and some buildings were larger--but always incremented by the size of a peasant...), and then break your map up into cells the size of that smallest unit. Of course, this requires that there actually be a smallest unit that you can quantize down to...

-Odd the Hermit

Share this post


Link to post
Share on other sites
One really good idea that I read in that recent featured article detailing how to use C scripts in your game was to implement a command in your script which basically tells the interperter: I am done for now, but let me finish when you have some time/next game loop/in x miliseconds. This would not be very hard to accomplish even without scripting just by breaking each long function into sub functions. Say you have to call a function GetPath(). Well, instead, call GetPath0().. then in your next game loop cycle, call GetPath1(), etc... or better yet:
void Unit::GetPath()
{
static iterationnum = 0;
switch(iterationnum)
{
case 0:
// do first stuff here

break;
case 1:
// do the rest here

iterationnum = 0;
break;
}
}

this of corse could be much larger than 2 switch cases if the function takes longer. This method is much easier to implement when bieng done through a script, but if this is not a choise than you might try this method, although it does impose some limitations. You can not call the same function multipule times per loop and expect it to start a new loop of itself. I guess to get around this you could pass a parameter telling it weather or not it should start a new loop or continue a previous one, ie. loop number... example:

int loopnum1 = GetPath(0); // 0 indicates that a new one should be started
int loopnum2 = GetPath(0);
GetPath(loopnum1);
int loopnum3 = GetPath(0);
GetPath(loopnum2);

etc

not sure how this well this will work as I have never seen an implementation, but its an idea

Dwiel


For all in need of free hosting for pics/etc:
upload to ftp location: dwiel.no-ip.com user/pass = FreeHostedPics/gamedev
Your files will be available on the inet immediately at http://dwiel.no-ip.com/~FreeHostedPics/
Feel Free to create a folder there for your self!

Share this post


Link to post
Share on other sites
I don''t really understand much of that. The way I have it set up now, I have a state machine for each object. The problem is that the only way I can think of iteration through all the objects is with a for loop.

if(rMouseDown){
for(i=0; iobj.moveTo();
}



"Half of the world is full of idiots. The other half is filled with people smart enough to make use of them.

Share this post


Link to post
Share on other sites
ok, sorry... I am not very good at explaining things.

Plus I somewhat misunderstod your question

Why don''t call the pathfinding function as soon as you find out that it needs to be done. ie. as soon as the command is given... it should be calculated outside of the FSM loop.

I think this is more of an answer to the question you asked... could you refrase your question differently? Sorry for my lack of confusion!

Dwiel

For all in need of free hosting for pics/etc:
upload to ftp location: dwiel.no-ip.com user/pass = FreeHostedPics/gamedev
Your files will be available on the inet immediately at http://dwiel.no-ip.com/~FreeHostedPics/
Feel Free to create a folder there for your self!

Share this post


Link to post
Share on other sites
I would suggest not having pathfinding in the for loop. When a unit decides what to do (move, attack, whatever), have it make a path immediately. If pathing fails, get it to pick a different thing to do. EG: In the for loop, unit 1 is current being checked. Unit 1 decides to attack Unit 143. Checks path, can''t find one to Unit 143, so decides to pick state defend base. Next iterration of for loop to Unit 2. Etc.

That way, if a unit wants to move and fails, it won''t wait till next turn to try something else.

Also, if you put a minimum limit to the path, then it doesn''t need to be checked every cycle. EG: You can setup so if there''s less than 5 elements in a units path, it checks it. And if you put an upper limit of 10 elements in the path, then the unit will create shorter paths, (with the same dest in mind remember), more often, with a probable decrease in CPU useage. This''ll also help the AI adapt the path to changing environments.

Share this post


Link to post
Share on other sites
But I believe the problem is having the for loop in the first place. Start the loop, go to unit 1, it''s supposed to be moving somewhere so find the path and tell it to move, then go to unit 2. The problem is that while unit 1 is doing something, units 2+ are just sitting waiting.

Share this post


Link to post
Share on other sites
it all depends on how long it takes to run through the task loop. it should hopefully complete the loop issuing task to every unit then move on to the next part of your program. Afterall your not doing any animation during the caluation phase. If you move a unit it the task loop should update the units current postion. When the task loop is done your draw method will simply draw the unit in its new position.

-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document

Share this post


Link to post
Share on other sites