# Run a script only once in BGE

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?

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

## 2 Answers

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.

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.

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'].

• So if I don't click on one of those three dots, it will only run once? Thanks! – Anson Savage Apr 24 '15 at 19:35
• @AnsonSavage yep, by default the Always sensor only runs once. – David Apr 24 '15 at 19:38
• 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! – Anson Savage Apr 25 '15 at 2:29

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?

• 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. – someonewithpc Apr 20 '15 at 21:46
• This is currently what I'm doing. Thank you for your input. – Anson Savage Apr 20 '15 at 22:53
• @AnsonSavage what is the drawback of this method (using a variable ) ? do you have a mush more complicated case ? – Chebhou Apr 21 '15 at 0:23
• 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! – Anson Savage Apr 21 '15 at 1:33
• 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. – beiller Apr 23 '15 at 16:37