lua-users home
lua-l archive

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



It sounds like all you want to do is index a table. Here is an example in Lua, but your classes could be implemented in C if you wish.


local function Class(methods)
 methods = methods or {}
 function methods:new(obj)
   return setmetatable(obj or {}, {__index = methods})
 end
 return methods
end

local function load(self, filename)
 print(self.name, 'load', filename, self)
end

local mesh = Class{ name = 'mesh', load = load }
local sound = Class{ name = 'sound', load = load }
local texture = Class{ name = 'texture', load = load }

cs = {
 mesh = {},
 sound = {},
 texture = {},
}

cs.texture.t1 = texture:new{ t = 1 }
cs.mesh.m1    = mesh:new{ m = 1 }
cs.mesh.m2    = mesh:new{ m = 2 }
cs.sound.s1   = sound:new{ s = 1 }
cs.sound.s2   = sound:new{ s = 2 }
cs.sound.s3   = sound:new{ s = 3 }

cc = cs.texture.t1 print('cc',cc) cc:load'one'
cc = cs.mesh.m2    print('cc',cc) cc:load'two'
cc = cs.sound.s3   print('cc',cc) cc:load'three'
cc = cs.sound.s1   print('cc',cc) cc:load'four'

$ lua x.lua
cc      table: 0x10045a98
texture load    one     table: 0x10045a98
cc      table: 0x10045c18
mesh    load    two     table: 0x10045c18
cc      table: 0x10047668
sound   load    three   table: 0x10047668
cc      table: 0x100473e0
sound   load    four    table: 0x100473e0

- Peter Shook

Moritz Mühlenthaler wrote:
Hi everyone,
im rather new to lua and i have some problems binding it to my cpp code. The
main problem is that all my classes are loaded dynamically, so i cant
hardcode the binding to a lua table. I could register each class' methods as soon
as it's loaded, but i'd prefer to set some kind of hook in the metatable,
which takes care of calling the right functions; I hope i can make it a little
clearer by getting more specific.
Lets say i have a "content server" which holds a hirarchy of classes
representing the content of a little game (texture classes, mesh classes, sounds, ect). This "content server" has something like a "current content" to save some
typing (i.e. cs.meshes.player01.load("mymesh.xyz") = cs.load("mymesh.xyz")
if cs.meshes.player01 is the "current content"). So the problem is that i cant hardcode the functions in the "cs" table, because the current content can be
a mesh, a texture, a camera or whatever... I've read about the metatables,
and the "__call" hook, but i somehow cant override it properly. I'd be glad if
someone could give me a hint how to set a cfunction as a "__call" hook from
c in the metatable. Perhaps you even have a completely different idea..
Thx for your time.

JiM.

_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*. http://join.msn.com/?page=features/featuredemail