Update property?

My inventory script that I am making goes like this:
Message +++++ Python Module [main function] (see below) +++++ Property Changer


import bge


cont = bge.logic.getCurrentController()
own = cont.owner


#Properties
ballN = own.get("ballN")
ball2N = own.get("ball2N")


#Sensors
act = cont.sensors["act"]


#Sensor Properties
mess = act.bodies[-1]


#Actuators
propC = cont.actuators["propC"]


#Functions
def main():
    if mess == "ball":
        propC.propName = "ballN"
        cont.activate(propC)
        
    elif mess == "ball2":
        propC.propName = "ball2N"
        cont.activate(propC)
    
    else:
        pass

The mess property never updates itself, so the propC always stays the same as the very first item that was collected. Does anyone know of a way to update this before the message bodies are checked by the main function?

If Message.bodies[0]==“test”:
Do stuff

I think tests the current trigger of the message sensor.

Bodies[-1] is the last item on the list, not the most recent

So you want

message=act.bodies[0]
If message==“ball”

It still doesn’t update. The variable is set after the first message is received.

May I see a .blend?

are you firing the property actuator besides when changing it?

I can’t update property’s value too.
I creat two property name “oldx” “oldy”
My code:

import bge

my_mouse = bge.logic.mouse
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
JUST_ACTIVE= bge.logic.KX_INPUT_ACTIVE
co = bge.logic.getCurrentController()
obj = co.owner

oldx=obj.get(“oldx”)
oldy=obj.get(“oldy”)
print(oldx)
if my_mouse.events[bge.events.LEFTMOUSE] == JUST_ACTIVATED:
oldx=bge.render.getWindowWidth()*my_mouse.position[0]
oldy=bge.render.getWindowWidth()*my_mouse.position[1]

print(oldx)

if my_mouse.events[bge.events.LEFTMOUSE] == JUST_ACTIVE:
bge.render.setMousePosition(int(oldx),int(oldy))

always>>>python

You ask a similar thing in your other thread. As written, I do not know why you want to set propName.

propName is the attribute that defines the property name of the actuator. This is equal to writing a value into the “property” field in the GUI.

I have the impression, you want to change the value of the already set property. To do that (with a property actuator indeed) Write the value as string into the “value” attribute:
bge.types.SCA_PropertyActuator.value.

As mentioned in the other thread, if you want to get the name of the hit object write its name into this attribute and activate the acutator.


def hitObjectNameToValue(controller):
  hitObject = controller.sensors[0].hitObject
  if not hitObject:
    return

  actuator = controller.actuators[0]
  actuator.value = hitObject.name
  controller.activate(actuator)

I am thinking he wants to change the object a copy property is targeting,

The API does not support that. The PropertyActuator and the PropertySensor both have an incomplete API. This usually does not matter that much as you can set properties without the actuator. But you have to give away the option to configure other parameters via GUI (such as configuring the property name).

@Joystik Studios: Unfortunately I do not understand what you want to do with this code. The choice of variable and function names doe not let me guess what they are supposed to do or contain. Can you describe what should happen when (maybe with a why)?

I want to have only on property actuator like so:

Message +++++ Python +++++ Property

The python script is to determine the body of the most recent message received and put that into the property that the Property actuator changes. Does that make sense?

It makes sense. S2A.firstBodyToValue performs a similar operation.

But there are some things to explain a little further:

How you define that? The messages come in without a specific order. Your idea of processing the last message has the same correctness as my version using the fist message.

Be aware a property consists of a name and a value. If you put a value into a property it means you write it into the value. You identify the property by its name.

If you look at the Property actuator you see there is a Name field (labeled “Property”; attribute propName) and a Value field (named Value; value attribute. If you want to set the value you write the new value into the value field and activate the actuator. hen activated the property actuator it will identify the property of its owner with the given name. If the actuator is in assign mode it writes whatever value is in the value field into the found property. Be aware the value field will be interpreted. If you want to assign a string you need to enter “string” including the ".

That’s what I mean, the message body is the property name. The most recent message received should go like this:
Player collects a ball. ‘Ball’ is now the most recent message body, and the script adds 1 to the property ‘Ball’. All the messages have the same subject.
Player collects a cube. ‘Cube’ is now the most recent message body, and the script adds 1 to the property ‘Cube’.
Player collects a ball again. ‘Ball’ is now the most recent message body, and the script adds 1 to the property ‘Cube’.

That is how I want it to work, using one property actuator and one message sensor.

Why not just set the property with python?

Target[‘property’]=Target[‘property’]+1

The property that the actuator changes must be dynamically allocated by the python script based upon the body of the message that was last received, lowering the amount of logic bricks and simplifying the system. Also I don’t understand the code you gave me, BluePrintRandom. I am setting the property with python, but trying to keep the amount of properties and bricks at a minimum.

Ok

MessageBody=message.bodies[0]
If MessageBody==“test 1”: object[‘Property’]=object[‘Property’]+1

If MessageBody=“test 2”: object[‘Property’]=object[‘Property’]+2

You dont need a property actuator,

But these properties are most likely going to be accessed by another script. There is probably a solution for this but it’s not what I want.
Also that does not solve my problem that the message.bodies[0] never updates. This is the only thing that I need fixed.

Check this out

it’s setting properties in the item, any item can grab them

w = add 1

s = add 10

:smiley:

Attachments

MessageCounterWithBodies.blend (441 KB)

I wonder why you want to send property names around. It looks like you want to send categorized information.

Due to the dynamic characteristics of an inventory it makes sense to implement in Python.

Just now you communicate via messages, but it looks like you are skipping messages (you deal with the first message only).

You shoud think about how your inventory should work. This means where to store the inventory content, ho to put additional content, how to get content from the inventory etc.

tada, receives multiple messages

now w sends 2 messages in 1 frame

Attachments

MultiMessageCounterWithBodies.blend (455 KB)

oops wrong file one sec


import bge




def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner


    message = cont.sensors['Message']
   


    if message.positive:
        for items in message.bodies:
            if items=="Test": own['Counter']=own['Counter']+1
            if items=="Test2": own['Counter']=own['Counter']+10
               


main()



Attachments

MessageCounterMultiMessages.blend (455 KB)