In-game scripting and what should be done

BlitzMax Forums/BlitzMax Programming/In-game scripting and what should be done

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Have a look at LUA. It's fantastic. As soon as you've got a groundwork to build on you can do just about anything. There is one problem though. It's extremely hard to debug in bmx at the moment (at least for me anyway).

You bind your bmx functions to LUA so it can use them. You can also go the other way. For simplish things like the ones you've outlined above, LUA would be perfect fo you.

http://www.blitzbasic.com/Community/posts.php?topic=52524

Noel's the bloke to talk to about this.

Here's a quick example:
 ' ******************************* LUA CODE BELOW

' LUA doesn't have data types as such.

Rem

-- these are global to this scriptEngine

a_variable = 2
something = "hello there"
windPower = 0.4474



function update()
	local name = getPlayerName()
	
	print("We are updating - "..name)
end

EndRem


 ' ******************************* LUA CODE ABOVE



' example:

Strict

Global windSpeed#=0.004
Global playerName$="jeff"


Local a:LUAScript=New LUAScript
	a.init("example.lua")
	
	While KeyHit(KEY_ESCAPE)=0
		a.update()
	Wend
	
	a=Null
	
	End



 ' ******************************* BMX CODE BELOW

Type LUAScript
	Field se:ScriptEngine
	Field file$
	
	Method init(_file$)
		se=New ScriptEngine
		se.reset()
		file=_file		
		' run the main script
		If (Not se.RunScriptFile(file)) Then
			Print "Error: " + se.GetLastErrorString()
		EndIf		
	
		' now add our functions we want LUA to have access to.
		'				our function			LUA uses		params?		returns
		se.AddFunction(LUA_setWindLevel, 	"setWindLevel") '	[ wind# ]
		se.AddFunction(LUA_getWindLevel, 	"getWindLevel")	'				 returns #
		
		se.AddFunction(LUA_getplayerName, 	"getplayerName")'				Returns $
		se.AddFunction(LUA_setPlayerName, 	"setPlayerName")'	[ name$ ]
	End Method
	
	Method update()
		' we can call LUA methods, like if you have an update function in LUA
		se.BeginLUAFunctionCall()
		se.CallFunction("update", True) ' true = we expect a return at some point from LUA
	End Method
	
	Method Delete()
		se.shutDown() ' shutdown LUA
	End Method
	
End Type

' params are put on a stack, so if we call the function setPlayerName("jeff","blah")
' 													then jeff is -2 and blah is -1
Function LUA_setPlayerName(ls:Byte Ptr)
	Local something$ = String.FromCString(luaL_checkstring(ls, -1))
	Local name$ =String.FromCString(luaL_checkstring(ls, -2))	
	playerName = name
	Return 0 ' we're not returning anything (this is important)
End Function

' an example of getting something from your game and putting it back into LUA
Function LUA_getWindLevel(ls:Byte Ptr)	
	lua_pushnumber(ls,windSpeed) ' send it back to LUA	
	Return 1 ' yes, we return something, a number.
End Function

' an example of sending a string back
Function LUA_getplayerName(ls:Byte Ptr)	
	lua_pushstring(ls, playerName.ToCString()) ' send it back to LUA	
	Return 1 ' yes, we return something, a string.
End Function

Function LUA_setWindLevel(ls:Byte Ptr)
	Local newWind# = luaL_checknumber(ls, -1)	
	windSpeed = newWind
	Return 0 ' we're not returning anything (this is important)
End Function