Can Class A method be copied to be class B method?

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

Can Class A method be copied to be class B method?

Post by Bugala »

Here is what I am trying to do:

Code: Select all

t1={ var=0 }
function t1:func1func()
self.var = self.var + 1
endfunction


t2 = { var=0 }
t2:func2func = t1:func1func 
But I am not sure how can i do it, or is it even possible to do so. For even if i succeed in copying the function, will the new functions self be then pointing towards t2 self, or will it still continue pointing towards t1 self?
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Can Class A method be copied to be class B method?

Post by Allanon »

Code: Select all

t1={ var=0 }
function t1:func1func()
self.var = self.var + 1
endfunction


t2 = { var=0 }
t2:func2func = t1:func1func
Could be written like this:

Code: Select all

; Defines the class
myClass=
  { var=0,
    func1 = function() self.var = self.var+1 endfunction
  }
endfunction

; Instantiate a new object
t1 = CopyTable(myClass)

; Instantiale a new object
t2 = CopyTable(myClass)

; Uses the first object created and its method
t1.var = 10
t1:func1()
You could alse create a method to create a new object like this:

Code: Select all

; Defines the class
myClass={ var=0 }
  function t1:func1func()
  self.var = self.var + 1
endfunction

function myClass:New()
   Return(CopyTable(self))
endfunction
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can Class A method be copied to be class B method?

Post by Bugala »

Yes, that is basically what I am doing.

But what my case is, to be more specific is that I am making as an exercise this card game which has more than one deck. (not normal decks, but decks full of special cards)

Now each deck has several methods, which i have put to Prototype_deck class.

Methods like:

Code: Select all

prototype_deck:New()
prototype_deck:Shuffle()
prototype_deck:DealACard()
prototype_deck:FindACard()
These are inherited to new card decks with:

Code: Select all

SpecialCardsDeck = Prototype_deck:New()
However, each player has his own playdeck that each contain the same 15 cards in them. But there is one special case with these player decks, for at beginning of them they need to pick one certain card (called "aboa") and put it to the bottom of the deck.

I can of course do it by adding new method to prototype deck:

Code: Select all

function prototype_deck:PutAboaToBottom()
stuff
endfunction
But since I am aiming for my code to be reusable, which means that if i make another game which has card decks, I am hoping that I can simply copy paste the whole "prototype_deck" class and simply use it at new game. And in this case, the PutAboaToBottom() method is such a specific class that it is unlikely there is similar method needed in any other games decks, and for that reason I would like it to be separate from the "prototype_deck" and instead be specific to these "player_deck[n]"s only.

I could make it on fly as:

Code: Select all

player_deck[n]:PutAboaToBottom = Function () stuff EndFunction
But then that piece of code gets in middle of code making it hard to find afterwards, and it also wont be listed in list of functions.

So for both better accessibility as well as style reasons, I would wish to make that method first at place where i could put all the specific to this game methods, and then copy that function to each of those player_deck[n]:

Code: Select all

function PutAboaToBottom()
do the stuff
endfunction

for n=1 to 4
   player_deck[n]:PutAboaToBottom = PutAboaToBottom
next
This would be from maintainability and understandability point of view the best solution i can come up with, but I dont know how to actually do it.
Post Reply