LUA doesn't return changes of object values

BlitzMax Forums/BlitzMax Beginners Area/LUA doesn't return changes of object values

Firstdeathmaker(Posted 2009) [#1]
Hi there,

Im just starting to learn how lua works with BMax, but now I encountered a question. I would like create for my ki lua-scripts to move and steer them. So I created a type (TEnemy) which is running a simple lua script.

As super object I set the object itself, and in the script I want to "move" that object over the screen.

But if I run that code, in BMax nothing happens. Just in the script the values are changed.

How can I make that by lua-script changed var's are also changed in BMax?

BMax Code
superstrict

'
Type TEnemy
	Field name:String = "Angry Enemy"
	Field luaclass:TLuaClass
	Field luaobject:TLuaObject
	Field x:float
	Field y:float
	
	Method setScript(scriptpath:String)
		Local f:TStream = ReadFile(scriptpath)
		Local source:String = f.ReadString(f.size() )
		CloseFile f
		Self.luaclass = TLuaClass.Create(source)
		Self.luaobject = TLuaObject.Create(luaclass , Self)
		Assert luaobject,"TEnemy.setScript() luaobject not created"
	End Method
	
	Method process()
		luaobject.Invoke "process_ki" , Null
		render()
	End method
	
	Method render()
		DrawRect Self.x,Self.y,10,10
	End method
End Type


Graphics 400,300
Local e:TEnemy = New TEnemy
e.setScript("script1.lua")

repeat
cls
	e.process()
flip
Until KeyHit(KEY_ESCAPE)
end



LUA script1.lua
function process_ki()
	self.x = self.x + 1
	self.y = self.y + 1
	print(self.x)
	if ( self.x>=400 ) then self.x = 0 end
	if ( self.y>=300 ) then self.y = 0 end
	print(self.name)
	print("process ki")
end



Scaremonger(Posted 2009) [#2]
Hiya,
You need to register the object "e" with lua like this:
LuaRegisterObject e,"enemy"

And then update your lua script to refer to the object 'enemy' rather than 'Self'. I've also reduced the amount of code in setscript by using loadstring()

luascript1.bmx


luascrip1.lua



Firstdeathmaker(Posted 2009) [#3]
Yea, that works. But that means also, that I have to register the object every loop? Or do I missunderstand how I should use Lua?


'superstrict

'
Type TEnemy
	Global list:TList = New TList
	
	Method New()
		TEnemy.list.addlast(Self)
	End method
	
	Function Update()
		For Local e:TEnemy = EachIn list
			e.process()
		next
	End function
	
	Field name:String = "Angry Enemy"
	Field luaclass:TLuaClass
	Field luaobject:TLuaObject
	Field x:Float
	Field y:Float
	
	Method setScript(scriptpath:String)
		Local f:TStream = ReadFile(scriptpath)
		Local source:String = f.ReadString(f.size() )
		CloseFile f
		Self.luaclass = TLuaClass.Create(source)
		Self.luaobject = TLuaObject.Create(luaclass , Self)
		Assert luaobject,"TEnemy.setScript() luaobject not created"
	End Method
	
	Method process()
		LuaRegisterObject(Self , "enemy")
		luaobject.Invoke "process_ki" , Null
		render()
	End Method
	
	Method render()
		DrawRect Self.x,Self.y,10,10
	End Method
End Type


Graphics 800 , 600

For Local i:Int = 0 Until 100
Local e:TEnemy = New TEnemy
e.setScript("script1.lua")
e.x = Rand(0 , GraphicsWidth())
e.y = Rand(0,GraphicsHeight())
next


Repeat
Cls
	TEnemy.Update()
Flip
Until KeyHit(KEY_ESCAPE)
End


function process_ki()
	enemy.x = enemy.x + 1
	enemy.y = enemy.y + 1
	if ( enemy.x>=800 ) then enemy.x = 0 end
	if ( enemy.y>=600 ) then enemy.y = 0 end
end



dan_upright(Posted 2009) [#4]
you only need to register the object once