Checking for method existance? in Lua

Started by
3 comments, last by ddn3 15 years, 9 months ago
I'm using Lua for my project, and have written some code for managing objects. These objects use an object oriented paradigm, ie data + methods. Im extending the interface and want to add new methods, but don't want to retrofit all the existing objects. Is there a way to check for a methods existance in Lua? Some code to demonstrate :

 function AppCore:onPostUpdate()
	for index, value in pairs(self.mManagers) do
		if (value.onPostUpdate~=nil) then 
			value:onPostUpdate(); 
		end
	end
	return 1;
 end


So this snippet just iterates over a set of managers and checks to see if the onPostUpdate function exist before calling it, since i've added it later and most of the managers don't have them. This doesn't work becuase value.onPostUpdate will always be nil becuase its the function doesn't live in the objects but some other place I can't ascertain. Any clues would be helpful thanks! :D -ddn
Advertisement
How about just plain

if value.onPostUpdate then

?
Million-to-one chances occur nine times out of ten!
value:onPostUpdate()

is just a shortcut for writing:

value.onPostUpdate(value)

So "onPostUpdate" must return something otherwise there would be nothing to call. I think that even if you would enter the if-statement you would get an "attemt to call a nil value".

Try to do some hack to verify if the call works in practice or not.

Are you using any metamethods such as __index or __call?
Yes im using metamethods __index to build a type of OO functionality in Lua, as suggested by some Lua wikis I've read. I understand the basic concepts of overloading the metamethods, but haven't develed too much into it. Here is a sample class impl for a manager :
Game_Object_Manager = { } -- Create a table to hold the class methodsfunction Game_Object_Manager:new() -- The constructor   __object =    { 		mActiveSet	={},	--active fx object set, all running fx objects live here		mDestroySet	={}		--destroy fx object set, for those fx objects to be destroyed this frame   }   setmetatable(__object, {     -- Overload the index event so that fields not present within the object are     -- looked up in the prototype parent class table     __index = Game_Object_Manager   })   return __object end  -- Initlaization function for this manager function Game_Object_Manager:onInit()	print("-->Game_Object_Manager onInit start.")	print("-->Game_Object_Manager onInit finish.")	return 1 end  -- Destroy function for this manager function Game_Object_Manager:onDestroy()	print("-->Game_Object_Manager onDestroy start.")	print("-->Game_Object_Manager onDestroy finish.")	return 1 end  -- Reset function for this manager function Game_Object_Manager:onReset()	print("-->Game_Object_Manager OnReset start.")	print("-->Game_Object_Manager OnReset finish.")	return 1 end  -- Update function for this manager function Game_Object_Manager:onUpdate()	return 1 end  -- Creates the game object, returns the Game_Object if it successfully creates one otherwise nil function Game_Object_Manager:createObject(typeName,setup)	local entity = Game_Object:new();		entity.mHandle = nfCreateObject(typeName,setup);		if (entity.mHandle==0) then 		return nil; 	end		return entity; end  


So this impl does overload the __index metamethod of the object so it uses a common class interface/impl for this object.

I'm going to mess around somemore with it, perhaps looking in the metatable for the interface might be the way to go?

Thanks again!

-ddn
Just to update everyone, I tried Mike nl suggestion and instead of checking for nil, i just checked for existence of the method and it worked ! :D so ezy now.

Thanks again!

-ddn

This topic is closed to new replies.

Advertisement