Getting my head around Lua

BlitzMax Forums/BlitzMax Programming/Getting my head around Lua

Robb(Posted 2011) [#1]
I have been working on a 2D game engine for a while and I now want to implement Lua scripting with the LuGi module. So far, I've managed to expose various types and can create new objects with lua scripts but I need a little help taking it further.

The engine is currently based around game states (let's say TengineState) and each TengineState contains an init, update, draw & destroy function. Rather than hardcode these states for each new object, what I would like to do is defer each TengineState object's functions to a lua script.

So for example, inside menustate.lua I could script a menu-specific update function, that would be called every time the engine ran its own update function.

Could anyone offer any help on how would I go about doing this?
I have trawled through the LuGi LuaInvaders example and I've looked at the scriptable type in the code archives but I'm not really grasping how I would achieve the results I'm after.


Pete Carter(Posted 2011) [#2]
This would interest me too.


Galaxy613(Posted 2011) [#3]
You can quickly call a LUA function from code using:

lua_getfield(LuaState, LUA_GLOBALSINDEX, "GlobalFunctionName")
lua_call(LuaState, 0, 0)

The only problem is this method can't access functions within tables. I got more code once I get home. I created an advanced hooking solution that allows you to call functions that are attached to hooks.

Last edited 2011


Robb(Posted 2011) [#4]
Thanks for the response. That'd be great if you could post any extra code.

The idea of objects existing in the Lua state but outside of the the main program (i.e. in a lua table, rather than in a Tlist) confuses me. As does the way objects are created in Lua without calling the blitzmax type's create() function.

I guess my main issue with integrating lua is figuring out what side of the app should be handling certain things i.e. should each entity be created and and stored in the Lua state, or should it still be held in a global entity Tlist within blitzmax...urg...my brain hurts.


AnniXa(Posted 2012) [#5]
Hey, a small question in here about lugi and lua.
I dont wanted to start an extra topic for this.

I was able to become lugi and lua running and i can execute scipts now.

But when i have the following lua src:

function startevent(event)
  print ("starting event")
  event:say()
end



how can i then run this code from bmx out and pass over a event type, i tried it with this:

(glue code and stuff is all generated)
type event {expose}

   field text:string

   method say()
       print ("event says:"+self.text)
   end method
end type

type luafuncs {expose static noclass}
   method getnextevent()
      local ev:event = new event
      ev.text = " its me!"
      return ev
   end method
end type

'call the lua func "startevent"

local str = "startevent(getnextevent())"
luaL_dostring(luastate,str)


then the lua function "startevent" is running fine, but event is allways nil, so its not passed over correctly :(

what am i making wrong here?
or is there another easyer way to call functions in lua and give them values/objects?

Last edited 2012


N(Posted 2012) [#6]
.

Last edited 2012


N(Posted 2012) [#7]
.

Last edited 2012