This isn't really related to any particular flavour of Blitz, or indeed to Blitz at all, so I posted it here. Tis a bit of a long question, so please bear with me if you're up on Lua.
I decided it would make sense to have all my game entities scriptable from Lua. That doesn't mean that all entities will be scripted form Lua, but they all can. Potentially that's a lot of Lua function calls every frame, so I want to make sure I'm doing it the fastest possible way. I suspect I'm not, which is why I'm asking for tips on making it faster.
Ok, here's the basics. I load all the files with the lua functions in them when the game begins.
In my main loop I have something like this ( pseudo code ) :
Then on the Lua side, I grab that Object handle and look it up in a global EntityTable to see if it has been sent before. If it has not, I create a new table containing all the functions which correspond to methods of my entity and store it in the global EntityTable with the type handle as the key. If it is already in the EntityTable, I just grab the table of functions I created for it, and use that. The table of functions also, of course, has a field called _Peer which holds the Blitz Object handle.
Now I have a Lua "object" and I can use all the "methods" of my Blitz entity. But every single frame, for every single entity which has a script ( which may be few or quite considerable ) I have to get the global function and put it on the stack, push the object ( entity ) to the stack, call the lua function, use the object handle to either create a new table of functions or look up another in a global table, before I can do anything with the object. Quite frankly, I have no idea how fast this is. At the moment, with one entity being scripted my FPS hold steady on 996 which is what it is before I attach a script to the entity, but I have no idea if it's going to get painful further down the line.
Is this the fastest way of doing this, or is it possible to cut my speed overhead here? I can't see any possibility to use UpValues here, because the number of entities I might be dealing with is unknown and changing. But then, I'm a bit of a Lua virgin, so I'm kinda hacking it together as I go. It works, but could/should it work faster?
I decided it would make sense to have all my game entities scriptable from Lua. That doesn't mean that all entities will be scripted form Lua, but they all can. Potentially that's a lot of Lua function calls every frame, so I want to make sure I'm doing it the fastest possible way. I suspect I'm not, which is why I'm asking for tips on making it faster.
Ok, here's the basics. I load all the files with the lua functions in them when the game begins.
In my main loop I have something like this ( pseudo code ) :
For Each Entity { If Entity.HasAScript(){ Lua_GetGlobal(FunctionName) Lua_PushLightUserData(BytePtr(HandleFromObject(Entity)) Lua_PCall(LuaVM,1,0,0) ' 1 PARAMETER, 0 RETURNS } }
Then on the Lua side, I grab that Object handle and look it up in a global EntityTable to see if it has been sent before. If it has not, I create a new table containing all the functions which correspond to methods of my entity and store it in the global EntityTable with the type handle as the key. If it is already in the EntityTable, I just grab the table of functions I created for it, and use that. The table of functions also, of course, has a field called _Peer which holds the Blitz Object handle.
Now I have a Lua "object" and I can use all the "methods" of my Blitz entity. But every single frame, for every single entity which has a script ( which may be few or quite considerable ) I have to get the global function and put it on the stack, push the object ( entity ) to the stack, call the lua function, use the object handle to either create a new table of functions or look up another in a global table, before I can do anything with the object. Quite frankly, I have no idea how fast this is. At the moment, with one entity being scripted my FPS hold steady on 996 which is what it is before I attach a script to the entity, but I have no idea if it's going to get painful further down the line.
Is this the fastest way of doing this, or is it possible to cut my speed overhead here? I can't see any possibility to use UpValues here, because the number of entities I might be dealing with is unknown and changing. But then, I'm a bit of a Lua virgin, so I'm kinda hacking it together as I go. It works, but could/should it work faster?