Lua Binding (Entities)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Lua Binding (Entities)

Postby lelandbdean on Tue Jul 05, 2016 11:52 pm

Ok, I need advice. After looking at several scripting languages for my C99 game dev project, I settled on Lua due to its popularity and simplicity.
I have since spent the better part of a day searching Google and this forum for information, and have read quite a bit
of Lua's user wiki as well.

If I had the cash right now I'd just buy the damn book. :oops:
In the meantime, I've been doing a lot of silly tests to get to know the language.

I find it odd that Lua has no notion of namespaces/packages/etc, but its tables are pretty sweet.
Binding global functions is easy peasy, especially since most engine calls can be handled with built in data types as arguments.

My problem is that I'm not sure how to handle entity scripts (and similar things) intelligently. It annoys me that I can't isolate local
"instances" of Lua state without making multiple VMs (hell nah).

My latest test is my attempt to be OO-ish about it: All scripts inherit from a Lua base class "Script," which looks like this:

Code: Select all
ScriptTable = {};

Script = {
  id = -1,
  class  = nil
};

function ClearScripts()
  ScriptTable = {};
end

function Script:New(O)
  O = O or {}
  setmetatable(O, self)
  self.__index = self;
  if O.id ~= -1 and O.class ~= nil then
    ScriptTable[O.class][O.id] = O;
  end
  return O;
end

function Script:Delete()
  if self.id ~= -1 and self.class ~= nil then
    ScriptTable[self.class][self.id] = nil;
  end
end

function Script:OnAwake() end
function Script:OnDie() end

function Script:OnEnable() end
function Script:OnDisable() end
function Script:OnUpdate(Delta) end
function Script:OnDraw(Delta) end
function Script:OnGui(Delta) end

function Script:OnMouseEnter() end
function Script:OnMouseLeave() end
function Script:OnMouseDown() end
function Script:OnMouseUp() end
function Script:OnFocusGained() end
function Script:OnFocusLost() end

function Script:OnMessage(Message) end

function Script:OnEnterRegion(Region) end
function Script:OnLeaveRegion(Region) end

function Script:OnInteract(Other) end

function Script:OnPreCollision(Other) end
function Script:OnCollision(Other) end
function Script:OnPostCollision(Other) end


My reasoning is that this will allow my scripts to tack custom attributes onto entities
for the lifetime of a loaded scene, and all manipulation done on engine entities can be handled
with global function calls (abusing Script.id).

All entities in my project are in a statically allocated list (10,000 entities = ~5mb or something on the heap)
stored in the loaded scene, with their GUIDs mapping directly to their position in that list. Because of this,
I assume accessing an entity from Lua using wrapped C functions (Entity.Kill(id) or something) shouldn't be too expensive.

Anyway, I'm a complete Lua virgin so any advice/pointing out of glaring problems would be greatly appreciated.

Thanks!

EDIT: Noticed a bug in my code.
lelandbdean
Chaos Rift Newbie
Chaos Rift Newbie
 
Posts: 19
Joined: Thu Mar 28, 2013 11:37 pm

Return to Programming Discussion

Who is online

Users browsing this forum: Bing [Bot] and 4 guests

cron