Python Script Problem!

Hey, I’ve made a script on a game that determines if the “hero” or the “Enemy” is going to play next (depending on their speed values), in order to take quick results to check if its working ive made it like that, if it’s time for Hero’s turn it will print ‘0’ on the console, if its enemy’s turn it will print ‘1’.

I’ve set this script to run everytime it receives a specific message
but the problem is that when I see the console (the same time when I run the game) it prints 2 numbers at the same time…
I’ve checked it so many times I have even tried making a property that it turns false when it activates (to check it activates only once) its driving me crazy!!!

if you have an answer until now plz help! if not im going to go in details:

this is the script:

#Imports
import random

#Sortcuts
cont = GameLogic.getCurrentController()
obj = cont.owner

#Definitions
def HeroTurn():
    obj['HeroTurn'] = obj['HeroTurn'] - obj['LowerSpeed']
    cont.activate("Hero Turn Msg")
    print 0
    
def EnemyTurn():
    obj['EnemyTurn'] = obj['EnemyTurn'] - obj['LowerSpeed']
    cont.activate("Enemy Turn Msg")
    print 1
    
#Basic Program
##Recalculate LowerSpeed

if obj['HeroSpeed'] < obj['EnemySpeed']:
    obj['LowerSpeed'] = obj['HeroSpeed']
elif obj['HeroSpeed'] > obj['EnemySpeed']:
    obj['LowerSpeed'] = obj['EnemySpeed']
elif obj['HeroSpeed'] == obj['EnemySpeed']:
    obj['LowerSpeed'] = obj['HeroSpeed']
    
##Recharge HeroTurn and EnemyTurn

if obj['HeroTurn'] < obj['LowerSpeed'] and obj['EnemyTurn'] < obj['LowerSpeed']:
    obj['HeroTurn'] = obj['HeroTurn'] + obj['HeroSpeed']
    obj['EnemyTurn'] = obj['EnemyTurn'] + obj['EnemySpeed']
    
##Turn Calculation

if obj['HeroTurn'] > obj['EnemyTurn']:
    HeroTurn()
elif obj['HeroTurn'] < obj['EnemyTurn']:
    EnemyTurn()
elif obj['HeroTurn'] == obj['EnemyTurn']:
    Turn = random.randint(0,1)
    if Turn == 0:
        HeroTurn()
    elif Turn == 1:
        EnemyTurn()

the values HeroSpeed and EnemyTurn are always positive (never negative or zero)
this script activates everytime it receives a message , I tried making it like it will activate every X seconds (with delay sensor) and it works normal printing one number every some seconds, but when I set it for message it works wrong…

am I doing something wrong?
if you need more info plz tell me !!!

classic mistake :slight_smile:
some sensors let your script run when they turn True, but also when they turn False, thats why your script runs twice.
simple way to solve this is to check if the sensor is True:


if cont.sensors["name_sensor"].positive: 
     your code

with the always sensor you shouldn`t have this problem

thx a lot it worked ;D

Code simplification:


if obj['HeroSpeed'] < obj['EnemySpeed']:
    obj['LowerSpeed'] = obj['HeroSpeed']
elif obj['HeroSpeed'] > obj['EnemySpeed']:
    obj['LowerSpeed'] = obj['EnemySpeed']
elif obj['HeroSpeed'] == obj['EnemySpeed']:
    obj['LowerSpeed'] = obj['HeroSpeed']

to


obj['LowerSpeed'] = min(obj['HeroSpeed'], obj['EnemySpeed'])

and


if obj['HeroTurn'] > obj['EnemyTurn']:
    HeroTurn()
elif obj['HeroTurn'] < obj['EnemyTurn']:
    EnemyTurn()
elif obj['HeroTurn'] == obj['EnemyTurn']:
    Turn = random.randint(0,1)
    if Turn == 0:
        HeroTurn()
    elif Turn == 1:
        EnemyTurn()

to


if obj['HeroTurn'] == obj['EnemyTurn']:
  turn = random.randint(0,1)
else
  turn = obj['HeroTurn'] > obj['EnemyTurn']
 
if turn:
 HeroTurn()
else:
 EnemyTurn()

just for fun :wink: