Need Help with Lua
BlitzMax Forums/BlitzMax Programming/Need Help with Lua
| ||
--Lua_Spell.lua Spell = {} setmetatable(Spell, {__mode="v"}) function Create_Spell(name,class,area,target,damage,cost) local i = { __Name = name, _Class = class, __Area = area, Target = target, Damage = damage, __Cost = cost, }, table.insert(Spell,i) return i end local i = Create_Spell("FIRE I","FIRE",1,"SINGLE",10,10) local i = Create_Spell("FIRE II","FIRE",1,"SINGLE",20,25) local i = Create_Spell("FIRE III","FIRE",1,"SINGLE",40,60) local i = Create_Spell("FIRE IV","FIRE",1,"SINGLE",80,130) local i = Create_Spell("FIRE V","FIRE",1,"SINGLE",160,270) function FindSpell_name(name) for x,v in pairs(Spell) do for y,z in pairs(x) do if z==name then return(y) end end end end function FindSpell_class(class) for x,v in pairs(Spell) do for y,z in pairs(x) do if z==class then return(y) end end end end function DumpStat(i) for x,v in pairs(i) do PrintPairs(x,v) end end What a am trying to do in bmax is request the pointer to the spell then in bmax call the function Dumpstat passing the pointer back to Lua Function lPrintPairs( L:Byte Ptr ) Local keys:Int = lua_gettop(L) For i = 1 To keys Step 2 Local Word$ If lua_isnumber(L,i) = True Word$:+lua_tointeger (L,i) Else Word$:+String(lua_tostring (L,i)) EndIf Word$:+":" If lua_isnumber(L,i+1) = True Word$:+lua_tointeger (L,i+1) Else Word$:+String(lua_tostring (L,i+1)) EndIf Print Word$ Next Return 0 End Function but i dont know how to add arguments to function calls from bmax or how to store the pointer in bmax allowing me to instant access in Lua without having to keep searching |
| ||
Unrelated to the actual issue can you declare i as local 5 times in a row in Lua?local i = Create_Spell("FIRE I","FIRE",1,"SINGLE",10,10) local i = Create_Spell("FIRE II","FIRE",1,"SINGLE",20,25) local i = Create_Spell("FIRE III","FIRE",1,"SINGLE",40,60) local i = Create_Spell("FIRE IV","FIRE",1,"SINGLE",80,130) local i = Create_Spell("FIRE V","FIRE",1,"SINGLE",160,270) |
| ||
yes i see no problem with local so far |
| ||
Weird, most languages would give you issue with multiple scope declarations like that at the same level |
| ||
I'm not entirely sure what you're asking, what this "pointer" is you're talking about, etc. As far as passing arguments to Lua functions, please read the manual. Unrelated to the actual issue can you declare i as local 5 times in a row in Lua? You can, but I don't know why you would when you could just reuse the variable. |
| ||
what im trying to do is create a search function in lua to find a spell by name. Then store this spells index and pass it to bmax so from bmax i can call the function DumpStat(i) for x,v in pairs(i) do PrintPairs(x,v) end end without having to search for the spell again |
| ||
So you want to store the result of one of the Find functions, then later pass it back to DumpStat? If so, just store the return value of the function after calling it in BlitzMax (hopefully you clicked the link to the manual I gave you, since it explained how to call functions and such). If that's not what you want to do, do you have any examples of how you'd expect to be doing this (forget about trying to make it look like a real application, pseudo code is easier to illustrate a problem with). Also, what's with the arbitrary number of underscores in the table keys, and the somewhat absurd way you've written the find functions? I mean, as it stands, I don't think you're going to get any results from the find functions. At worst, they're liable to cause an error when calling them. |
| ||
--Lua_Spell.lua Spell = {} setmetatable(Spell, {__mode="v"}) function Create_Spell(name,class,area,target,damage,cost) spells = { Name = name, Class = class, Area = area, Target = target, Damage = damage, Cost = cost, }, table.insert(Spell,spells) end Create_Spell("FIRE I","FIRE",1,"SINGLE",10,10) Create_Spell("FIRE II","FIRE",1,"SINGLE",20,25) Create_Spell("FIRE III","FIRE",1,"SINGLE",40,60) Create_Spell("FIRE IV","FIRE",1,"SINGLE",80,130) Create_Spell("FIRE V","FIRE",1,"SINGLE",160,270) function FindSpellname(name) local k=nil local v=nil for k,v in pairs(Spell) do if v.Name==name then DumpPairs(v) end end end function DumpPairs(temp) local k=nil local v=nil for k,v in pairs(temp) do PrintPairs(k,v) end end FindSpellname("FIRE IV") FindSpellname("FIRE I") FindSpellname("FIRE V") 'My_Lua_Engine.bmx Global lua_state:Byte Ptr = Null OnEnd(KillLuaState) lua_state = luaL_newstate() ' Load base, math, table, and os libraries luaopen_base(lua_state) luaopen_math(lua_state) luaopen_table(lua_state) luaopen_os(lua_state) lua_register( lua_state, "Print", lPrint ) lua_register( lua_state, "PrintPairs", lPrintPairs ) StartTheApp() Function KillLuaState() If lua_state Then lua_close(lua_state) lua_state = Null EndIf End Function Function StartTheApp() LoadScript("Lua_Spell.lua") End Function Function LoadScript(_String$) Local error:String If luaL_dofile( lua_state, _String$ ) Then error:String = lua_tostring( lua_state, -1 ) lua_pop( lua_state, 1 ) RuntimeError(error) EndIf End Function Function lPrint( L:Byte Ptr ) Local keys:Int = lua_gettop(L) For i = 1 To keys If lua_isnumber(L,i) = True Print lua_tointeger (L,i) Else Print String(lua_tostring (L,i)) EndIf Next Return 0 End Function Function lPrintPairs( L:Byte Ptr ) Local keys:Int = lua_gettop(L) For i = 1 To keys Step 2 Local Word$ If lua_isnumber(L,i) = True Word$:+lua_tointeger (L,i) Else Word$:+String(lua_tostring (L,i)) EndIf Word$:+":" If lua_isnumber(L,i+1) = True Word$:+lua_tointeger (L,i+1) Else Word$:+String(lua_tostring (L,i+1)) EndIf Print Word$ Next Return 0 End Function Ive managed to get the search function sorted but now there is a issue when trying to get the last item added to the table ie "FIRE V" any idea whats up with that. please post a Bmax function calling FindSpellname(name) and Passing the String "FIRE III" :) |
| ||
Function lua_dumpStat(_name$) lua_getglobal( lua_state, "FindSpellname" ) If lua_type( lua_state, -1 ) = LUA_TFUNCTION Then lua_pushstring ( lua_state, _name$ ) If lua_pcall( lua_state, 1, 0, 0 ) Then Local error:String = lua_tostring( lua_state, -1 ) lua_pop( lua_state, 1 ) RuntimeError(error) EndIf Else lua_pop( lua_state, 1 ) EndIf EndFunction well thats that sorted now |
| ||
by changing the spellcreation part and adding in a blank spell at the end allows me to get "FIRE V" so somethings up with the TableCreate_Spell("FIRE I","FIRE",1,"SINGLE",10,10) Create_Spell("FIRE II","FIRE",1,"SINGLE",20,25) Create_Spell("FIRE III","FIRE",1,"SINGLE",40,60) Create_Spell("FIRE IV","FIRE",1,"SINGLE",80,130) Create_Spell("FIRE V","FIRE",1,"SINGLE",160,270) Create_Spell() |
| ||
I'm not sure what you mean. All you're doing there is adding an empty table ({}) to the Spell table when you call it without providing any parameters. Either way, why are you even excluding the parameters? |
| ||
The problem is when i do this lua functionCreate_Spell("FIRE I","FIRE",1,"SINGLE",10,10) Create_Spell("FIRE II","FIRE",1,"SINGLE",20,25) Create_Spell("FIRE III","FIRE",1,"SINGLE",40,60) Create_Spell("FIRE IV","FIRE",1,"SINGLE",80,130) Create_Spell("FIRE V","FIRE",1,"SINGLE",160,270) function PrintallSpells() local k=nil local v=nil for k,v in pairs(Spell) do DumpPairs(v) end end end I only get Spells FIRE I to FIRE IV returned but if create another spell then try the PrintallSpells function it now prints from FIRE I to FIRE V so for some reason the function is not getting the last pair any ideas how to fix this ? or is the a better approach to looping over the Spell table ? |
| ||
Your problem is the comma you have after your } in the create spell function. Not sure why that's there, but it shouldn't be. |
| ||
\o/ thanks its fixed |