Page 1 of 1

How to make Function with : on a fly?

Posted: Tue Dec 01, 2020 10:54 am
by Bugala
I have this situation here (writing this longer than necessary to get the idea better)

Code: Select all

OOP = {}

Function OOP:NewInstance()
	o = {}
	o.variable = 1
	Return(o)
EndFunction

Function OOP:FuncToBeMadeLater()
EndFunction

Function OOP:NewInstance_Type1()
	o = OOP:NewInstance()
	o:FuncToBeMadeLater = Function() stuff EndFunction
	Return(o)
EndFunction

testOOP = OOP:NewInstance_Type1()
By other words I am first making a prototype OOP, and then there will come different types of OOPs that are going to be using that Prototype to get their stuff in place, but these different types will have a different "FuncToBeMadeLater" method, that I am planning on making at point I am making New Instance of Type1, Type2, Type3...

But now the problem is that I want to use the SELF system, which means I need to use : when declaring that. However, when I do that at O:FuncToBeMadeLater = Function() stuff EndFunction, it gives me an error.

If I use O.FunctoBeMadeLater instead, it works fine, except I dont have the self then.

Is there a way for me to do this in such a way that I can get the SELF there?

Re: How to make Function with : on a fly?

Posted: Tue Dec 01, 2020 1:44 pm
by Bugala
I realised I can do the function the traditional way inside the function to solve this problem:

Code: Select all

Function OOP:NewInstance_Type1()
	o = OOP:NewInstance()
	function O:MyFuncToBeMade()
	stuff
	endfunction
endfunction
But in case I need it, is there also a way to get it done in way of:

Code: Select all

O:MyFuncToBeMade = function() stuff endfunction
?

Re: How to make Function with : on a fly?

Posted: Tue Dec 01, 2020 4:16 pm
by SamuraiCrow
You can pass self to a function explicitly as the first parameter. The : only implicitly adds the self parameter to contain the table it's a member of.

Code: Select all

o.fntoaddlater=Function(self) stuff EndFunction