• Advertisement
Sign in to follow this  

LUA/luabind : attempt to index global 'self' (a nil value)

This topic is 3231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys, i'm begining LUA with (luabind for c++ bindings) and it seems i didn't understood something, because when i try to declare a class with tables inside, i have a strange error. basically, i have a class declared like:
class 'scheduler'
--
function scheduler:__init(agents)
	print("[+] new scheduler instance ...")
	self.agentsCoroutines = {}
	self.writesQueue = {}
	for i,j in pairs(agents) do
	table.insert(self.agentsCoroutines, {
										coroutine.create(agents[1]), 
										agents[2] --[[channel--]]} )
	print(self.agentsCoroutines,"status:",coroutine.status(self.agentsCoroutines[1]) )
	end
	return self
end



this works, albeit when i declare this:
function scheduler.start()
	local myThread = self.agentsCoroutines
end



i have this strange error:
Quote:
attempt to index global 'self' (a nil value)
do you know what i'm having wrong?? thank you in advance

Share this post


Link to post
Share on other sites
Advertisement
function scheduler.start() should be function scheduler:start(), with a : instead of a .

Share this post


Link to post
Share on other sites
Quote:
Original post by Giedrius
function scheduler.start() should be function scheduler:start(), with a : instead of a .


hi
i've tryed it but i still have the same problem :/

Share this post


Link to post
Share on other sites
post your class definition for the scheduler, it should be something like this:



scheduler = { } -- Create a table to hold the class methods
function scheduler:new() -- The constructor
local object = {
mMemebers=nil,
}
setmetatable(object, {
-- Overload the index event so that fields not present within the object are
-- looked up in the prototype parent class table
__index = scheduler
})
return object
end

function scheduler:start()
local myThread = self.agentsCoroutines
end




The idea of programming OO in Lua is to create a instance of a table with the approraite __index methamethod to allow access to the methods of that class.

You create the scheduler instance using some function similar to the :new() from above. Like so :

local instance = scheduler:new();

now the self parameter is valid for the given instance, otherwise calling the function of scheduler will not have any valid self parameter, whether u use the : or . notation.

now you can do something like..

instance:start();

There isn't enough information from the scheduler class to give you any clear answers.

Good Luck!

-ddn

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement