lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Greetings! I'm using Lua in a game project, so, naturally, I'm worried about
speed of execution.

The game is a sort of puzzle-action hybrid. Think of it like a Zelda clone;
although it isn't, the metaphor is suitable, I think. My Lua scripts control
both the triggers and scripting functions for the current room as well as
the scripting functions for all of the entities in the current room.

In all of these scripting methods, I access global tables quite frequently.
The tables I access are called State, Audio, Video, and Input. Since I use
them about 10 times per entity update and there may be about 50 entities in
one room being updated 50 times per second, I'm worried about the hash table
lookups causing a slight loss in efficiency.

Now, I hear that making global variables local results in a good speed
increase. Since I create the above four tables from C++ before loading any
entity scripts, I had an idea to increase efficiency. Right now, a script
file may look like this:

--
function LoadRoom000()
   Audio.PlayTrack("Track001")
   State.LoadTileMap("TMap000")
end

function UpdateRoom000()
   local k = Input.Key()
   if k ~= 0 then
      Video.DrawText(Input.KeyNames[k], 0, 0, Video.LeftAlign, 0, 128, 255)
   end
end
--

The point is, I access the tables a lot.
What if I were to prefix the script files with the following line:
   local Video, Audio, State, Input = Video, Audio, State, Input
where the four on the right refer, of course, to the global tables, which
have been declared by the time the script files are loaded.

Would this cause a significant speed increase?

I'm at an early enough stage in my game where I can't really test this for
myself. I just need to know if this will have the desired effect.

Thanks for reading,
~Nick