[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Re[2]: why no "continue" statement for loops?
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 24 Jun 2003 08:53:44 -0300
> With multi-threaded scripting languages, especially those for games,
> it is necessary to implement state machines, or more simply put, areas
> of code you can *jump* to, to get a character to go into a particular
> state.
Lua 5 has proper tail calls, which are a form of goto between functions.
If you make each state into a function, you can jump from one to another
with proper tail calls:
function check ()
nearest_player = check_for_nearest_player();
if nearest_player then
return attack() -- jump_to_state
end
return idle() -- go throu ;)
end
function idle ()
play( "scratch_arse" );
wait( 5 )
play( "look_around_aimlessly" );
return check() -- jump_to_state
end
function attack ()
play( "monster_roar" );
while ( nearest_player == check_for_nearest_player() ) do
if ( turn_to_player() ) then
play( "chase_1_step" )
end
wait( 1 ) // wait 1 frame (or nth of a second)
end
return check() -- jump_to_state
end
-- Roberto