lua - cant get it to work

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

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

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++.

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:


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")

Function AddMan(luaVM:Byte Ptr)

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

EndFunction

lua_close(luaVM)



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

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.

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 ?

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:


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()



MyScript.lua (case sensitive I think):


print(MyFunction());



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.

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()




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



ballx=0.0
bally=0.0
ballxs=0.0
ballys=0.0

function update()
	
	
	ballys=ballys+0.04
	
	ballx=ballx+ballxs
	bally=bally+ballys
	
	
	if ballx>640 then
		if ballxs>0 then
			ballxs=-ballxs
		end
	end
	if bally>480 then
		if ballys>0 then
			ballys=-ballys
		end
	end
	if ballx<0 then
		if ballxs<0 then
			ballxs=-ballxs
		end
	end
	if bally<0 then
		if ballys<0 then
			ballys=-ballys
		end
	end
end

function getballx()
	return ballx
end

function getbally()
	return bally
end

function setball(x,y)
	ballx=x
	bally=y
end



function getballxs()
	return ballxs
end

function getballys()
	return ballys
end

function setballspeed(xs,ys)
	ballxs=xs
	ballys=ys
end




test.bmx



Strict




Type Tball
	
	Global list:Tlist=CreateList()
	Field link:TLink=ListAddLast(list,Self)
	
	
	Field script:ScriptEngine=Load()
	
	
	
	Function Load:ScriptEngine()
		Local script:ScriptEngine=New ScriptEngine
		script.reset()
		
		Try
			script.RunScriptFile("Test.lua")
		Catch ex:TBlitzException
			Print "Script error:"
			Print ex.ToString()
		EndTry
		
		Return script
	End Function
	
	Method update()
		
		script.BeginLUAFunctionCall()
		script.CallFunction("update", 1)
		
	End Method
	
	Method getballx#()
		
		script.BeginLUAFunctionCall()
		script.CallFunction("getballx", 1)
		Return script.GetResultNumber()
	
	End Method
	
	Method getbally#()
		
		script.BeginLUAFunctionCall()
		script.CallFunction("getbally", 1)
		Return script.GetResultNumber()
	
	End Method
	
	Method getballxs#()
		
		script.BeginLUAFunctionCall()
		script.CallFunction("getballxs", 1)
		Return script.GetResultNumber()
	
	End Method
	
	Method getballys#()
		
		script.BeginLUAFunctionCall()
		script.CallFunction("getballys", 1)
		Return script.GetResultNumber()
	
	End Method
	
	Method setball(x#,y#)
		script.BeginLUAFunctionCall()
		script.AddNumberParameter(x)
		script.AddNumberParameter(y)
		script.CallFunction("setball", 1)
	End Method
	
	Method setballspeed(xs#,ys#)
		script.BeginLUAFunctionCall()
		script.AddNumberParameter(xs)
		script.AddNumberParameter(ys)
		script.CallFunction("setballspeed", 1)
	End Method
	
	
	
End Type




For Local n=0 To 200
	
	
	Local ball:Tball=New Tball
	
	ball.setball(Rnd(0,640),Rnd(0,480))
	
	ball.setballspeed(Rnd(-3,3),Rnd(-3,3))
	
	
	
Next









Graphics 640,480

While Not KeyDown(KEY_ESCAPE)
	
	Cls
	
	
	For Local ball:Tball=EachIn Tball.List
		
		ball.update()
		
		DrawOval ball.getballx()-5,ball.getbally()-5,10,10
		
		
	Next
	
	
	Flip
	
Wend


For Local ball:Tball=EachIn Tball.List
	ball.Script.Shutdown()
Next






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.]]

Check this one out:

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

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.