Create object properties inside a script?

[EDIT: I’m going to utilize both logic bricks and python controllers now. Read Monster’s posts below for more info.]

I was wondering if, rather than cluttering the logic editor with dozens of object properties, I could create properties inside a script/class/module/whatever. For example, creating a float property named “prop01” with a value of 0.25. Would it be possible to write that into a script and then give the owner of the script that property? And if so, how?

import bge

cont = bge.logic.getCurrentController()

own = cont.owner

if 'prop01' not in own:
    own['prop01']=.25


Thanks for the quick reply. Yeah, I didn’t even need the “if” statement; I just needed to define own[“prop01”] lol. Embarrassing :o.

However, I’ve run into an unexpected problem. After I defined “prop01” and “prop02”, I removed them from the logic editor’s Property panel. Those properties are needed by two actuators, but since removing them from the Property panel, the actuators won’t work. (Both properties are still defined in the script.)

The first Property actuator (i.e. prop03) is set to “Assign” mode, with Property “prop01”, and with Value "prop01 + prop02. The second actuator is the same as the first, except it subtracts the two properties (i.e., Value = prop01 - prop02).

So . . . is it possible to define the actuators in the script as well lol? Or perhaps somehow get the actuators in the logic editor to recognize the properties in the script?

open the script template ‘Gamelogic simple’ in the text editor in blender.

import bge



def main():


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


    sens = cont.sensors['mySensor']
    actu = cont.actuators['myActuator']


    if sens.positive:
        cont.activate(actu)
    else:
        cont.deactivate(actu)


main()



Sorry, that doesn’t really help me. Let me rephrase my question: Do I need to create actuators in the Logic Editor, or can I specify which actuators I want, along with their various properties, inside the script?

For example, is it possible to somehow transcribe this:


into my script?

own[‘prop01’] += own[‘prop02’]

or

own[‘prop01’] = own[‘prop01’]+own[‘prop02’]


own['prop01'] += own['prop02']

Is that what you’re trying to do?

The easiest way to use properties within the GUI of logic bricks you need to define them in the GUI.

You can dynamically create them via Python. In that case you need to apply the properties to the logic bricks too. Otherwise they do not know about them. Yes, it is that complicated as it sounds. You will lose a lot of the benefits of using the GUI.

When make a statement like “dozens of object properties” the question arose in my head “Why do you need that many properties at a single object?”. This question grows even more when you call your properties “***01”, “***02”. Numbering variables, functions, properties is a sign of insufficient data structures and/or replications.

To offer better fitting solutions we need to know a bit more of what you want to do.

The reason I need lots of object properties is because this is for the player character, and I plan to make the game very interactive. And no, I don’t call my properties prop01, prop02, etc.; that was just as an example lol.

There are two main reasons I tend to stay away from logic bricks: 1) It can quickly become a tangled mess of bricks and wires; and 2) in case I need to start over, I can just use a few python controllers rather than recreate every single property.

So I don’t know, I might just stick with the Logic Editor lol. I’ve been tinkering with the script, but none of my actuators are working.

Both ways offer a great variety of possibilities to create a huge mess.

Usually the best option is a mix of both - predefined logic bricks and custom python controllers. The trick is to know when to use what and when to separate aspects from each other ;).

The worst case is to create a “god object” that want to care about everything.

Example:
When you calculate damage you can focus on the calculation, or you can do calculation and mix it with showing the result and mix it with the influence on the character in combination with all possible participants.

I prefer to focus on the pure calculation. It would get input from a defined interface (e.g. messages and/or properties and/or function arguments). It calculates the result and provides it over another defined interface (e.g. messages and/or properties and/or function arguments).

My aspect does not care how and if the result is shown
-> freaking simple
-> minor dependencies

Other aspects (like the health bar) do not care how the damage is calculated, they focus on showing the health provided via their defined interface.

just some thoughts

Both ways offer a great variety of possibilities to create a huge mess.

Indeed lol. My script was getting long to the point of being illegible. I was trying to go for that “god object” that would address everything I could possibly need in one script, but yeah it’s not very practical.

Thanks for the advice, Monster. I guess it wouldn’t be feasible to create a game exclusively via python or logic bricks; a mixture of both is often required. I still hope to finish my little side project one day, and when I do and people study its inner workings, I hope it’ll look clean and professional.

what is good code?

portable, and easy to understand, with good performance.

I tend to use many small snippets, in many cases, so I an re-use them,

this works fine unless you get 100’s of 1 object all running the same code.
then its better to have 1 script manage all of the objects, and cut out 90% of the overhead.