[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Simple AI Scripting, Help Me!
- From: Jared Krinke <furiousj@...>
- Date: Sun, 07 Dec 2003 10:22:06 -0800
I'm not sure how helpful this will be, but here goes:
I'm currently using Lua as an embedded scripting language in a game
engine I'm working on. I've exposed the various attributes of the actors
("entities" if you like that term better) via Lua.
My current plan (which is working out nicely) is to expose that
functionality for those that needed and then I write Lua functions to
further wrap up functions when possible (I guess they'd be equivalent to
your "enemyclose," etc., though my game is a top-scrolling shooter).
Additionally, all of the actor attributes (x, y, velocity, acceleration,
angle, etc.) are stored in a C structure (created as a Lua userdata).
This way access from C is still very fast (since the triangle
hit-detection is what currently uses the most CPU time).
Lastly, I've setup my engine so that it automatically loads all Lua
files in a directory. This will allow users to add new weapons and
enemies if they want to give it a shot. Sure they could cheat, but I
want the game to be fun, not hard, so if you wanna cheat, that's just
fine.
I guess I rambled a bit more than necessary. Hopefully some of that was
helpful.
On Sun, 2003-12-07 at 00:46, Tim wrote:
> Thanks for you help, I really appreciate it, my only question now is I need
> to call a function everyframe to update a dudes position or state (every
> frame or every 5 or whatever). Not sure how I would do this with this
> example below. How would it tie in with my player class etc. I don't want
> to really expose a whole players class (from lua), nothing if I can help it.
> I want the scripts
> to be really high level, so anyone could write scripts for it. I don't want
> to write the scripts myself, I want to set them up, and hand them off to
> some else who doesn't have
> much knowledge of programming (if need be). So I don't really want it to
> look like c++, or c (lua does resemble it), just they way one would think
> enemyclose(), runforcover(), seekhealth(), findescort(), etc etc, and I
> expose those functions to lua. If I load this main.lua it will parse the
> whole thing, and stall my app, I call it from my initfunction of my game,
> where is before, I called a function to update a guy from c++, everyframe,
> and returned true when it finished the function .. his goals.
>
>
> Thanks,
>
> Tim Ryness
>
> ----- Original Message -----
> From: "Kevin Baca" <lualist@aidiastudios.com>
> To: "'Lua list'" <lua@bazar2.conectiva.com.br>
> Sent: Saturday, December 06, 2003 3:59 PM
> Subject: RE: Simple AI Scripting, Help Me!
>
>
> > 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
> >
>