How to loop a procedure

Hi. I am planning on making a turn based strategy game. The main game loop is like this: player move–>player attack–>enemy pathfinding–>enemy move–>enemy attack. How do I set up this kind of loop?

Thanks.

Won’t functions with if statements and calling other functions work?
Or panda3d.org/manual/index.php/F … e_Machines

You might also want to dig into task chains.

I’d just keep it simple, and make a method for each of the tasks you listed. Call each in turn inside a loop that checks for game-over conditions.

There are definitely more elaborate solutions that may scale better, but you haven’t mentioned any factors that whould make them better for your simple situation.

Finally, you are looking at the problem from one direction, and seeing some small tasks that make up the bigger task (the game). It is possible that there is a better vantage point. For instance, if those tasks ever change order, then you should consider the game a finite state machine instead so you can model things correctly.

Good luck!

EDIT: Lest anyone have trouble thinking of a scenario in which the tasks might need reordering, perhaps a user can earn a double-turn (or lose a turn).

How about something like this. It is based on the idea of FSM.

state=1
class World(DirectObject.DriectObject):

def __init__(self):
    self.accept("playermove",self.playermove)
    self.accept("aimove",self.aimove)    
    
def mainloop(self,task):
    global state
    if state==0:
        return task.cont
    if state==1:
        messenger.send("playermove")
        return task.cont
    if state==2:
        messenger.send("aimove")
        return task.cont

def playermove(self):
    global state
    state=0
    #select unit,change unit position,play animation,etc
    state=2

def aimove(self):
    global state
    state=0
    #ai movement,ai attack,etc
    state=1

myworld=World()

taskMgr.add(mainloop,‘mainloop’)

run()

On second thought, all I’m going to say is “looks plausible, good luck”.

My first thought:

Yup, that’s close to how I’d start, although at this point I’d want to stop coding and think about how well it will work once it gets more complicated. One improvement: there is no need to set state = 0 at the beginning of each helper function because you are only using one thread and your mainloop will never get too far ahead. Another: the states could be useful strings instead of ingeters (e.g. ‘state_calc_enemy_move’, ‘state_enemy_move’, etc.).

Since the mainloop function will be run once per frame, it will just keep calling the other functions without the “0 state”. For example, when I am using a Lerp Intervals to move a unit, the unit will just stay in the starting position.

My bad.