pls help in python

i got a python problem… dont know where is here the problem. did my self this python, but it has somewhere mistake. i am still very weak on python, probably i dont know some python tricks… if i do without randoms, it goes okay. but with random it is wrong somewhere… please check this:


import random
cont = GameLogic.getCurrentController()
own = cont.owner
sensor2 = cont.sensors['sensor2']
scene = GameLogic.getCurrentScene()
objects = scene.objects
player = objects['OBplayer']
x = 0.1*(100-((player['evasion'])/(own['ontaget'])*100)) #problem can be here
if sensor2.positive:
 own.endObject()
 if own['damage'] < player['defence']:
  own['damage'] = 0
  own['Damage'] = 1 #it does so, that my real damage would go back after 0.1 second
 else:
  if player['evasion'] < own['ontarget']: 
   if random.randint(1,str(x)) == 1 :   #problem can be here
    own['damage'] = 0
    own['Damage'] = 1
   else:
    player['health'] -= own['damage'] - player['defence']
    
  if player['evasion'] > own['ontarget']: 
  own['damage'] = 0
  own['Damage'] = 1

why for me this doesnt work? here is everything clear - i think i wrote right… but doesnt work. checked everything - have all propertys added right… maybe i wrote bad variable?
pls help - where is the problem here?

do you get any error messages or something , can you post a simple blend file?

There’s this thing called the “console” which displays error messages and the line of the error of Python scripts. It’s that little window that launches with Blender.

On the off-chance that you’re running on a Mac (and getting the console to show in that case is harder), I have to ask… what are you doing here:


random.randint(1,str(x))

The str(x) command converts another object into a string… ie str(5) will return “5” (a string), and it will cease to be an integer (if you were to do x = 2x, you’d get “55” instead of 10.) The randint function takes two INTEGERS, not a string. It should be random.randint(1,x).

Also, in one place you have own[‘ontarget’] and in another place you have own[‘ontaget’] (no R). That’s probably another problem.

i changed it to simpler without variables, but it still doesnt work…


import random
cont = GameLogic.getCurrentController()
own = cont.owner
sensor2 = cont.sensors['sensor2']
scene = GameLogic.getCurrentScene()
objects = scene.objects
player = objects['OBplayer']
if sensor2.positive:
 own.endObject()
 if own['damage'] < player['defence']:
  own['damage'] = 0
  own['Damage'] = 1 #it does so, that my real damage would go back after 0.1 second
 else:
  if player['evasion'] < own['ontarget']:
   if random.randint(1,5) == 1:
    own['damage'] = 0
    own['Damage'] = 1
  else:
            player['health'] -= own['damage'] - player['defence']
       
 if player['evasion'] > own['ontarget']:
  
  own['damage'] = 0
  own['Damage'] = 1

in my console says something is wrong with
player[‘health’] -= own[‘damage’] - player[‘defence’]
but if i have no randoms, it goes okay.
and in console says another one (dont know or from this python)
IndentationError: unindent does not mach any outer indentation level

found out -problem was spaces and tabs . i have copied pasted half of formula and not curently spaced.


import random
cont = GameLogic.getCurrentController()
own = cont.owner
sensor2 = cont.sensors['sensor2']
scene = GameLogic.getCurrentScene()
objects = scene.objects
player = objects['OBplayer']
x = 0.1*(100-((player['evasion'])/(own['ontarget'])*100))
if sensor2.positive:
 
 own.endObject()
 if own['damage'] < player['defence']:
  own['damage'] = 0
  own['Damage'] = 1
 
 else:
  if player['evasion'] < own['ontarget']:
   if random.randint (1,x) == 1:
    own['damage'] = 0
    own['Damage'] = 1
   else:
    player['health'] -= own['damage'] - player['defence']
  else:
   own['damage'] = 0
   own['Damage'] = 1

    

this works now.
thank you for support, i have found out other problems.