GE Python Speed

Is BGE’s python processing frequency bottlenecked by the framerate? For example, if I’m running the game at 60fps, can I only run a script 60 times per second?

I use much python in my game, and the game run at 60fps, so, some scripts are executed 60 times per second, and the logic not amount much, all depend of the methods of you use.

In this moment i have like 5% of logic with 5 scripts(some with more than 100 lines), but why that not slowdown? because you need to make the parts of the scripts only execute when is necessary.

For example, if you are making a grab system, not used a always sensor with a long script.

Use a ray sensor(not triggering!) with a script that get the object front to the camera, so the script only execute 2 times when the player is front to a object.

For get if you are pressing a key of the mouse, use a mouse sensor, but in the script use “events” because with this you can get is the button is pressed(etc) and the script only exucte 2 times when the mouse or the ray sensor is activate, if you use in a loop if the event “just pressed” that part of the script is execute one time.

Use modules, the ray sensor use the module a script for store the object in a property, the mouse sensor use another module of the last script for get if is there a object stored in the property…etc.

(all that withot triggering!)

i don’t know if i explain well…

Take a look on the structure of your code. Bad strructured script or script with very much nested loops could make performance go down quickly if the time for script execution is greater than 1/60 seconds.

I wish to know… Why would you want to execute a script more than 60 times in one second?

I think you are talking about the double trigger the sensors make, but this double execution can be avoided using the SENSOR.positive attribute.

The controller that executes your Python script can only be triggered once per frame. However, you can execute your code as many times as you’d like by using loops. Just be aware that your game can’t continue to the next frame until the execution of your script is finished, so scripts that take a long time may cause a performance drop.

To answer your question simply: yes, script invocations are tied to framerate. This is not that great of a design, but we’re dealing with a pretty old engine here. If you want to attempt to work around this, you could try something with threads or multiprocessing.

In general, when writing scripts, you should try to make sure to base calculations on a change in time (usually known as delta time or dt). For example, with character movement, instead of moving a character X units every run of your movement script, you should say a character can move Y units per second and then move a character Y*dt units per script execution.