(15)
Got a new problem with class instances and intervals it seems.
I’ve got a base class for spaceships now, it has some attributes (which are present), one of them being the model node “.ShipModel”. Attached to the model node are: nodes for exhaust graphics; nodes for running lights; nodes for rotating parts.The player ship is based on that as well as some NPC ships that I throw in every some seconds. The exhaust appears only when I hit the UP button, so the NPC ships don’t have that effect, they have the nodes though for later use. So currently on the NPC ships you got two effect: blinking lights and rotating parts, while on the player ship you got those two + the exhaust when UP is pressed.
Adding the player:
def addPlayerShip(self):
self.PlayerShip = MCShipBase(self.PlayerShipIdentifier)
Adding NPC ships:
def addSceneryShipByJumpIn(self):
self.addShipObject(MCShipBase("Commando Ship"))
# this adds it to a dictionary
The ship base class, just the important parts:
class MCShipBase(object): # object
def __init__(self, ShipIdentifier): # ShipIdentifier
self.ship_exhaustlist = [] # holds the
self.ship_runlightlist = [] # exhaust and runlight nodes
def loadShipModel(self):
self.ShipModel = loader.loadModel("../mcships/bla")
self.ShipModel.reparentTo(render)
return self.ShipModel
### rotating parts
def buildShipModelRot(self):
self.shiprotmodel = loader.loadModel("../mcships/")
self.shiprotmodel.reparentTo(self.ShipModel)
self.shiprotint = self.shiprotmodel.hprInterval(bla)
self.shiprotint.loop()
### runlights
def buildShipRunlights(self):
for i in range(bla):
self.ship_runlightlist.append(self.createShipRunlights(self.ShipIdentifier, i))
self.ship_runlights_seq = Sequence(Func(self.showShipRunlights), Wait(0.05), Func(self.hideShipRunlights), Wait(bla), name="spaceship_runlight_seq")
self.ship_runlights_seq.loop()
def createShipRunlights(self, ShipIdentifier, indexno):
runlgt = CardMaker("runlight")
runlight = runlgt.generate()
self.runlgtnode = self.ShipModel.attachNewNode(runlight)
self.runlgtnode.reparentTo(self.ShipModel)
def showShipRunlights(self):
for runninglight in self.ship_runlightlist:
runninglight.show()
The problem here is that as soon as I throw in a new instance of the class (here: a new NPC ship), the runlights disappear. If only the player is present and no NPC ships, all continues to work, the exhaust, the runlights and the rotating part (the latter two using intervals). As soon as the first NPC ship comes in, the rotating node on the player ship continues rotating, the exhaust appears when pressing UP, but the runlights disappear. On the NPC ship things rotate and blink … until the next NPC ship comes in, then the runlights go off while the rotation continues. So the runlight effect only appears on the most recent ship.
I thought this comes from losing the class attributes at the creation of a new instance, here: the runlights and/or the intervals. But checking with getChildren() the nodes are still there. I thought the intervals would go, but checking with isPlaying() it says all intervals are still playing.
A screenshot to illustrate things: s5.directupload.net/images/090902/44zact9z.jpg
In the middle the player ship. The satellite dish rotates. Every some seconds a blinking light shows (not in picture). As soon as the NPC ships come in (here: every 5 seconds a new one), the satellite dish keeps rotating, but the blinking lights appear only on the most recent ship, even though the rotation interval continues.
I am using a dictionary to store stuff, but I don’t think that has to do with anything, like: {0: instance adress, 1: instance adress, …}
Then the main loop looks like:
def updateShipModels(self, task):
for ID in self.ShipObjects.keys():
self.positionObject(self.ShipObjects[ID].ShipModel)
return Task.cont
All effects use nodes and reparentTo(bla.ShipModel), all effects use intervals. One thing I notice is that for the rotating part I use the shortcut:
self.shiprotint = self.shiprotmodel.hprInterval( ... ).loop()
For the exhaust I use no interval (since they appear on key press), the runlight effect uses a function sequence. There are no console errors.