7
$\begingroup$

I have a script that I only need to run once. What I usually do for startup scripts is an Always > Tap Sensor. This python script is being activated by a message sensor with the tap setting enabled. Running this script multiple times will cause problems, so I used a variable system to prevent this from happening.

Is there anyway to stop the script from running more than once in the first place, using logic bricks?

$\endgroup$
  • 4
    $\begingroup$ with an accountant, or a property becomes false, or a value that compares if a property is of particular value $\endgroup$ – yhoyo Apr 13 '15 at 22:59
  • 1
    $\begingroup$ Can you please expound on this comment? $\endgroup$ – Anson Savage Apr 14 '15 at 2:35
8
+50
$\begingroup$

For a script that only runs once, say a setup script, you can use an Always Sensor without True level triggering (the buttons outlined in red in the image below). Doing so will unintuitively to the Always Sensor's name will only run once.
always sensor


As for scripts where you only want a function to run once, but the rest constantly evaluated, I would highly recommend using The Python Controller in Module mode.
Python Controller in Module mode
The way it works is you pass in the name of your script (must have a .py), then the name of the function. So in the picture my script is named script.py and the function I am calling is functionName.


The final way is to use a variable, or property.

import bge  

cont = bge.logic.getCurrentController()
own = cont.owner

if 'init' not in own: # Will only run once, or when the var gets removed
    own['init'] = True
    print('only once')

Then if you need that code to run again use this line del own['init'].

$\endgroup$
  • $\begingroup$ So if I don't click on one of those three dots, it will only run once? Thanks! $\endgroup$ – Anson Savage Apr 24 '15 at 19:35
  • 1
    $\begingroup$ @AnsonSavage yep, by default the Always sensor only runs once. $\endgroup$ – David Apr 24 '15 at 19:38
  • $\begingroup$ Oh, so if I don't click on the three dots on my message sensor, the python controller that is connected will only be run one time? Nice! $\endgroup$ – Anson Savage Apr 25 '15 at 2:29
4
$\begingroup$

What do you think about setting a static variable, like a Property, in Blender at the end of the Script that prevents it from running again ?

For more Details see: How to Set a Property Value in Blender Game Engine?

$\endgroup$
  • 2
    $\begingroup$ Please avoid link only answers; cite the relevant parts of the link so your answer can be understood without following the link, and add the link at the end for further information. $\endgroup$ – someonewithpc Apr 20 '15 at 21:46
  • $\begingroup$ This is currently what I'm doing. Thank you for your input. $\endgroup$ – Anson Savage Apr 20 '15 at 22:53
  • 1
    $\begingroup$ @AnsonSavage what is the drawback of this method (using a variable ) ? do you have a mush more complicated case ? $\endgroup$ – Chebhou Apr 21 '15 at 0:23
  • $\begingroup$ There's not a problem with it, I was just expecting the tap option to make it only run one time. This question really isn't that important, I just put bounty on it just to see how much publicity it gets. Thanks! $\endgroup$ – Anson Savage Apr 21 '15 at 1:33
  • 1
    $\begingroup$ Using a variable (IE named init, boolean) and check it on start up, and set it to true when complete, is the de-facto method to run once. I've seen this method used in many tutorials and used it myself. $\endgroup$ – beiller Apr 23 '15 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.