Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Kurai Tsubasa

Member Since 22 Aug 2013
Offline Last Active Oct 11 2014 05:30 AM

Posts I've Made

In Topic: What happen if "self" in second argument

22 September 2014 - 04:17 AM

nothing to be sorry about, just constructive criticism.

 

 

 

 

Yeah,i am pretty familiar with OOP smile.png ,i know what self do in first argument but not in the second argument,thank you

 Oh sorry, your first post was hard to understand.







self.tilemap.update(dt / 1000., self)

tilemap.update() takes 3 arguments, self(i.e tilemap), dt, and game. Second self indicates to 'game' instance, which tilemap is a member of. And since we're in scope of game class. we pass it as 'self'.

 

 

Sorry for my post ^^a.Now i don't understand how game can be an instance mellow.png .Thank you

 

P.S: i only know how to make instance like this: a=classname() ^^a.

 

 

Let us walk through this code:

First off is



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Pyllet Town")
    Game(screen).main()

I think everything is quite straightforward except for the last line:

With Game(screen) you create an instance, but then it gets funny, because on that red hot instance a function is called (with .main()). Notice, that we do not take a reference to this new instance (we have no assignment). That means that the instance will be unreachable as soon as the function returns, but since the whole game runs in the main() method we are comfortable with that.

So now we look at the main() method:



def main(self):
    while 1: 
        self.tilemap.update(dt / 1000., self)

This is where things begin to get python and your confusion probably stems from:

From the Signature ( main(self) ) we can see, that we get passed one argument, the reference to the instance which this function is called on. This instance is of course the instance created with Game(screen). The tricky part is, that python passes this argument implicitly for every member method, which is why you don't see it in the call ( .main() ).

The next line is just the obvious main loop almost all games run in.

But then we get another strange line, which is easiest to understand when dissected:

self still is a reference to the instance of Game, so we are looking up it's tilemap (a reference to a tilemap instance) and call the function update on that tilemap instance.

This tilemap takes two arguments: The time that has passed since the last frame as well as the Game instance currently running the main funcion (probably to do some draw calls on that game object).

The update method itself will look like this:



def update(self, time_elapsed, game):
    #do something

What is important to understand is, that the self of the update function is a reference to the tilemap instance while the game in the update function is a reference to the game instance (we just handed this reference over by putting the self reference of the main function into this argument position).

 

I hope this helps, scoping and lifetimes can be quite confusing.

Wow,you explain it clearly.Thank you very much for your answer it really great explanation laugh.png 


In Topic: What happen if "self" in second argument

21 September 2014 - 04:56 AM

you beat me to the post.

@Kurai: this is the correct solution, next time you could specifically point out the line you are having trouble with (I only now saw it)

i'm really Sorry for my post ^^a


In Topic: What happen if "self" in second argument

21 September 2014 - 04:55 AM

 

Yeah,i am pretty familiar with OOP smile.png ,i know what self do in first argument but not in the second argument,thank you

 Oh sorry, your first post was hard to understand.





self.tilemap.update(dt / 1000., self)

tilemap.update() takes 3 arguments, self(i.e tilemap), dt, and game. Second self indicates to 'game' instance, which tilemap is a member of. And since we're in scope of game class. we pass it as 'self'.

 

 

Sorry for my post ^^a.Now i don't understand how game can be an instance mellow.png .Thank you

 

P.S: i only know how to make instance like this: a=classname() ^^a.


In Topic: What happen if "self" in second argument

21 September 2014 - 04:37 AM

In order for python to know which object you refer to, you use self

 

What you mean is self referring to the object Game(screen).main().I'm pretty confused with self in second argument ^^a


In Topic: What happen if "self" in second argument

21 September 2014 - 04:01 AM

Hello there.

 

Quick question: are you familiar with OOP?

 

'self.' is just a pythonic way of saying member of a class, or more like an instance of a class

 

Really quick comparison:













class jedi
{
public:
    int force;
    double lightsaber;
};












class jedi(object):
    def __init__(self):
        self.force = 0
        self.lightsaber = 0

In both cases force and lightsaber are class members, each instance of jedi will have their own force and lightsaber.

 

edit:

and python is designed that you need to pass 'self' (class instance) to its method among other arguments.

Yeah,i am pretty familiar with OOP smile.png ,i know what self do in first argument but not in the second argument,thank you


PARTNERS