Question on referencing nodes and parents (Beginner)(SOLVED)

Hello, I’m a new guy who just joined the forums a while back, have to work on a 3D game project with my friends, so I’ll most likely be popping by often…

The one thing I constantly have problems with at the moment, however, is the issue of “referencing”…

Lets say, you create an object Crate with the class of Crate. Is there any way that in the Crate class code, I can get the value of something from the World, or another object from another class?

If you know Flash, it would go something like…

class Crate(){
print _root.World.value

or something like that.

Is it possible?

Thanks in advance!

myVariable = myValue

class myClass():
   global myVariable
   print myVariable

you could create another script, for instance, and type in all the world variables, to access them you type in your main script or in the script you want to print, change or use that global variables:

import globals

class myClass():
   print globals.myVariable

EDIT: myVariable has to exist in the script

I hope I helped you

Ohhh… So…

myVariable = myValue 

class myClass(): 
   global myVariable 
   print myVariable

This will be how my global class code will look like?

As in,

Then I import it into my main script, and everytime i need it, I call the class and the variable?


Like that?


globals.myVariable will call your variable
globals.myClass() will call your class

if you want a variable, function or method inside myClass you type:

print globals.myClass().classVariable

PS.:classVariable, for instance, must be created like that:

class myClass():
     classVariable = value

valueReturned = globals.myClass().myClassFunction() will store a value that function myClassFunction() will return

Thanks for the advice everyone!

Actually… I kinda came up with a simpler solution… Just input the World into the class initialisation…

Kinda like this:

Class Pie(self,world): = world

Class World(DirectObject):
  thepie = pie(self)

And it actually works! :smiley:

Thanks alot for the ideas though! It can help alot for later…

Yeah I think passing the instance of the world class would definitely be the best way here but I’m pretty sure it’s done like this

class Pie (DirectObject):
    def __init__( self,w)
        #note that I used w intead of world to avoid any issues with names = w