Lua?

BlitzMax Forums/BlitzMax Programming/Lua?

MattVonFat(Posted 2005) [#1]
Can anyone explain how you use this in BlitzMAX because it seems quite complicated.


N(Posted 2005) [#2]
Go to its site and download the manual.


MattVonFat(Posted 2005) [#3]
Ok i will have a look at that thanks.


N(Posted 2005) [#4]
For the record, it operates the exact same way as it would in C, so the examples in the manual will work, albeit there will be syntactical differences.

For a basic example..
Local state:Byte Ptr = lua_open()

Function lPrint:Int(ls:Byte Ptr)
     For Local i:Int = 1 To lua_gettop(ls)
          Local lType:Int = lua_type(ls,i)
          Select lType
               Case LUA_TNUMBER
                    Print lua_tonumber(ls,i)
               Case LUA_TSTRING
                    Print String.FromCString(lua_tostring(ls,i))
               Case LUA_TBOOLEAN
                    Print lua_toboolean(ls,i)
               Default
                    Print "Can't print format "+lType
          End Select
     Next
     Return 0
End Function

lua_register(state,"lPrint",lPrint)

lua_dofile(state,"myscript.lua".ToCString())

lua_close(state)

Input() ' So you can see it before the console closes


And for myscript.lua..
-- myscript.lua -- no, you don't need to put a comment at the top of the file

lPrint("Weebl!",2.5,false,true,"Har dee har har!",500)



MattVonFat(Posted 2005) [#5]
Thanks but im still having a few problems.

I have this as my code:

Local state:Byte Ptr = lua_open()

lua_dofile(state,"Script.lua".ToCString())

lua_close(state)

Input()


With this as the script file:
print ("hello")


Now i was reading about LUA on the manual and a tutorial. The tutorial said that print("hello") should work. I read the manual and then checked out the mod and found that it does include the C header files. Now i'm not an an expert on C so i don't know whether there is something i'm mising out or whether i cant use those commands and i have to create them like you did in your example. But if the latter is the case then why use LUA when i could just make up my own sort of script?


N(Posted 2005) [#6]
Because writing your own scripting language is difficult (I've done it, it's really interesting so I'd say definitely try it once at least, but it's hard as hell to get something working well), and since Lua provides a lot of features that a basic (I mean feature-wise, not syntax) scripting language would not offer (tables, for one -- they're extremely useful). Also, if you want, you could actually write a little game-making tool that uses Lua for scripting and then write your own little level editor and such and then never have to redistribute the source, but only binaries (I've thought about this ;) ).

In any case, if you don't want to use Lua or you can't think of a good reason to use it, chances are you don't need it. I'm not using it for my game because I don't need it to be modifiable (yet, if the time comes that I need it then I'll add it).


MattVonFat(Posted 2005) [#7]
yeah but my problem is that i cant get the commands that work in Lua to work - such as print.


N(Posted 2005) [#8]
Well the problem with print not working in yours is you didn't open the base library.

Do:
luaopen_base(state)


After opening your Lua state.


MattVonFat(Posted 2005) [#9]
Oh right thanks. I was tring it without the (state) bit. Thankyou very much.


Mirko(Posted 2005) [#10]
The explanations above got me started with lua. My little testprogram now has a few functions which can be called by a lua script. Everything works nice.

But how can my function return an int or string to lua?

return (myvalue)

at the end of my function does not seem to work.


N(Posted 2005) [#11]
Function ReturnString(ls:Byte Ptr)
     lua_pushstring(ls,"Foobar".ToCString())
     Return 1
End Function


You push the return values onto the function's stack (ls in the example) then return the amount of returned values. It pops the returned amount of values after pushing them onto the stack that preceeded the function's stack, if I recall correctly.