[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Simple AI Scripting, Help Me!
- From: "Kevin Baca" <lualist@...>
- Date: Sat, 6 Dec 2003 15:59:47 -0800
There are many ways to do this, and the "file == class" technique (where
a file containing methods represents a class) is not necessarily the
easiest or most straightforward.
The most straightforward, IMO, is described here:
http://lua-users.org/wiki/ObjectOrientationTutorial
If you want to use the "file == class" technique, look at loadfile():
http://www.lua.org/manual/5.0/manual.html#5.1
A simple example:
BadGuy.lua
----------
local BadGuy = {}
function BadGuy:doSomething()
print( "Bad Guy doing something" )
end
return BadGuy
GoodGuy.lua
----------
local GoodGuy = {}
function GoodGuy:doSomething()
print( "Good Guy doing something" )
end
return GoodGuy
main.lua
--------
local BadGuy = assert( loadfile( "BadGuy.lua" ) )()
local GoodGuy = assert( loadfile( "GoodGuy.lua" ) )()
function createBadGuy()
return setmetatable( {}, { __index = BadGuy } )
end
function createGoodGuy()
return setmetatable( {}, { __index = GoodGuy } )
end
Interpreter
-----------
> dofile "main.lua"
> badguy = createBadGuy()
> goodguy = createGoodGuy()
> badguy:doSomething()
Bad Guy doing something
> goodguy:doSomething()
Good Guy doing something
>
See the following for examples on associating lua objects with C
objects:
http://lua-users.org/wiki/SimpleCppBinding
You should try to minimize the number of interfaces between lua and C.
This implies that you should minimize the number of C data structures
that have direct counterparts in lua, and vice versa.
Based on my own experience I've found that if you plan to have most of
your game logic in C, you should create your heavyweight entity
structures in C and use lua only for configuration, variable
definitions, etc.
OTOH, if you plan to have your game logic in lua then your heavyweight
entity structures should be in lua.
-Kevin
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Tim
Sent: Saturday, December 06, 2003 2:35 PM
To: lua@bazar2.conectiva.com.br
Subject: Simple AI Scripting, Help Me!
I have been playing with lua for the last little while trying to get lua
working within the game. I can run a single script via/ lua_dofile, and
register some functions. So I now I have a script such as the
following.
function playerinit()
local rvalue = 0
if ( keyhit("DIK_SPACE") > 0 ) then
drawstring(-1,30,"hit spacebar")
rvalue = 1
end
return rvalue
end
ok yeah really simple, I get this just fine, what I want to do is have
every player in the game, have it's own script like badguy uses =
badguy.lua, hero uses = hero.lua what I want to do now is have three
states for every player like init, process, exit, so I have a
playerinit(), playerproc(), playerexit() in each ai script, so when I
process my guy from my c++ code, I can say (from my main process loop
c++) registerplayer(this), it registers my luaclass with a pointer to
the active player. Then I then later call lua .. via
lua_getglobal(luaVM, "playerinit") etc, but I want each of my .lua files
to have the same function names init proccess exit. when a
state(funcion in lua) is complete it returns true, and then I change
from say init to process, and inside process would have commands like..
if(enemyisnear()) runforcover();
else patrolpath();
That would be my process function, the problem I guess I'm having, is
the whole problem with multiple lua_states, and stuff, and how to
arrange it, how to set it up, etc. My app is not multithreaded, but can
I use lua_newthread? I'm basically lost, combing through threads of
threads looking for answers so I have come to this. What it seems as
though is happening, is that when I add a new guy into the world, and
call lua_dofile(), it basically becomes the script in which all players
use, I tried something with calling lua_open and storing a state in the
player class, but that didn't seem to work to well.. Ideally I would
like to use one lua_state, and then when the lua functions are called
from c, it just works itself out, I have no idea, how to set this up.
I'm beyond frustrated, I'm a beginner user, I don't have lots of time to
spend researching this stuff, but I want to use it, it's a great
language, there must be a good and easy way to do this. I think I could
probably do it if each guy had unique function names, and somehow, store
them in the class and call them as needed, but I would rather have the
same set of functions for every actor in the world.
Any Ideas?
Thanks,
Tim Ryness