What would your solution be?
Personally, I see nothing wrong with this, aside from a bit of unnecessary code. E.g., in SetKey, all you'd need is this:
function object:SetKey(key,value)
if key=="resolution" then
local value = tonumber(value)
if 0 <= power and power <= 3 then object.light:SetShadowmapSize(256 * 2^power) end
else
return self.super:SetKey(key,value)
end
end
This probably comes down to coding style, however, but I like having fewer lines. Far as I'm concerned, nested functions in Lua are fine. You wouldn't have to put everything in Create, for one - you could store it in a local and reference that in the function, like so:
require "scripts/class"
do
local function table_merge(table, into)
for k,v in next, table, nil do
into[k]=v
end
end
-- object methods
local object_methods = {}
function object_methods:SetKey(key,value)
if key=="resolution" then
local power = tonumber(value)
if 0 <= power and power <= 3 then object.light:SetShadowmapSize(256 * 2^power) end
else
return self.super:SetKey(key,value)
end
end
function object_methods:GetKey(key,value)
if key=="linearoffset" then
return object.light:GetShadowOffset(0,0)..","..object.light:GetShadowOffset(0,1)..","..object.light:GetShadowOffset(0,2)
elseif key=="shadowdistance" then
return object.light:GetShadowDistance(0)..","..object.light:GetShadowDistance(1)..","..object.light:GetShadowDistance(2)
else
return self.super:GetKey(key,value)
end
end
function object_methods:Kill(model)
if self.light then
self.light:Free()
self.light = nil
end
self.super:Kill()
end
-- class methods
local class_methods = {}
function class_methods:InitDialog(grid)
self.super:InitDialog(grid)
group=grid:AddGroup("Light")
group:AddProperty("Resolution",PROPERTY_CHOICE,"256,512,1024,2048")
group:AddProperty("linearoffset",PROPERTY_VEC3,"0,1,2","Linear offset" )
group:AddProperty("shadowdistance",PROPERTY_VEC3,"","Shadow distance" )
group:AddProperty("Range",PROPERTY_FLOAT)
group:Expand(1)
end
function class_methods:Spawn(model)
local object=self.super:Spawn(model)
object.model:SetKey("resolution","2")
object.light=CreateDirectionalLight(object.model)
table_merge(object_methods, object)
return object
end
-- light_directional_CreateClass
function light_directional_CreateClass(modelreference)
local class=CreateClass(modelreference)
table_merge(class_methods, class)
return class
end
end