0
$\begingroup$

Is there a way to set an object property using the return value of a module function like this?

enter image description here

I'm sure the module works because I can catch the final "value" of the function with that print() in the console.

It works if I put this line instead of the return:

bge.logic.getCurrentController().owner["Text"] = value

but I would like not use it this way.

$\endgroup$
3
$\begingroup$

No, the module functions in BGE work without returning values, and you can't set property values that way.

The basic structure of a module function in BGE is:

import bge

# cont is a reference to the controller, similar to bge.logic.getCurrentController()
def functionName(cont):
    own = cont.owner

To set property values, you don't need property actuators, just use the syntax:

object["property"] =  value

You said you don't want to use that syntax, but along with actuators, it's the only way to set object properties. You don't need to add the property through Blender interface, just set it through the code and it will automatically be added. Also, it works not only for game objects but for scenes as well, and they can store not only the basic types (int, float, str, bool, etc), but any kind of type and reference (including references to other objects). For example:

object1["target"] = object2

That way, you can access the object2 without needing to search for it, just accessing it through the reference.

object1["target"].worldPosition
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.