0
$\begingroup$

so I'm relatively new to Python in BGE (and just Python in general, really.) and I'm trying to make a script that changes the maximum integer on a random actuator. I hovered over it, and it said to type "int_max" but I got an error.

    import bge
    from bge import logic as l

    cont = l.getCurrentController()
    own = cont.owner

    Dif = own['DIF']
    Timer = (19 - (Dif * 3))
    Rand = cont.actuators['Random']

    if cont.sensors['Always'].positive and Dif != 0:
        Rand.int_max = Timer
    else:
        if Dif == 0:
            Rand.int_max = 

I have no idea how to change the maximum integer for the actuator, what the attribute is called. Does anyone know how I could change it, or if my script is wrong? And is there any way for me to check what the attribute is named for an actuator/sensor? Thanks a bunch! (If you need more info I'm happy to oblige)

$\endgroup$

1 Answer 1

0
$\begingroup$

Without knowing the error it's hard to guess, but I think it's caused by a syntax error in the last line. If you don't set a value for Rand.int_max, python won't execute this script. To get detailed information about the error, you can open the Blender Console (Window > Toggle System Console).

So instead of Rand.int_max = write Rand.int_max = [some integer].

But, you can't set int_max directly via BGE Python. Instead you have to use a call to Rand.setIntUniform(lower_bound, upper_bound)(API). lower_bound is int_min, upper_bound int_max.

A few other things you could improve:

  • Timers are actually floats, but Rand.setIntUniform() requires an Integer, so put the timer value into int().
  • Instead of else: if Dif == 0: you can write elif Dif == 0: (or just else: if Dif is never negative).
  • If you only need the logic module and no other bge modules, you can delete the first line, you only need the second line (from bge import logic as l).
  • Variable names in python are in snake_case by convention, so don't call your variables Dif or Rand, call them dif or rand instead. But this is a matter of taste, your code is working the same regardless of the naming conventions. The Blender API calls (like getCurrentController() are in CamelCase because Blender is written in C and C++ and these languages use CamelCase.
  • If you want to activate (= execute) the Random Actuator after setting the value, add cont.activate(rand)(API) to your script. Other than the other Controller types, a Python Controller doesn't execute all connected Actuators by default.

So here is a version of your code that should work:

from bge import logic as l

cont = l.getCurrentController()
own = cont.owner

dif = own['DIF']
timer = int((19 - (dif * 3))) # Convert the result to int
rand = cont.actuators['Random']

if cont.sensors['Always'].positive and dif != 0:
    rand.setIntUniform(0, timer)

# just 'else' if dif can't be negative
elif dif == 0:
    rand.setIntUniform(0, 0)

# If you want to execute the Actuator
cont.activate(rand)
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .