This time saving function will loop a certain animation ( currently idle,die,hit,move,attack ) for any monster, and can also check if the animation is already playing, or check which animation is playing.
The animation rule it follows is idle, die, hit, move, attack. (ie the monsters are appended in that order)
sync on: sync rate 100
`I use this system for my monsters, and the ani() function is easily changed to suit your system
type monster
ani_die as integer
ani_hit as integer
ani_move as integer
ani_attack as integer
model as integer
endtype
`just two monsters for this example
dim mon(2) as monster
mon(1).model = 1
mon(2).model = 2
`******Necessary variables******
global idle as integer : idle = 1
global die as integer : die = 2
global hit as integer : hit = 3
global move as integer : move = 4
global attack as integer : attack = 5
global playing as integer : playing = 1
global play as integer : play = 2
`****** End Necessary variables******
load object "res/models/enemies/alien hivebrain/H-Alien Hivebrain-Idle.x",mon(1).model
mon(1).ani_die = total object frames(mon(1).model)+1
append object "res/models/enemies/alien hivebrain/H-Alien Hivebrain-Die.x",mon(1).model,mon(1).ani_die
mon(1).ani_hit = total object frames(mon(1).model)+1
append object "res/models/enemies/alien hivebrain/H-Alien Hivebrain-Impact.x",mon(1).model,mon(1).ani_hit
mon(1).ani_move = total object frames(mon(1).model)+1
append object "res/models/enemies/alien hivebrain/H-Alien Hivebrain-Move.x",mon(1).model,mon(1).ani_move
mon(1).ani_attack = total object frames(mon(1).model)+1
append object "res/models/enemies/alien hivebrain/H-Alien Hivebrain-Attack1.x",mon(1).model,mon(1).ani_attack
load object "res/models/enemies/android/L-Android-Idle.x",mon(2).model
mon(2).ani_die = total object frames(mon(2).model)+1
append object "res/models/enemies/android/L-Android-Die.x",mon(2).model,mon(2).ani_die
mon(2).ani_hit = total object frames(mon(2).model)+1
append object "res/models/enemies/android/H-Android-Impact.x",mon(2).model,mon(2).ani_hit
mon(2).ani_move = total object frames(mon(2).model)+1
append object "res/models/enemies/android/L-Android-Move.x",mon(2).model,mon(2).ani_move
mon(2).ani_attack = total object frames(mon(2).model)+1
append object "res/models/enemies/android/L-Android-Attack1.x",mon(2).model,mon(2).ani_attack
spd = 20 `ideal speed in my opinion
position object mon(1).model,2,-2,0
position object mon(2).model,-2,-2,0
do
`just for convenience
control camera using arrowkeys 0,0.2,1
`i find having two sets of IF:THEN statements alot neater than having IF:ENDIF ones.
if inkey$() = "1" then ani(1,idle,play)
if inkey$() = "2" then ani(1,die,play)
if inkey$() = "3" then ani(1,hit,play)
if inkey$() = "4" then ani(1,move,play)
if inkey$() = "5" then ani(1,attack,play)
if inkey$() = "1" then ani(2,idle,play)
if inkey$() = "2" then ani(2,die,play)
if inkey$() = "3" then ani(2,hit,play)
if inkey$() = "4" then ani(2,move,play)
if inkey$() = "5" then ani(2,attack,play)
`text to show you an aspect of the function
text 0,0,"--alien hivebrain--"
text 0,20,"die animation playing: "+str$(ani(1,die,playing))
text 0,40,"rule: idle,die,hit,move,attack"
text 0,60,"current animation playing: "+str$(ani(1,0,playing))
sync
loop
`rule: idle,die,hit,move,attack
function ani(mon,ani,mode)
animation = 0
if mode = play
if ani = idle
start = 1
finish = mon(mon).ani_die-1
endif
if ani = die
start = mon(mon).ani_die
finish = mon(mon).ani_hit-1
endif
if ani = hit
start = mon(mon).ani_hit
finish = mon(mon).ani_move-1
endif
if ani = move
start = mon(mon).ani_move + 5
finish = mon(mon).ani_attack-1
endif
if ani = attack
start = mon(mon).ani_attack
finish = total object frames(mon(mon).model)
endif
loop object mon(mon).model,start,finish
endif
if mode = playing
if object frame(mon(mon).model) < mon(mon).ani_die then animation = idle
if object frame(mon(mon).model) > mon(mon).ani_die-1 and object frame(mon(mon).model) < mon(mon).ani_hit then animation = die
if object frame(mon(mon).model) > mon(mon).ani_hit-1 and object frame(mon(mon).model) < mon(mon).ani_move then animation = hit
if object frame(mon(mon).model) > mon(mon).ani_move-1 and object frame(mon(mon).model) < mon(mon).ani_attack then animation = move
if object frame(mon(mon).model) > mon(mon).ani_attack-1 then animation = attack
if ani>0
if ani = animation
animation = 1
else
animation = 0
endif
endif
endif
endfunction animation
How to use it
-ani(monster, animation, play/playing)
-to loop animation, ani(1,idle,play) < will loop the monster's idle animation
-to check if a certain animation is playing, ani(1,die,playing) < will return 1 if playing and 0 if playing.
- to find out what animation is playing, ani(1,0,playing) < will return what animation is playing
Notes
I have discovered that the DarkMatter models' (which i use for this example) movement animation does not loop properly because of an extra 5 frames at the beginning of the animation. if you look down through the ani() function, you will see that i have +5 to the looping of the movement animation.
As you can see, the function is very specified by my mon(monster).types monster system.
Feel free to change this if it does not suit you.
Give me a holler in the credits if you found this function useful!