December 04, 2020, 11:51:21 AM

Author Topic: [bmx] Lua For-Each Function by Galaxy613 [ 1+ years ago ]  (Read 443 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Lua For-Each Function by Galaxy613 [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Lua For-Each Function
Author : Galaxy613
Posted : 1+ years ago

Description : I did a lot of hunting down, at first I thought it was too crazy to work, but it does work.

I wanted a easy way to go through a TList object inside of Lua. So I came up with the idea of just passing the TList object, and passing a anonymous function to do for each member. The function will only receive one argument right now, and that's the object. I initially tried having LuGI take care of the BMX function, but it turned out way easier to just register it manually. What I did with the temporary function was put it in a temporary register which each object of the loop can run, then at the end it releases the register.

Please share any improvements you make!

Here's an example of how to use it in Lua:

Code: [Select]
ForEach(CurrentSystem.PlanetList,
function (planet)
DrawText(planet.Name.." : "..planet.oAngle, SCREENW/2-128, 0+(16*Planet_Count))
Planet_Count=Planet_Count+1
end )

-- Or even clearer:

ForEach(ListObject,
function (ListMember)
--Do operations  here
end )


Code :
Code: BlitzMax
  1. ''' Put somewhere after you start your LuaState
  2. lua_register( lua_state, "ForEach", ForEach )
  3.  
  4. ''' The actual function
  5.  
  6. Function ForEach:Int( lua_vm:Byte Ptr )
  7.  
  8.         Local _arg_1:TList = Null
  9.        
  10.         If lua_gettop(lua_vm) > 1
  11.                 _arg_1 = TList(lua_tobmaxobject(lua_vm, 1))
  12.                
  13.                 If lua_isfunction(lua_vm,2)
  14.                         Local tmpFunc% = luaL_ref(lua_vm, LUA_REGISTRYINDEX)
  15.                                
  16.                         For Local tmp:Object = EachIn _arg_1
  17.                                 'call luaFunc
  18.                                 lua_rawgeti(lua_vm,LUA_REGISTRYINDEX,tmpFunc)
  19.                                 lua_pushbmaxobject(lua_vm,tmp)
  20.                                 If lua_pcall( lua_vm, 1, 1, 0 ) Then
  21.                                                 scriptError = lua_tostring( lua_vm, -1 )
  22.                                                 lua_pop( lua_vm, 1 )
  23.                                                 Notify("ForEach :: "+scriptError)
  24.                                                 scriptIsGood = False
  25.                                 'Else
  26.                                         ' Get the result
  27.                                         'local result% = lua_tointeger( luaState, -1 )
  28.                                 EndIf
  29.                                 lua_pop( lua_vm, 1 )
  30.                         Next
  31.                        
  32.                         luaL_unref(lua_vm,LUA_REGISTRYINDEX,tmpFunc)
  33.                 EndIf
  34.         End If
  35.  
  36.         lua_pushinteger( lua_vm, 0 )
  37.  
  38.         Return 1
  39.  
  40. End Function


Comments :


Jim Teeuwen(Posted 1+ years ago)

 By request: <a href="codearcs8dd7.html?code=2544" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=2544[/url]


Galaxy613(Posted 1+ years ago)

 Haha, second time I submit something and someone else did it better and proper. xD Well, I guess mine is at least a working case study on how to send a lua function to a BlitzMax function and execute it multiple times...


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal