Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Understanding basic function of classes — Gideros Forum

Understanding basic function of classes

incipientincipient Member
edited February 2014 in General questions
Hi All,

I am getting back into Gideros after a while, and I seem to have forgotten some very basic class stuff! Feel free to give me a heads up how this should work.

I have my "main" class:

| Home = gideros.class(SpriteBase)
|
| function Home:init()
| bl = myclass.new()
| bl.method1()
| bl.method2()
| end
|
| home = Home.new()

I then have this code for my class.

| myclass= gideros.class(SpriteBase)
|
| function myclass:init(images, speed)
| self.here = "i want to print this"
| -- print it here
| print(self.here)
| end
|
| function myclass:method1()
| -- here
| print(self.here)
| end
|
| function myclass:method2()
| -- and here
| print(self.here)
| end

Can someone tell me what the print calls from method1 and method2 print?

Using something like C# or a "true" OOP language, they should print "i want to print this" because the self (as in self.here) would be the "same" self that exists in the methods...can someone tell me how best to mimic this in Gideros? I can pass a copy of myclass to each method...but that just seems very messy.

Thanks for your help! :)

Comments

  • There is no hope for some people...syntax error! I used "." where I should have used ":"!

    Such a silly mistake taking so much time...
  • eezingeezing Member
    Accepted Answer
    Yep, syntactic sugar. All of the following are the same.
    some = Core.class(Sprite)
     
    function some:init()
     
    	print("----- inside class -------")
     
    	self:foo()
    	self.foo(self)
    	some.foo(self)
     
    	self:bar()
    	self.bar(self)
     
    end
     
    function some:foo()
     
    	print(self)
    end
     
    function some.bar(self)
     
    	print(self)
    end
     
    local new = some.new()
     
    print("----- outside class -------")
     
    new.foo(new)
    new:foo()
     
    new.bar(new)
    new:bar()
     
    some.foo(new)
    some.bar(new)
Sign In or Register to comment.