Int Object Not Callable in Python

Here is a simplified example of my situation…

i have 28 objects in layer 2. all are named 1-28 accordingly (each object name is just a number between 1 and 28

i have an empty in layer 1

that empty has a property for adding objects

the empty also has a property sensor set to “changed”

when property is 1 i want it to add object 1…
when property is 2 i want it to add object 2…etc

instead of having lots of logic bricks with all of the objects and property values, i have made a script…

 
 
cont = GameLogic.getCurrentController()
 
addobj = cont.getActuator("addobj")
prop = cont.getSensor("prop")
 
propvalue = prop.getValue()
 
if propvalue<29():
     addobj.setObject(propvalue)
     GameLogic.addActiveActuator(addobj, 1)

when i start the game, the dos displays the error "TypeError: ‘int’ object is not callable

im pretty that this means that the value of propvalue is an integer. and you cannot call an object by using an integer? am i right?

so is there a way to call an object with the value of propvalue? i’m not quite sure how to make the script call the object by name, not integer.

i understand that this may be a little vague, but i tried to explain it as well as i could.

i would appreciate any help. thanks:D

The object names are strings, not integers.

You need to convert this property from integer form, into string form.
A good way to do this is the repr() built in function.

The code below should fix your problems.


cont = GameLogic.getCurrentController()
 
addobj = cont.getActuator("addobj")
prop = cont.getSensor("prop")
 
propvalue = prop.getValue()
 
if propvalue<29():
        objname = repr(propvalue) # This line is added.
        addobj.setObject(objname) # This line is modified.
        GameLogic.addActiveActuator(addobj, 1)

Edit: Also, I feel like I should add that there is an easier way of getting and manipulating a property.


con = GameLogic.getCurrentController()
own = con.getOwner() # This is the object that the controller is on.

# A property named "myProp" can be accessed
# as own.myProp, as shown below

own.myProp = 8    # setting myProp to 8
print own.myProp    # printing out the property.

thanks. the repr function works great.

i don’t know why, but for it to work, i have to get rid of if propvalue<29():

it’s not a big deal. i know a workaround.

but thanks for teaching me the repr function!:smiley:

ya. i don’t think i was really thinking straight when i started this. i swapped out the property sensor for own.prop in the script. whatever. i fixed that and it’s working fine now

The int not callable thing is because you have a () after 29, like you would have for a function…

Replace the line with ‘if propvalue < 29:’ and it will fix that error.


cont = GameLogic.getCurrentController()

if own.propertyname &lt; 29:
    addobj = cont.getActuator("addobj")
    addobj.setObject(str(own.propertyname))
    GameLogic.addActiveActuator(addobj, 1)

There is how I would write the script, I moved the addobj = cont.getActuator(“addobj”) line after the if: because if the property is over 29 it will just be a waste of getting the actuator and storing it in a variable so it will have a small (unnoticible) performance increase but if you have enough of those unnoticible increases they will become noticible :slight_smile:

And you can use the str() function to turn an int or a float into a string, same as there is an int() function to turn a float or a string into an int and there is also a float() function, guess what that does.

Replace own.propertyname with whatever the name of the property is and with this there is no reason to even get the sensor so that will be another small performance increase.

I’m not sure wether repr() or str() is faster but str() seems more widely used.
I hope some if this helped even if this question was already answered… :slight_smile:

Nice scabootssca, I didn’t notice that :smiley:

I believe repr is faster then str, because str checks if the given data is already a string. repr on the other hand, will “string-ify” a string if you tell it to.

For example:

word = "hello"
print str(word)
print repr(word)
### OUTPUT ###
hello
'hello'

As you can see, repr will add quotations to a string, whereas str will not.