I wanna create a game engine based on Panda3D but can't find out wheres the problem

So i can do the window but i can’t create an object.It gives me “Attempt to spawn multiple ShowBase instances!” error…

class TestEngine(ShowBase):

    def __init__(self):

        global ShowBase

        super().__init__()

        #ShowBase.__init__(self)

        self.disableMouse()

        properties = WindowProperties()

        properties.title = " "

        properties.setFullscreen(1)

        properties.setSize(1366, 768)

        globalClock.setMode(ClockObject.MLimited)

        globalClock.setFrameRate(60)

        base.setFrameRateMeter(False)

        self.win.requestProperties(properties)

class Object(NodePath):

    def __init__(self, Name, ModelPath, SizeX = 1, SizeY = 1, SizeZ = 1, PosX = 0, PosY = 0, PosZ = 0):

        ENGINE = TestEngine()

        Name = NodePath()

        self.scene = Name

        self.scene = ENGINE.loader.loadModel(ModelPath)

        self.scene.setScale(SizeX, SizeY, SizeZ)

        self.scene.setPos(PosX, PosY, PosZ)

        self.scene.reparentTo(self.render)

As it stands, your code will create an instance of the engine–which inherits from ShowBase–with every “Object” instance that you create. And, as the error indicates, there may be only one instance of ShowBase per program!

So, I suppose that my question is this: Why is it that you’re creating an instance of the engine with every Object?

I have to add definitions inside every class.So people can modify the Object.But i don’t have any idea how i am gonna fix that thing…

I… don’t follow. Why would there need to be an instance of ShowBase inside the Object in order for people to modify it…?

Well people can do something with them.Like change their positions later or change their size or something else.Do you know how to fix it?

But none of that requires that there be a ShowBase instance in the class.

In short: Don’t have a ShowBase instance in the class.

… If I may, perhaps it might help to link you to my “Panda3D Beginner’s Tutorial”–amongst other things it covers one approach to designing the classes for a game, which might prove useful to you in this matter. Here’s a link below, if you’re interested:

Wait doesn’t it needs ShowBase?How can i do it without ShowBase can you please tell me?

This depends on what you mean by “it”.

You overall game might be aided by the presence of a ShowBase object (although there are other ways). However, depending on your design, your Object class very likely doesn’t require a ShowBase object–or at least, doesn’t require a separate ShowBase object for every Object.

To some degree this depends on quite what you have in mind, but a simple design might look something like this:

class TestEngine(ShowBase):
def __init__(self):
        super().__init__()

        # The rest of your setup--e.g. "self.disableMouse()"--follows
        # here. I've just omitted it for brevity

class Object(NodePath):
    def __init__(self, EngineObject, Name, ModelPath, SizeX = 1, SizeY = 1, SizeZ = 1, PosX = 0, PosY = 0, PosZ = 0):
        self.model = EngineObject.loader.loadModel(ModelPath)
        self.model.setScale(SizeX, SizeY, SizeZ)
        self.model.setPos(PosX, PosY, PosZ)
        self.model.reparentTo(EngineObject.render)

In this case I’ve chosen to pass a reference to the ShowBase object into the constructor of the Object class as a parameter, giving me access to it without requiring that I make a new one.

That said, personally I prefer to keep a “common” file that stores a reference to the ShowBase object and that other files can then import–I find it a little neater, myself.

Thanks.I guess EngineObject is

Engine = TestEngine()

right?

Sort of–but there wouldn’t be one made for every Object. There would be just one, and it would be passed in whenever an Object is made. Something like this:

class TestEngine(ShowBase):
def __init__(self):
        super().__init__()

        # The rest of your setup--e.g. "self.disableMouse()"--follows
        # here. I've just omitted it for brevity

        anObject = Object(self, "a name", "model.egg")
        anotherObject = Object(self, "another name", "model.egg")
        # Note that these pass in "self", which here refers to
        # the current instance of "TestEngine"

You’d then just start the “TestEngine” as per usual.

Thanks man that works :slight_smile:

Not a problem! I’m glad if I’ve helped! :slight_smile: