alternatives to per-frame functions?

i noticed that in quake, the AI is written as a set of functions, with one function for each frame.
in each function, it tells the game which to call next. it looks something like:


attack1(){ modelframe =1; nextthink=attack2;}
attack2(){modelframe=2; nextthink = attack3; damage(enemy); nextthink=run1}

and so on.
if you save, say, in mid-attack, and load, the enemy will be in exactly the same position he was when you save.
however, i think that if you write the AI as one large function, when you load, the enemy will revert to idle position and have to spot you again.

is there any way to preserve the progress of an enemy when you save other than frame functions?
maybe if you serialize while the method is still executing, or will that cause an error?

or should i use an interpreted script?

Why not remember the state of each bot in the game. So if they see you and are attacking, they have some set of state for attacking, ex: (PLAYER_SEEN=TRUE, MOVING_TO_ATTACK=FINISHED, RAISING_WEAPON=TRUE, FIRED=FALSE). Of course some of this state should probably be stored in the animation state that needs to be saved, if you really want to get detailed.

They just grouped their AI and animation state together inside the function calls, but that doesn’t mean it’s the only or best way.

hmmm… how can i make this easier for modders?

maybe have a program to add the state checks where certain function calls are present and set a variable to tell where it is and if statements to skip over already visited code.

I don’t suggest to safe the actual situation. That could cause the player to run amok :wink: Depending on your game the player should restart at the last possible checkpoint. Restarting directly in the action could be pretty annoying.

Thats just my point of view. For the question, yes i think this kind of per frame function is the only way, when you don’t have something like a global tick that controls the whole gameflow. Then you could save the last tick state and start from there.

i know i can save which frame it’s on and what sequence it’s playing, but how do i save where it is in the code?

how does this global tick work?

If you serialize “enough” you should be fine ?

It does work independently on your renderer. In the unreal engine for example the whole logic is determined by the Tick. So when a pawn enters a specific state, you could save that state and even subanimation info. I think you could even save the position of the animation or recalculate it on the current tick value. Lets say the walking state was entered on Tick 5.000 and now the player likes to save his game. Current Tick is 5.025 and the walking animation takes 7 ticks to be done. That would make up 3 full walking sequences and the fourth is at 3/7 in his animation.

Sounds plausible :wink: