Text scripting relying on another object property

I’m trying to make certain text in my game to change dynamically depending on variables.
For example:
I have a switch that turns a signal on and off. Whenever it’s on, I want the text to say “ON”, and when off, I want it to say “OFF.”
My signal is toggled through a boolean property. Would it be easier/smarter to make my text change via a script that refers to the switch’s property? If so, how would I go about that. I’ve tried methods and failed. Could have been missing something or messed up the script. Otherwise, I’m open to any option (scripting preferred though).

If any clarification is needed, feel free to ask questions.

always sensor pulse mode


import bge


scene=bge.logic.getCurrentScene()


switch_prop=scene.objects["Cube"]["prop"] #boolean
text_on_off=scene.objects["Text"] 


if switch_prop==True:
    text_on_off.text="on"
else:
    text_on_off.text="off"

you can use a function, or write a similar code in the text controller, its up to you

you can even set the text via a property (text objects have a special property see game properties panel).

I haven’t tried either of those. I have tried using a property with no avail but I’ll look more into it. I could have been doing something wrong.

I’ve also thought of using a message logic block on the switch and send a message to the text which when it gets the message it changes the property to True or False. Could that work as well possibly?

You can use a message notify the text object when to copy the value (from foreign property to the own text property). This requires the text object to know the other object.

Another way is to send the text as message. Due to it’s dynamic behavior you need Python to do that. Have a look at the BGE Guide to Messages incl. Healthbar tutorial for more details.

This worked perfectly! Thank you very much!