Python related questions

Hello everyone! I hope you’re all doing a fantastic day! :slight_smile:

I’m currently building a game and as usual, we encounter some new stuff that I need help figuring out. I hope you could help me with this.

I’ll write down the questions that I have on this single thread. Please tell me if this is unappropriate or I could make a every single thread for each questions. :smiley:

PLEASE NOTE: I’m trying to do this all in PYTHON.

  1. How can I make a text print a property of another object?
  2. Would it be possible to make a single .py file to the whole blender project? and just use every single module to every object?
  3. How can I make an int property +15 property every 1 second?
  4. I’m using Geany as my IDE. Would it be possible to auto update any changes from a 3rd party IDE?
  5. Ending an object after the timer ends?

Thank you very much guys! This forum is awesome! Take care!

  • Chris
  1. Get the property using other_obj[‘property’] You can get the other object using it’s name and scene.objects[‘other_object_name’] Write to the text property using font_object.text = “This is the text”

  2. Not quite sure what you mean, but in CaveX16 I had a single Always->Python logic setup and everything else was python modules. If you’re clever you can drop even those logic bricks and use the main property of the scene to take control of the gameloop, but things get complex fast.

  3. You could use python’s time module, you could assume a framerate of 60 and count frames or anything else really. I tend to use the time module, and write some sort of scheduler from that.

  4. I use Geany as my IDE as well. If you’re meaning to update files inside of blender from Geany, then simply don’t have the python modules open in blender. BGE checks the folder of the current blend file for python modules. Then those python modules can be edited and whatnot without needing to reload them from within blender.

  5. When the timer ends, run obj.endObject() See #3 for how to manage time amd #1 for how to get objects.

I can go into more detail on any of these should you require it.

Hey! Thanks for this! I sent you a message.

Cheers! :slight_smile:

The contents of the message are probably better to be mentioned to the community, so I’ve posted it here:

I still do have few questions regarding the timer, IDE and one file script though.

How can I code it with timer? I’m still getting into coding and I’m still not that familiar with those complicated stuff.

with Geany. I just reload the script by ALT+R everytime I do changes. Is that how you do it?

lastly, I know it’s really complicated but can you give me just the main concept behind one file scripting? if that’s even possible. lol

Let’s say we want to know the time between two events. One frame we can do:


start_time = time.time() #Alternatively datetime.now() depending on what time module you are working with

And then later we can do:


end_time = time.time()
duration = end_time - start_time

So to have something that runs every second, you may do:


start_time = time.time() # Run this at some point in the past
....
...
current_time = time.time()
if end_time - start_time > time_in_seconds:
    do_something()

Why are you reloading things frow within geany? What is changing them? In my setup, Geany is making the changes, and the BGE has to read those changes. So what’s doing the modification of the files?

***Ho boy, this is a biggie. Have you ever worked with embedded systems? Ever worked with a RTOS? If you have, some things here may be a little easier.

Pretty much, if we have a single python script running every frame, we have to have the script do everything. Except that is madness, so we split it into parts. Modules that handle subsections. These can be imported using the python import statement.
But what goes into this one file? In the case of my latest project CaveX16, it is the file you see here. Pretty much there are three things it does:

  • Initialize the game
  • Update the load sequence
  • Update the actual game

So by initialize the game, I mean things that are major subsystems: Adding the scheduling system, adding the sound management system etc.
By “loading the system” I mean loading models, loading the sounds themselves, generating the level etc. Some things in the load sequence add themselves to the scheduler to be run at regular intervals at a later time.
Then in running the game, it goes through the items in the scheduler and runs thost when they need it.

So for instance, a game unit:

  • Has the model loaded in part 2
  • Adds itself to the scheduler in part 2
  • Updates it’s AI every frame by being run from the scheduler run in part 3

The core part of such a system is the scheduler, which for CaveX16 can be seen here. If you aren’t familiar with object oriented programming it may look a little strange. The instalce of the scheudler created in Game.py I linked to earlier.

So there you go, there’s an overview of how this was set up in one of my recent projects.