Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 1 Feb 2008 20:49:32 +1100
Hi Andreas,
Still struggling with the OO concepts and what has been implemented.
I have attempted to do this in various ways but it still seems to not work i.e. the lua reference that I am reading relates to 5.0.. I worked out that self for one reason or another does not refer to itself in Hollywood (correct me if I am wrong) so I had to re-write it with the name of the object and this worked as it gave me the capability to create two objects based on the same prototype. These values will change independantly. This was achieved using the following (typed from memory so there could be some typos):
Code: Select all
Character={}
Function Character.New (o)
o=o or {}
o.name="base"
setmetatable (o, Character)
Character.__index=Character
return (o)
EndFunction
pc1=Character.New(pc1)
pc2=Character.New(pc2)
nprint (pc1.name)
nprint (pc2.name)
pc1.name="changed"
nprint (pc1.name)
nprint (pc2.name)
This seems to work and the values held within pc1 and pc2 are completely seperate.
I was also able to write some methods as well :
Code: Select all
function Character.ChangeName (o, newname)
o.name=newname
endfunction
pc1.ChangeName (pc1, "changed")
I wanted to use inheritance to create a new prototype based on an existing prototype thus:
Code: Select all
Barbarian = Character.New (Barbarian)
pc2=Barbarian.New (pc2)
Which works but if I try to extend the methods of theis supposed inherited prototype it fails i.e.
Code: Select all
Function Barbarian.ChangeName (o)
o.name="barbarian"
EndFunction
It tells me that table o{} doesn't exist..
I tried to apply some logic to my thinking and created a new method with the following statements but this also fails.
Code: Select all
Barbarian = {}
Function Barbarian.New (o)
o=Character.New (o)
setmetatable (o, Barbarian)
Barbarian.__index = Barbarian
return (o)
EndFunction
This still does not seem to inherit the original Character prototype..
I would appreciate a very simplistic example of how to implement Classes and Inheritance as I am wracking my brain trying to figure out what will and won't considering their are syntax differences between Hollywood and Lua 5.0 I can't use the references and am trying to modify them.
Regards,
Dwayne