lua-users home
lua-l archive

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


Hi,
 
I'm trying to execute a function made from loadstring, but I want the function to remain part of the class that I'm executing it from.
 
Here's some code:
 

Program = [[if self.ScanTheArea == true then

if ScanArea() > 0 then

FireRocketAtClosest()

else

self.ScanTheArea = false;

end

end]]

self.f = loadstring(Program)

self:f()

-- The self doesn;t seem to make this part of class I'm executing it in.
-- Th error I Get is
-- 15:36:22: LUA: [string "..."]:1: attempt to index global `self' (a nil value)
-- which is this line "if self.ScanTheArea == true then" - essentially it can;t find "self" in a LoadString'ed function 
 
 
 
I have  base class simpletank:
 

SimpleTank= {}

function SimpleTank:new()

local Tab = {

m_MeshName = "SimpleTank.mesh",

m_Life = 450,

m_MaxLife = 450,

m_Node = 0,

m_ClientObjectId = 0,

m_ScanAngle = 0,

m_StateChangeTimer = 5,

m_StateChangeInterval = 3,

m_State = 0,

m_Resolution = 10,

m_Param1 = nil,

m_Param2 = nil,

m_Param3 = nil,

m_Param4 = nil,

m_Param5 = nil

}

setmetatable(Tab, self)

self.__index = self

return Tab

end

 

 

-- and I have a class derived from it

 

CreatorBot = SimpleTank:new()

 

function CreatorBot:Updater(dt)

Program = [[if self.ScanTheArea == true then

if ScanArea() > 0 then

FireRocketAtClosest()

else

self.ScanTheArea = false;

end

end]]

self.f = loadstring(Program)

self:f()

end

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.13.13/198 - Release Date: 12/12/2005