Page 1 of 1

[01 Feb 2008] Object Oriented lua scripts

Posted: Sat Jun 13, 2020 5:31 pm
by Dwayne
Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 1 Feb 2008 01:08:11 +1100

Andreas,

How much of the OO stuff for Lua has been implemented in Hollywood?

I was trying to create a class from the LUA reference manual and it failed i.e.

Code: Select all

Character = {}

function Character:new (o)
                o = o or {}
                setmetatable  (o, self)
                self.__index = self
                return o
end
and the compiler fails with Opening Bracket Expected (line and line number of the function statement).

Regards,

Dwayne

[31 Jan 2008] Re: Object Oriented lua scripts

Posted: Sat Jun 13, 2020 5:31 pm
by airsoftsoftwair
Note: This is an archived post that was originally sent to the Hollywood mailing list on Thu, 31 Jan 2008 22:27:59 +0100
Andreas,

How much of the OO stuff for Lua has been implemented in Hollywood?
Most features of Lua 5.0 are also available in Hollywood, but the syntax is mostly different. Also pay attention to the fact that the latest version of Lua is 5.1 but Hollywood uses 5.0.
I was trying to create a class from the LUA reference manual and it failed i.e.

and the compiler fails with Opening Bracket Expected (line and line number of the function statement).
Hollywood does not support the ":" type declaration because the colon conflicts with the Hollywood 1.0 syntax emulation which is still present in Hollywood 3. Once Hollywood 1.0 compatibility will be removed, you can also use the colon. Until then, you have to declare the function in a different way:

Code: Select all

Character.New = Function(o) .... EndFunction
Also

Code: Select all

return o
won't work in Hollywood. It must be put into parentheses:

Code: Select all

return(o)
And finally, the "End" must be an "EndFunction" in Hollywood. Please see the Hollywood manual for documentation.

[01 Feb 2008] RE: Object Oriented lua scripts

Posted: Sat Jun 13, 2020 5:31 pm
by Dwayne
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

[03 Feb 2008] RE: Object Oriented lua scripts

Posted: Sat Jun 13, 2020 5:31 pm
by airsoftsoftwair
Note: This is an archived post that was originally sent to the Hollywood mailing list on Sun, 03 Feb 2008 11:45:55 +0100
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.
Yes, "self" is currently not implemented because it uses the colon syntax which I can't implement as long as Hollywood supports Hollywood 1.x scripts. I will think about this issue and probably introduce the "self" concept in the next update.

However, it should be possible to do everything even without "self". I'm not an OO expert, but I think this should do what you want. You just have to emulate the "self" keyword by using an extra argument in the constructor (here called "myself"). I would implement it in the following way:

Hope this helps.

Best regards,

Andreas --

[03 Feb 2008] RE: Object Oriented lua scripts

Posted: Sat Jun 13, 2020 5:31 pm
by airsoftsoftwair
Note: This is an archived post that was originally sent to the Hollywood mailing list on Sun, 03 Feb 2008 11:52:36 +0100

Hmm, I do not seem to be able to put my code into a mail. It doesn't come through. Weird.

So, please find it here: http://www.softwarefailure.de/test.hws

If I understood you correctly, this should do what you want. The error in your code was that your class did not inherit "barbarian" but "character". Thus, it did not know the "ChangeName" method. In my code the class "pc3" inherits "barbarian" which inherits "character".

[04 Feb 2008] RE: Object Oriented lua scripts

Posted: Sat Jun 13, 2020 5:31 pm
by Dwayne
Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 4 Feb 2008 00:36:16 +1100

Andreas,

This worked perfectly and ended up being a lot more succinctly than mine which I completed a little while ago.

I got confused about the self object and to be honest don't completely understand how your code uses it but it will be the basis for what I am trying to do.

Regards,

Dwayne

[04 Feb 2008] RE: Object Oriented lua scripts

Posted: Sat Jun 13, 2020 5:31 pm
by airsoftsoftwair
Note: This is an archived post that was originally sent to the Hollywood mailing list on Mon, 04 Feb 2008 12:56:06 +0100
Andreas,

This worked perfectly and ended up being a lot more succinctly than mine which I completed a little while ago.

I got confused about the self object and to be honest don't completely understand how your code uses it but it will be the basis for what I am trying to do.
I don't use "self" at all, because it's not present in Hollywood. I'm just emulating the behaviour by introducing an argument called "myself" which always gets the parent object passed.