lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This is an ideal application for coroutines. Basically, rather than the bot AI being called every frame, it is a function running in a thread that is resumed every frame. Here's how my engine does it (more or less):

-- starts a new thread running a "move helper" function, and
-- returns a table with a boolean member called "complete" that is
-- switched from false to true just before the new thread completes
local action = MOVE(WP5)

-- yields continuously until the action is complete (this assumes a
-- round-robin scheduler for all threads)
while not action.complete
  yield()
end

action = MOVE(WP6)
while not action.complete
  yield()
end

I use helper functions for the most common cases, so it looks more like this:

waitfor(MOVE(WP5))
waitfor(MOVE(WP6))

Coroutines are extremely easy now that threads are first-class objects. Read all about them in the manual.

Ben

----- Original Message -----
From: sohyl siddiqui <sohyl_s@yahoo.com>
Date: Friday, May 16, 2003 1:14 pm
Subject: Resuming execution of a LUA script???

> hello ppl,
> i am working on a 3D game engine and i wanted to add scripting support to 
> it. i have chosen LUA as the scripting language and have done some basic 
> work.( still i am a comlepte noob in terms of LUA .....)
> wat i basically want to do is like this :
> for example a patrol script .... that wud enable an NPC to patrol between 
> waypoint 5 and 6.
> 
> MOVE(WP5)
> IDLE()
> MOVE(WP6)
> IDLE()
> i know the basics like exporting functions to LUA and stuff the problem 
> that i am facing is that for that above script to work i need to stop 
> further execution of the script once the move function is called ...... 
> and resume execution (once the bot has moved to the given location .... ) 
> from the next statement i.e. IDLE()
> well one of the reasons that i want this to happen this way is that the 
> script is called(executed) at every frame to determine the bots behaviour.
> anybody has any ideas on this one ..... is there sumthin that i am doing 
> wrong???urgent help is needed.
> sorry for my poor english but i hope that i have expressed myself clearly 
> .... any help will be greatly appreciated.
> sohyl.
> 
> 
> ---------------------------------
> Do you Yahoo!?
> The New Yahoo! Search - Faster. Easier. Bingo.