Turning a message body into a usable variable with python?

Hi every one.
I can get a list of bodies recieved and everything, but blender won’t let me assign the one I want to a variable. It just says ‘CListObject’ is not callable. How can I assign the body value to a variable and not get this error, ie, have it work? Here is the script I have written so far. If you want the .blend let me know and I will post it.

cont = GameLogic.getCurrentController()
own = cont.getOwner()
gold = own.Gold

Get Sensors

earnGold = cont.sensors[‘goldGain’]
spendGold = cont.sensors[‘goldLose’]

#Get Actuators
newGold = cont.actuators[‘GoldAdjust’]
gainList = earnGold.bodies()
gain = gainList.get(Gold)
print(gain)

Oh, and if there is a easier and more effective way to setting this up, could you point in the right direction?:smiley:

First, you’re mixing in some deprecated code here in the beginning


own = cont.owner
gold = own['Gold']

The error is because bodies is not a function but an attribute


gainList = earnGold.bodies

The next line doesn’t make much sense for me but I assume you most likely want to read the body value into an integer variable.

The important thing here is to check that the right message sensor is positive so there are message bodies in body list or you’ll end up accessing empty list index when sensor sends negative pulse. Also bodies are strings which you need to convert into integers.


if earnGold.positive:
    gain = int(earnGold.bodies[0])
    <i># do something #</i>

you can also use the firstBodyToValue() function of the S2A library. See http://blenderartists.org/forum/showthread.php?t=184214.

I hope it helps

Thanks alot! That really helped me.
@supersocks, I used your line of code, and it works perfectly, but my troubles are not over…
I posted that post late last night when I was tired. I forgot to say that I wanted to take the ‘gain’ variable and assign it to the value of a property actuator to adjust the value of the object’s property. I was doing it like this;
earnGold.setValue(gain)
Doesn’t seem to work? Is there another way to directly adjust the property value of the object without using a property actuator?
@Monster, thank you for the S2A library, I am trying to figure out how to use it though… I am a really raw noobie at programing.

Adds gain to property ‘Gold’ assuming the property is in same object the Python controller.

own['Gold'] += gain

If you still need to use the actuator, remember to activate it.

cont.activate('GoldAdjust')

Thank you very much, every one. I think that should do it! Again, thanks for all your input!

Hi Gyis,

no programming needed:

  • load the S2A.py into the internal texteditor or your Blenderfolder.
  • add a message sensor
  • add a python controller in module mode with S2A.firstBodyToValue
  • add a property actuator configure the property you which to receive the value.
  • connect all three bricks

Thats all

Okay. I will try it sometime. Thank you, Monster.