lua - cant get it to work

BlitzMax Forums/BlitzMax Beginners Area/lua - cant get it to work

QuietBloke(Posted 2006) [#1]
OK.. I have tried various examples I found found within the forums.

with this code :


Local luaVM:Byte Ptr = lua_open( )

luaopen_base(luaVM)
luaopen_table(luaVM)
luaopen_io(luaVM)
luaopen_string(luaVM)
luaopen_math(luaVM)

lua_register(LuaVM,"AddMan",AddMan)

lua_dofile(luaVM,"MyScript3.lua".ToCString())

Function AddMan(luaVM:Byte Ptr)

Print String.FromCString(lua_tostring(luaVM,-1))
lua_pushnumber(luaVM,0)
Return 1

EndFunction

lua_close(luaVM)


I get Compile Error : Unable to convert from 'Byte Ptr' to CString

on the line

lua_dofile(luaVM,"MyScript3.lua".ToCString())



with this code

Global scriptEnv:ScriptEngine=New ScriptEngine
scriptEnv.reset()

Function LUA_MyFunction( luaState:Byte Ptr )
' Let's return a string
scriptEnv.ReturnStringToLua(luaState, "Hello, World!")
Return 1 ' Returning one variable
End Function

scriptEnv.addFunction(LUA_MyFunction,"MyFunction")

scriptEnv.RunScriptFile("MyScript.lua")

ScriptEnv.Shutdown()


It compiles but then when I run it I get

Unhandled Exception: Unhandled Memory Exception Error

on the

scriptEnv.RunScriptFile("MyScript.lua")

line

So... No one else is getting these problems so what the heck have I got wrong ?

I have the latest verison of BlitzMax which I uninstalled / deleted the BlitzMax folder and reinstalled. I synchronised Modules.

If someone has any hints as to what might be causing my problem I would be very grateful.

Thanx


QuietBloke(Posted 2006) [#2]
Well a complete fresh install of XP with just BlitzMax installed didnt help.
I had minor sucess using an older version of BlitzMax but as no one has responded guess I'll have to forget about BlitzMax for messing around with lua and install C++.


Luke.H(Posted 2006) [#3]
I don't know if you have fixed it yet, but taking out:

.ToCString()

(and)

.FromCString

Makes it work? I don't why, I found the same code on the forums too.

Try this:



I like the second bit of code but I cannot get it to work too


Scott Shaver(Posted 2006) [#4]
The lua stuff changed recently so you don't have to use the ToCString FromCString call all over the place. So many examples may not have been updated to reflect this change.


QuietBloke(Posted 2006) [#5]
Thanx for the Replies. The code you gave works Luke :). Of course now I have lua working with C++ I cant decide which language to use !

Still very odd how the ScriptEngine code doesnt work. Is it a known bug ?.. should I put an entry in the BlitzMax bugs forum ?


Luke.H(Posted 2006) [#6]
I can get ScriptEngine to work now
but I get Unhandled Exception if anything to wrong with my script (no "performs syntax checks")

can you post your script file

and also I think scripts are case sensitive.

This works:



MyScript.lua (case sensitive I think):




QuietBloke(Posted 2006) [#7]
lol.. absolutely right !... when I checked back on the script I had Print instead of print.

A helpful error message would have saved a lot of time and frustration.
Thanx for the help.


Luke.H(Posted 2006) [#8]
Yes I agree,

Look at a doc command


Method: SetScriptText:Int(scriptText:String, scriptName:String)
Description: Set engine script text (performs syntax checks)



But I see no syntax checks.

If you are making a game or program were you are loading lots of scripts you could do a Try and Catch block but you still get the same error, no helpful error message or line number (Just stop it crashing):



Global scriptEnv:ScriptEngine=New ScriptEngine
scriptEnv.reset()

Function LUA_MyFunction( luaState:Byte Ptr )
' Let's return a string
scriptEnv.ReturnStringToLua(luaState, "Hello, World!")
Return 1 ' Returning one variable
End Function

scriptEnv.addFunction(LUA_MyFunction,"MyFunction")


Try
	scriptEnv.RunScriptFile("MyScript.lua")
Catch ex:TBlitzException
	Print "Script error:"
	Print ex.ToString()
EndTry




ScriptEnv.Shutdown()





Luke.H(Posted 2006) [#9]
I got some code to work, 200 balls bounce around the screen,

I cannot work out the comments in lua, those anyone know?



Test.lua



test.bmx





Scott Shaver(Posted 2006) [#10]
single line comment

--ans=QMMsgBox("Yes/No","This is a Yes/No box with ASK icon.",MSGBOX_YES_NO,MSGBOX_ICON_ASK)

multiline comment
--[[ What kind of icon in
a message box.]]


CoderLaureate(Posted 2006) [#11]
Check this one out:

http://www.codersworkshop.com/viewshowcase.php?id=714


QuietBloke(Posted 2006) [#12]
Plenty of example code to look at there. Thanx

Still there is the problem of any coding error in the script will cause an Unhandled memory exception.

guess Ill just have to place a try block around it.

Thanx for all the help.