Jump to content

How to run a lua code string from blitzmax ?


eleXity
 Share

Recommended Posts

Hi,

I want to make some item scripts in XML like this

 

<class name="item">
<item> 
	<!-- Function block-->
		<function name="OnPick">
			local itemClass = {}

			itemClass.light = CreateSpotLight()
			itemClass.light:Hide()

		</function>

		<function name="OnUseSwitch">
			if (bItem_Used=true) then
				itemClass.light:Show()
			else
				itemClass.light:Hide()
			end
		</function>

		<function name="OnUpdate">
			itemClass.light:SetMatrix(this.tItem_PositionPivot.mat)
		</function>

</item>
</class>

 

I load every function block into an ScriptFunction type in blitz max and they all have one new instance of a TLuaState

so that I can run it with DoString(), but with the new tLuaState the lugi functions are not registered, how should i do that ?

hope you understand my problem

 

PS.: sorry for my bad English B)

Windows 10 | Intel i5 - 3570K | Nvidia GTX 960 | 16GiB RAM | LE 3 | BlitzMax | C# | Java | German

 

76561197992185761.png

Link to comment
Share on other sites

mhh but this is not what i expect,

I not want to do something with the entity-lua scripts, I only want that for the entity´s and nothing more B)

the item scripts should be separate...

 

I have another question, is it possible to create a lua class from blitzmax and/or invoke functions from within blitzmax

like this :

function classname:Bla()

?

Windows 10 | Intel i5 - 3570K | Nvidia GTX 960 | 16GiB RAM | LE 3 | BlitzMax | C# | Java | German

 

76561197992185761.png

Link to comment
Share on other sites

I think the answer stays the same, you can do any Lua commands and class definitons by loading a model in LE. As long there is no function like RunScript() in LE, there's no other way, unless BlitzMax has some undocumented hack again, which doesn't work with the DLL version.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Q#1: Why you don't write string to a file and then call with RunScript?

Q#2: Why you have inserted XML between Lua script and BMX? Why you don't write your class Item like lua class?

 


TItemClass = {}


function TItemClass:New()

   o = {
		bItem_Used = false
	}

setmetatable(o, self)
   self.__index = self

   return o

end

function TItemClass:OnPick()

self.light = CreateSpotLight()
self.light:Hide()
end

function TItemClass:OnUseSwitch()

if (self.bItem_Used==true) then
	self.light:Show()
else
	self.light:Hide()
end
end

function TItemClass:OnUpdate()
-- ...
end


[HW] C2D Q6600, 4GB RAM, NV8800GTX, Vista Ultimate x64

[sW] Blide Plus, BlitzMax, Delphi, C++, 3DWS 5.53, Leadwerks 2.xx

 

76561197970156808.pngAndyGFX.png

Link to comment
Share on other sites

mhh but this is not what i expect,

I not want to do something with the entity-lua scripts, I only want that for the entity´s and nothing more :)

the item scripts should be separate...

 

I have another question, is it possible to create a lua class from blitzmax and/or invoke functions from within blitzmax

like this :

function classname:Bla()

?

 

When you want call existing function in lua script you need add to BMX code this part:

 

C sample:

 

http://www.lua.org/manual/2.1/subsection3_7_6.html

 

DELPHI sample:

 


// call LUA function 
function ExecuteFunction(functionname: pchar): pchar;

begin

   if (enabled = false) then exit;

   lua_getglobal(VM, functionname);
   lua_call(VM, 0, 1);
   result := lua_tostring(VM, 1);

   //result:=0;

end;

// call LUA function with int arg
function ExecuteFunction(functionname: pchar;val:integer): pchar;
var state:integer;
begin
   lua_getglobal(VM, functionname);
	state := lua_type(VM,-1);
	lua_pushnumber(VM, val);
	if (state=LUA_TFUNCTION)  then lua_call(VM, 1, 0);
end;

 

And at end, my TIP:

 

- add PUB.LUA to your BMX source

- then you have this very useful functions added:

 

Function lua_dobuffer:Int (lua_state:Byte Ptr, buff:String, sz:Int, name:String)
Function lua_dofile:Int (lua_state:Byte Ptr, filename:String)
Function lua_dostring:Int (lua_state:Byte Ptr, str:String)

 

and many others.

[HW] C2D Q6600, 4GB RAM, NV8800GTX, Vista Ultimate x64

[sW] Blide Plus, BlitzMax, Delphi, C++, 3DWS 5.53, Leadwerks 2.xx

 

76561197970156808.pngAndyGFX.png

Link to comment
Share on other sites

ok thanks for your tips :P

 

I will try the lua version of it, but i don´t understand how it works..

lets say I run your lua script with RunScript(), how can I then call the function

TItemClass:New()

from blitzmax to create the new class, or

the OnUpdate() function ?

 

or must I only call it like this :

lua_getglobal(VM, "TItemClass:New");
lua_call(VM, 0, 1);

 

Ps.: sorry for the noob questions but I never worked with lua before LE

Windows 10 | Intel i5 - 3570K | Nvidia GTX 960 | 16GiB RAM | LE 3 | BlitzMax | C# | Java | German

 

76561197992185761.png

Link to comment
Share on other sites

I think that for you is now best way, learn LUA with LE Scene editor.

You need know, how lua works, then how it works in LE Scene and after this goals to learning, you are partly ready to use lua in BMX or other language.

[HW] C2D Q6600, 4GB RAM, NV8800GTX, Vista Ultimate x64

[sW] Blide Plus, BlitzMax, Delphi, C++, 3DWS 5.53, Leadwerks 2.xx

 

76561197970156808.pngAndyGFX.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...