Methods of level loading in Panda3d (Solved)

I’m pretty sure that you can use “base” if your main class is a sub-class of “ShowBase”–in fact, a quick check indicates that I’m doing so myself in at least two projects.

Yeah, the presence of two approaches in the samples could well lead to some confusion, I fear.:/

For the main class, you’re probably correct–but it can still be useful elsewhere. For example, I sometimes use it as a quick way to gain access to the current level.

You could probably get away with not using globals by passing in references to the game just about everywhere–but that would likely become rather tiresome!

I think that the current recommended method is sub-classing ShowBase. If so, then it would probably help indeed for the various resources to stick consistently to that one method.

I just wanted to say in my post that newcomers are constantly stumbling over this.
Although I like to use import direct.directbase.DirectStart :slight_smile:

Fair enough, on both counts!

Myself, I don’t think that I use “DirectStart” at all these days–a quick search suggests that the only place that I have it is in old projects, made before I moved over to sub-classing from “ShowBase”. :slight_smile:

For the record, importing DirectStart is exactly equivalent to:

from direct.showbase.ShowBase import ShowBase

The ShowBase constructor writes base to the built-in scope.

Far be it from me to criticise anyone for their approach to programming, but I do personally avoid the use of globals as much as possible; I think it’s perfectly possible to avoid them by structuring your classes well. For example, you can pass in a reference to a World class to your WorldObject class, so that they can do operations with respect to the world they are in, parent themselves to the World’s root, etc.

I prefer to use functions, and it is not clear to me why it is necessary to create classes in Panda3D, at a time when this is not necessary. And the transfer of constantly referencing, sooner or later leads to a tangled tangle of threads. Sometimes it is easier to create a new code than to understand :slight_smile:

Using the built-in scope reduces many access problems and reduces the number of lines of code.

It’s possible, and I do prefer doing so in general, but it can become unwieldy at times, in my experience. That was why I started using a “Common” object that provides sort-of global access to a few commonly-used things.

I think that it really is a matter of what you prefer, and what works better for you. For myself, I find that classes fit well with the way that I think, and so make it easier for me to keep everything organised and (relatively) neat.

Thaumaturge: You may have to wait on your request for a example, because my script is mess right now as I,m still implementing the multiple scene function, but I will post it after I,m done though. though I think you may be right for separating functions into files. though, I,m worried about compiling, may I ask? how does panda compile games?

Do the python scripts retain their code form or are they compiled into machine code like C++, because from research, I found out that C++ does not retain it’s original form, and is compiled into machine code like assembly, so if I separated them into multiple files would the import functions fail because it is no longer python script?

So self, would need to be replaced with base then, thanks, I will use this in the future.

serega-kkz: I used showbase because I copied a lot of the roaming ralph demo’s code to jumpstart my game, I have seen some demo use Directstart, but from I can get of what you are saying is, showbase is more automatic? because you don’t have to add a method in front and just use self?

rdb: so showBase is automatic? I use global variables because they can be altered by functions, but also because they are referenced first for code needing to check for a variable before it is declared, I keep getting a “store your variables in a class” vibe, and it sounds I like a good idea, but I need to test out the class if it repeats or not first.

Hahah, fair enough! Of course, you could always put together a small script to demonstrate a specific issue–such as the example that I intend to request further down this post.

You don’t need to worry about that: your imports should continue to work perfectly well, I believe. (Again, I’ve made and distributed multi-file projects myself, and so speak from experience.)

As to whether it’s compiled… I’m a little shaky on the specifics of this myself, but ultimately it’s not something that you’re likely to have to worry about. Python deals with everything below the “source code” level, and it seems to work well.

I’ll leave a more-detailed description to someone who knows the internals better than I!

Note also, by the way, that while C++ is compiled into a much lower level, multi-file C++ programs still work after being compiled.

Class instances and local variables can be altered by functions, too–it just calls for giving the function a reference to the variable in question.

This really confuses me. What is it that you do with functions and global variables that you’re concerned that you can’t do with classes? Could you show us a bit of example code–even if it’s not a whole program–that demonstrates what you mean?


Honestly, if functional programming works better for you, then stick with it!

Myself, I find that classes work very well for me–but that may not be the case for you.

I’m not sure of how you might keep your variables organised in a functional approach to programming–I’m not as familiar with it.

I do encourage you to try out classes (i.e. “object-oriented programming”) if you haven’t previously, because you might find that they work for you. But if you have tried them, and they don’t fit well with the way that you think, then I daresay that you can stick with functional programming.

(With the minor caveat that you will encounter classes in using Panda, as Panda’s various elements are generally classes that you’re intended to create instances of.)

I think you expects the panda to create classes at each iteration of the loop. The answer will be negative if you did not explicitly specify this using the task manager or the for loop. It’s also easy to check use print.

But… the same would be true of a call to a function, would it not? The computer won’t do something that it’s not told to do, after all.

Unless they’re thinking of a function passed in to the task manager, being “repeated” by the task system–in which case one could just pass in a method belonging to the relevant class.

I’m very hesitant to speculate further, because I really don’t know what’s meant, and so fear that advice given under the wrong assumptions may mislead, or miss the point.

Thaumaturge: heh heh heh, well, I actually don’t fully remember why I went with global variables and why I think classes don’t repeat themselves, since I have been working on this 3 months straight, but it is notion I got somewhere, that is why it is untested (I test it tomorrow though, just been vary busy adding features)

I guess you can chalk it up to laziness on my part as I have been just going forward, I do apologize for confusion though, but classes do sound better for organization though. So should I just post some code bits in my posts? or do you want a script file?

serega-kkz: no, I mean the “return Task.cont” function that allows a task manger function to repeat endlessly.

Heh, fair enough! That can indeed happen, I think. :slight_smile:

Don’t worry about it! It can be easy sometimes for communication to become unclear in situations like this, I think.

However, thanks to your mention of “return Task.cont”, I think that I have a slightly better idea of what you’re getting at.

In that sense, you’re somewhat right: classes don’t “repeat themselves”–but that’s not really a problem.

(In fact, functions don’t generally repeat themselves, either: Panda’s task manager repeats them. Just as calling a function twice in your own code “repeats” it, Panda’s task manager “repeats” the functions given to it as long as you tell it to via return “Task.cont”, I believe.)

A way of thinking about classes and functions that I recall being taught, and that may prove useful to you is this: classes are essentially “nouns”, or “objects”, while functions are “verbs”, or “actions”. An object can perform an action, and just so, a class can have functions. Calling a function belonging to an instance of a class is a bit like having that object perform that action.

Thinking about it this way, one doesn’t generally “repeat” an “object”–but an object can “repeat” an “action”.

So, you wouldn’t have the task manager “repeat” an instance of a class–instead, you’d have it “repeat” a function belonging to an instance of a class.

For example:
(It’s possible that I’ve made some mistakes below, as I’m not checking that it runs. My apologies if so!)

class Cat():
    def __init__(self, name): = name
        self.cuteness = 5000
    def meow(self, target):
        print target, "hears",, "meow."

class Game(ShowBase):
    def __init__(self):
        # <Normal initialisation here>
        # Now, make a "Cat", and add an update task = Cat("socks")
        self.taskMgr.add(self.update, "update task")
    def update(self, task):"Thaumaturge")
myGame = Game()

The result would be Panda printing out a rapid stream of repeated “Thaumaturge hears Socks meow”, I believe.

Thaumaturge: Hmm, so separating features into classes would be the better way? like if I had the environment related functions (including the related global variables) into their own class from the character functions and variables? if so, that does so good, but I lack any syntax experience to perform such a task, which is why I want to take Udamy class on python.

You are right though about the class’s non repeating nature (as well as the function), it’s the “return Task.cont” code that is doing the repeating, as it turns out that I was embarrassedly wrong about classes, because I just tested it out today, and global and class variables stay still, so the class does not update, and was a irrational fear that I got from somewhere.

And I,m sorry I got you mixed up in this weird notion got, I’ll try to be more vigilant about panda3d features in the future, also thanks for the insight, it will hopefully lead to better code.

Seriously, don’t worry about it! You misapprehended–I daresay that most, perhaps all programmers have done so at some point.

I don’t know that I’d universalise so much as to call it outright better. I think that it’s better for me–but that doesn’t mean that it’s necessarily better for you. Look into it, see whether it works for you, and if so, go with it. If not, find a way that does work for you.

That seems like a reasonable pair of classes, yes. Exactly what classes you come up with might vary depending on what makes sense to you, and on the game itself. In some games I’ve had several “layers” of character-related classes; in others I’ve had just one or two.

Fair enough! I don’t know Udamy, so I’m not in a position to speak to their quality, but if they’re good, then that seems to make sense. There may be other resources out there, too.

Well, I’m glad that you did try that experiment, and gained an answer from it, then. :slight_smile:

Good luck with your continued learning! :slight_smile:

Thanks, I,m learning a lot, which is good as it can assist me in the future.

Well, finally after a week and half of work, and with the help of everyone on this thread, I have created the “parsing class”, a special class that can take “made up” code from a txt file and turn it into usable room data.

Sadly, Thaumaturge, I cannot upload the demo as per your request as it is too big, even when I remove the audio, it’s weighing a 3.3 MB at that, and this site reports only a 3MB max, anyways, thank you, rdb and Thaumaturge for helping me form the class, and thank you serega-kkz for the demo as it helped me greatly for the parsing logic of that class that I made.

I couldn’t have done this without you guys, and with that being said, this issue has now been solved.

I’m glad that you seem to have come up with something that works for you! Good luck with the rest of your project–I hope that it goes well. :slight_smile:

thanks, it is hard work, but exciting when it comes together.

If you’re interested in sharing your project you should throw it up on git! Even if you’re not, if you’re not using version control right now I’d highly recommend putting it in a private repo.

Don’t you need a account with them?, also are your suggesting compiled version, sorry for asking, but what is a private repo?