OOP modules help needed

chris-allnut - When you do “self=nil” in your example, you’re not actually setting the ‘newbullet’ to nil. As SegaBoy mentioned, table functions run the same as anonymous functions that are passed “self” for the first argument – “self” is automatically declared locally (as reference to the function’s parent table), when the table function runs. However, ownership can be re-assigned through dot notation (also known as “decorating”/ “inheritance”). That’s why I leave creation and destruction of instances up to the Class, not the (instances themselves).

thomas6 - Try to define functions with colon notation when you feel that an object should own the function (i.e. when something needs to move, jump, etc). and define functions with dot notation if they need to be assigned late, locally (temporary), to change ownership (decorate/ inherit), or when ownership is clearly not needed.

Thanks for all the good words, it totally make writing the tutorials worth while! [import]uid: 36792 topic_id: 34539 reply_id: 138004[/import]

ArdentKid,
I thought that

function object.setNil(self, a, b)  
 self = nil  
end  

was the same as

function object:setNil(a, b)   
 self = nil  
end  

but your saying the first just passes a reference, while the seconds self is the actual object?

Or is it a difference of a table function vs a locally defined function used with a closure? [import]uid: 130806 topic_id: 34539 reply_id: 138011[/import]

chris-allnutt - Correct, the functions are essentially the same, but the issue has to do with scoping. When passing variables, they are always received locally. By nil-ing “self”, you are only nil-ing the local reference. However, since “self” is also being referenced outside of your function (it exists elsewhere), then any other modifications you make to that reference will sustain.

Just remember always have to nil from the parent level, which is why it’s best to have the Class do it for an instance such as a bullet. [import]uid: 36792 topic_id: 34539 reply_id: 138016[/import]