Adding/removing properties dynamically (visible in debug list) while engine is running

While assigning properties to various objects in my scene, I’m starting to get a bit disorganized as a lot of objects have more than 10 properties. Is there a way to add and remove them with python while the engine is running? That way the relevant ones will remain on the list or be added to it depending on what part(s) of the scene is active.
Example - I want to create a unique timer property for a specific event, but only use it that one time and have it taken off the list when I don’t need to use the timer.

1 Like

Bump +1 for an interesting question!
I remember running into this issue many years ago, back then there was no direct solution : S

2 Likes

Hey dude, do you know python in UPBGE?

Yes? It’s been a couple years, so I’m a little rusty, but it’s not that much different from Python in the standard BGE. Why do you ask?

Just a min

Here is my issue, please try to do something

Ummm, this is kind of the wrong place to ask. You should use private messages for this kind of stuff, and not clog up someone else’s thread. I will reply to you in such a message.

Don’t think it is possible, but maybe I’m wrong. The whole scope of the property is to be predefined so even if you manage to add new properties in game still means you need to write code for it :)). What you can do is to define global properties, messages.

1 Like

Thanks for the bump. Using own[‘property name’] = value seems to work in my code, but it seems to default to an integer or float and I wish there was a way to set it to timer. Also, when I add them in the script they of course don’t show up in debug mode.

I wish there was something like obj.addProperty([‘property name’], 1, True) where the 1st number is something like KX_PROPERTY_TYPE_TIMER or something and the bool is whether it is visible in debug but I went through the whole API and haven’t found anything like that.

value = 10.00004578
#creates and set a property
obj['property'] = value

#creates and set a property and convert it to int so 10.00004578 becomes 10
obj['property'] = int(value)

timer just create one on your own, depending on what kind of timer simply add:

#counts 60 a second (basic ticrate per frame)

obj['timer'] + = 1

to keep the tic rate in mind

#only adds 1 a second
obj['timer'] += 1 / logic.getLogicTicRate()

if you want real time then use the time module
and calculate start and end time, etc.

lots of ways, google can also tell you those ways

#remove a property
del obj['property']

#empty a property
obj['property'] = '' # or None or False etc...

There is a command to set the debug flag per object in KX_object()

checkout

print(dir(any_py_object))

Naive;

own["propName"] = 1.0;

# add to debug prop list
own.addDebugProperty("propName", 1);

# remove from debug prop list
own.addDebugProperty("propName", 0);

Fancy;


propNameList  = ["prop0", "prop1", "prop2"];
propStateList = [0, 1, 0];

for propName, propState in zip(propNameList, propStateList):
    own.addDebugProperty(propName, propState);

Fancier~;

propDict = { "prop0":0, "prop1":1, "prop2":0 };
for propName, propState in propDict.items():
    own.addDebugProperty(propName, propState);

If a property is deleted it goes away from debug list. So delete from own and propDict when you’re done with it;

del own["propName"]
del propDict["propName"]

If deleting multiple props at a time, loop through;

for propName in deleteTheseProps:
    del own[propName]
    del propDict[propName]

All in all, easy enough to manage. Not too difficult to have a manger look out for unused properties every few frames or seconds or however long you want it to sleep;

3 Likes