Function defined using self.var at later point, possible?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 759
Joined: Sun Feb 14, 2010 7:11 pm

Function defined using self.var at later point, possible?

Post by Bugala » Fri Jul 21, 2017 11:05 pm

What I am trying to do is to first define a function that uses "self" as part of it:

Code: Select all

function myfunc()
   self.var = self.var + 1
endfunction
Then at later point i want to attach it to my table the following way:

Code: Select all

table = {}
table.func1 = myfunc
But how do i do this, or is it even possible?

I am not looking for alternative solution to this, since I already know how I can do it alternatively, I am just hoping to find out if it would be possible to do it this way, since this way i believe it would be better from code reading/maintenance point of view.

User avatar
Allanon
Posts: 461
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Function defined using self.var at later point, possible

Post by Allanon » Sat Jul 22, 2017 10:47 am

Hi Bugala,

if you want to use self, classes and methods I suggest you to read this.
----------------------------
[Allanon] Fabio Falcucci
AMC - Creative Development // Docs Site // Support Forum
Support me on Patreon for Hollywood libraries!

Bugala
Posts: 759
Joined: Sun Feb 14, 2010 7:11 pm

Re: Function defined using self.var at later point, possible

Post by Bugala » Sat Jul 22, 2017 4:06 pm

Thanks from that link. Didnt know that was available from internet. I actually have the third edition book myself from where i have read these, but I am quite new to using methods and classes still (although i now find them to be superior to what i used to do) so I am still having problems here and there, and this before mentioned question is one of them, since you can attach a function in hollywood just like that:

Code: Select all

function myfunc()
b=b+1
endfunction

t.func = function () a=a+1 endfunction

t.func = myfunc
and now t.func is myfunc, not the original function.

But I am not sure how do you attach a function with self references, or do you always have to inherit them?

User avatar
Allanon
Posts: 461
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Function defined using self.var at later point, possible

Post by Allanon » Sat Jul 22, 2017 11:32 pm

Your example is right, for your question here is a sample:

Code: Select all

; define a basic class
myClass =  { a = 10 }

; and its method
function myClass:func(data)
   ; add data to the <a> class item

   ; self refers to the <myClass> table, to be precise self refers to what is on left of the colons
   self.a = self.a + data
endfunction
Now that you have defined a class you can instatiate a new object and modify its own function or modify the class function.

1. Instantiate a new object and change its function

Code: Select all

; Instantiate the new object
myObj = CopyTable(myClass)

; Chamge the object's method
function myObj:func(data)
   self.a = self.a - data
endfunction
This way only the object's method is changed, if you need to change the class method you have to overwrite the "func" function on myClass table.
----------------------------
[Allanon] Fabio Falcucci
AMC - Creative Development // Docs Site // Support Forum
Support me on Patreon for Hollywood libraries!

Bugala
Posts: 759
Joined: Sun Feb 14, 2010 7:11 pm

Re: Function defined using self.var at later point, possible

Post by Bugala » Sun Jul 23, 2017 9:27 am

Yes, that I have understood, but my specific question was about replacing myObj:func with just func?

That is there a way to:

Code: Select all

myfunction(data)
self.data = data
endfunction

myObj:func = myfunction
Notice that the "myfunction" doesnt belong to any class at beginning, that I am only hoping to define that function so that it would later be added as a method on a class through class:func = myfunction

As to explain it alternativale, if i have:

Code: Select all

myfunction(data)
myvar = data
endfunction

myObj:func = myfunction
then this works just fine, as the myfunction is simply changing the myvar to be the value of data.

But what I want to do, is to define this myfunction (which at beginning isnt a method, but normal function) to use "self", so that at later point when it is attached to a class as a method, it would then be using "self".
So idea is that I dont plan to use that myfunction alone at any point. I am just hoping to make it separate so that it is better from code maintenance point of view and then at later point attach it to a class to becoma a method.

SamuraiCrow
Posts: 215
Joined: Fri May 15, 2015 5:15 pm
Location: Ft. Collins, Colorado USA

Re: Function defined using self.var at later point, possible

Post by SamuraiCrow » Sun Jul 23, 2017 10:39 am

Pass the self pointer as the first parameter and it might work. Even if it does not work, you could always have a wrapper method call it as a workaround.

Bugala
Posts: 759
Joined: Sun Feb 14, 2010 7:11 pm

Re: Function defined using self.var at later point, possible

Post by Bugala » Sun Jul 23, 2017 2:59 pm

I thought i already tried passing self as first argument and it failed, but i now tried it again, and it worked.

Thanks SamuraiCrow, this solves my problem.

for those wondering, heres the working solution test:

Code: Select all

Function func(self)
self.a = self.a + 1
EndFunction

t = { a=1 }
t.func = func

t:func()

DebugPrint(t.a)
results in debug printing "2"

Post Reply