I need help with creating a list of objects inside an area.

Ok i have my game where there is going to be multiple star systems in which you can travel. so here is my code so far:



import direct.directbase.DirectStart

gamePlaying = True
currentArea = "sol"

class area:
    def _init_(self):
        self.areaName = "untitled"
        
        self.type = "system"
        
        self.objectList = {"Sun": "Star"}

        self.backGroundTex = "stars"
        
    def renderArea(self):
        for key in self.objectList.keys():
            if key == "planet":
                planet.planetRender(key)
            
    

class planet:
    def _init_(self):
        self.planetName = "earth"
        
        self.area = "sol"
        
        self.xPos = 0

        self.yPos = 40
        
        self.zPos = 20
        
        self.position = (self.xPos, self.yPos, self.zPos)
        
        self.size = (15,15,15)
        
        self.planetType = "earth"
        
        
        
        
    def planetRender(self):
        if currentArea == self.area:
            self.sphere = loader.loadModel("Models/Planets/planet_sphere")
            self.tex = loader.loadTexture("Models/Planets/" + self.planetType + "_tex.jpg")
            self.sphere.setTexture(self.tex, 1)
            self.sphere.setPos(self.position)
            self.sphere.setScale(self.size)
            self.sphere.reparentTo(render)
            
            

            

        
class ship:
    def __init__(self):
        self.shipName = "fighter"

        self.xPos = 0

        self.yPos = 0
        
        self.zPos = 0

        self.position = (self.xPos, self.yPos, self.zPos)
        
        self.size = (0.25,0.25,0.25)

        self.hull = 100

        self.hullMax = 1000

        self.energy = 1000

        self.energyMax = 1000

        self.engineSpeed = 5
        
        self.turningSpeed = 5

        self.thrustingSpeed = 0

        self.thrustingSpeedMax = 10

        self.primaryWeapon = 1

        self.secondaryWeapon = 0

        self.secondaryWeaponMax = 0

        self.area = "sol"

        self.isComp = True;

    def shipRender(self):
        if currentArea == self.area:
            self.ship = loader.loadModel("Models/Ships/" + self.shipName)
            self.ship.setPos(self.position)
            self.ship.setScale(self.size)
            self.ship.reparentTo(render)
            
     
sol = area()
area._init_(sol) 
sol.objectList = {"Sun": "Star" , "earth" : "planet"}
   
       
# setting up the planets

earth = planet()
planet._init_(earth)

area.renderArea(sol)




# setting up the player
playerShip = ship()
playerShip.xPos = 0
playerShip.yPos = 0
playerShip.zPos = 0
playerShip.turningSpeed = 90
playerShip.isComp = False


base.disableMouse()

base.camera.setPos(0, playerShip.yPos - 30, 0)        

ship.shipRender(playerShip)

starbase = loader.loadModel("Models/toaster")
starbase.reparentTo(render)
starbase.setPos(0,0,10)
starbase.setScale(1,1,1)

run()

so i have a planet class with another method that renders it. And this would work by just rendering all the planets when the game starts but since there is multiple area class objects that each have their own objects to be rendered this would be hard.

So how do i fix my code so its easier to relate an object to its area? Im thinking of putting the planet class inside the area class. How would i do that though? And then how would i make a list of planets like:

if playerShip.area == “sol”:
foreach planet IN sol:
#render planet

Can somone help me with this?

I can’t get where’s the problem - the code seems to have all the data structures needed so could you explain in the specific what’s your concern?

I know there isnt a problem in the code at the moment. But my question is how can i create a list of all the objects (instances of planet class) inside and instance of an area class so that i can easily render everything at once like

foreach PLANET IN self.OBJECTLIST:
Render(Planet)

But im not sure how to do so. Can anyone please help

There is no foreach loop in python.
There is a loop like it that is just called the for loop.
Documentation here in section 4.2

yea i know i used that in my code… i just refer to it as foreach (too used to C#) but thats not really my problem. I just need to know how to make a simple list to store all the planet instance names so i can render them.

You mean like,

self.planets = [aPlanet, aPlanet2]

I’m beginning to think you got a pretty confused idea of what a class and a list are.

In your code there is a planet class - you have to instance that class and fill it with the data inherent the relative planet object, then store that instance in the area class. I mean:


for planetdata in planetsdatabase:
  p=planet(*planet parameters gathered from planetdata here*) # assign your class members (planetname, area, type etc) with those parameters
  area.objectlist.append(p)

in your code you ain’t defined a list of objects but a dictionary of strings, pretty useless - with my code you’ll define instead a list of planet objects that then you’ll render iterating like this:

for p in area.objectlist:
  p.planetRender()

let me know if helps or we’re still off the point

Well, problem solved :smiley: I just made a small mistake and thought it was impossible to have a list of instances when it actually wasnt so i made a list like xboxjoshone suggested and it actually worked!!! thanks for your help guys

Edit: HEre is a screenshot of it using the panda3d models as placeholders for now:

watch this for inspiration:
youtube.com/watch?v=P0-lsyo28SU

read here for ideas:
infinity-universe.com/Infini … &Itemid=27

awesome hint treeform thanks

treeform. I made some of the models for that game :slight_smile: I have been checking infinity’s website for the past 2 yrs waiting for it to be finished. But then i couldnt wait so i started my own :stuck_out_tongue: