I am working with Panda3d 1.7 and Python 2.5. In my code, I have a world class, that, inside of it, are the player, and some test objects that I am using to test parts of code. In one of them, I am working on the targeting system, and I need to access the player.root (which is a Node, which represents the center of the ship) to set the target. How would I access it every frame?
I don’t think I understand what you are asking for, but I’ll take two sporting cracks at it:
“How do I access instance variables in Python?” – If player is a variable of your World class, and your code is a method in World, you can use self.player.root to access the node. In Python, instance variables are unprotected, so you can access them directly from any other class.
Wow, thanks for trying so hard, but you all missed. (sorry that sounds sarcastic, none intended)
Here’s what I mean:
class player:
def __init__(self):
self.root = render.attachNewNode(etc)
class World:
def __init__(self):
self.player = player()
self.info = 1234
What I want to know is how I would access world.info from inside world.player.
Now that I’ve written this out, I think I know. I would just make master an argument of player, and pass self as the argument inside world. If this is right what would I make it an argument of though? init? but then wouldn’t it only be accessible inside init?
class Player:
def __init__(self, world):
self.world = world
def eat_banana(self):
self.world.bananas -= 1
def count_bananas(self):
if self.world.bananas > 0:
print "there are", self.world.bananas, "bananas in my world"
else:
print "all out of bananas, surely i will starve to death"
class World:
def __init__(self):
self.bananas = 24
self.player = Player(self)
I recommend writing your class names starting with a capital letter, this will help prevent any confusion later on.
Ok thank you,
I normally do write my classes like that, it was just an example.
One last question, player.world is just a pointer to the world class, it doesn’t create a new world class right?
Thanks again guys for all the help,
Peter
Don’t mix “class” and “instance”.
A class is an abstract original.
Think of it as of a car. You know what a car does and how it might look like but you can’t point at “car”.
If you say “my car” or “the car over there” then you also have a “car”, but a specific one. This would be an instance.
Creating instances is done in Python like this:
mysinstance = Classname()
Inside “Classname”, i mean the class, you can describe how the class and its methods interact with future instances by writing “self”, which is a placeholder for any instance.
That said, from the above line “myinstance” could be seen the same as “self” depending on context (inside or outside the class).
Without the finishing brackets, the variable to the left would get a reference to the class, not to a just created object/instance:
myclass = Classname
I suggest you read on the web about object oriented programming in general (OOP). It’s kind of the base of Python.