Confused about Lua and Bmax

BlitzMax Forums/BlitzMax Programming/Confused about Lua and Bmax

Robb(Posted 2009) [#1]
In the spirit of the recent Lua threads I decided that it was about time that I tried to learn the language and I've been experimenting with pub.lua and LuGi for a few days.

Unfortunately though, I'm thoroughly confused as to how the interaction between the max and lua works. I've read a fair amount of the Lua manual but between LuGi, pub.lua, brl.maxlua etc. it's not getting any clearer.
Are there any examples available at all?

I can successfully generate glue code and load a simple script to create an object and assign it values (using the NewDaemon lua script from the LuGi docs) but how, for example, would I be able to define the object's update method in the Lua script rather than the Bmax code?

Here is my current BMAX code:
SuperStrict
Import LuGI.Core

Type Daemon
	Field m_name$
	
	Method Init:Daemon( name$ )
		Assert name Else "Empty name"
		m_name = name
		Return Self
	End Method
	
	Method Name:String()
		Print m_name
	End Method
	
	
End Type


' Include LuGi generated glue code
Include "glue.bmx"

' Main code

Local state:Byte Ptr = luaL_newstate()

InitLuGI(state)

Print "--Running script--"
If luaL_dofile(state, "test.lua")
	Local err:String = "Error: "+lua_tostring(state, -1)
	lua_close(state)
	RuntimeError(err)
EndIf

Print "--Done--"


lua_close(state)


and the lua, test.lua
-- Default constructors are NewTypename()

local daemon = NewDaemon()


-- or, call init in the same go
 
daemon = NewDaemon():Init("Geoffrey")
daemon:Name()


Thanks for any help!


GW(Posted 2009) [#2]
you can't create native methods to host objects in a script. I think you should be able to add a new lua method to a host type. but obviously that would only work in lua.


Robb(Posted 2009) [#3]
To give an example of what I would like to do, I saw this video from the new Leadwerks lua implementation where a windmill object's rotation was updated via a script (4:20 in the video).

Granted there is probably a hell of a lot more going on in that video, but I would like to be able to update my objects in blitzmax, however define what actuallys happens to them in the lua code, i.e.

function daemon:Update()

        daemon.x = deamon.x+1

end function


or whatever.


N(Posted 2009) [#4]
If you want to attach a method to an object, you should set the BMX_TABLE_SUPPORT macro to 1 in lgcore.h. You can't override an entire type's methods (you can't access any of the methods without having an instance in the first place [this is by design- it's essentially there to prevent you from breaking something]), but it would allow you to replace instance methods and such. The host application will be able to access the overridden Update method provided you use lua_gettable to access it, and if the method is set to nil it'll resume accessing the original implementation.

Another option is to wrap your objects in a table with a metatable that contains override methods. E.g. (very, very pseudocode)
object table = {
    object = the BMax object;
    
    metatable = {
        overrides = { override methods };
        
        index=[check overrides for method,
               otherwise return object.key];
        
        newindex=[if value is function,
                  set overrides.key to value and maybe attaches a 'super' variable to the function's environment];
    }
}


As for how the Leadwerks code works, I'll let Josh explain it if he feels like it, since it'd be rude of me to do so.


Robb(Posted 2009) [#5]
Thanks for the reply!

I think I will try looking into the second method of using tables to wrap objects.

I tried using the LuGi version of the LuaInvaders source to figure out how to do what I wan but the update function in that script is not a method. There seems to be little in the way of examples or documentation on how to use Lua in Max, so I don't really know what I am doing! lol.

Hopefully Josh will read this and will be able to put me out of my misery! :-D