Self parameter

Find quick help here to get you started with Hollywood
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Self parameter

Post by Allanon »

I made a mistake in the previuos post, well the answer is that this way I'm able to create new objects from objects, see below what I mean:

Code: Select all

myClass = { x = 0, y = 0 }

Function myClass:New(parameters)
   Local NewObj = CopyTable(self) ; <--- here is where I've made the mistake in the previous post
   TB.Set(NewObj, parameters, False)
   Return(NewObj)
EndFunction

Local myObject = myClass:New()

; Now I can derivate objects from the one we have just created
; but I want more properties
myObject.z = 1
myObject.k = 0.5

; New object based on myObject
myDerivedObject = myObject:New({ z = 10 })
This way the original class is untouched and I'm able to override any existing properties because the TB.Set() used in the New method.
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
tolkien
Posts: 193
Joined: Sun Oct 17, 2010 10:40 pm
Location: Spain

Re: Self parameter

Post by tolkien »

Yes, I understand the error and what you explain.

I´ll try to code something to practice it.

Thanks again mates.
Post Reply