create/delete a property with python

is it possible to create and delte a property with python? my character has a sword, and damageable objects have a collision sensor looking for the property “sword”. I only want the sword to have that property when the sword swing animation is playing. so can anybody help?

It might be easier to use a boolean property on the sword, and only count the collision if that property is True. You can still have the sensor looking for the sword property, then use python to get (collision object)[‘sword’]. A return of 0 is false, 1 is true.

Alternatively, you can have the sword without the property, but switch with an identical one with the property when you swing.

Thanks magnum, I’ll try the boolean idea. How can I find out if it’s it’s true or false, and have the hit object know. I know how to find out on the same object, but not with two objects. GlobalDict maybe?

You access an existing property with
own[“property”]

You can delete it with:
del own[“property”]

You can add a new one, just as you would change an existing one:
own[“property”] = value

Where “own” is the object who owns the property. Could be any object anywhere in your scene, independent from the controller the script is running on.

Some more…

check a property exists

if “property” in own: print (1)

get a property, or use a value if the property is not found

val = own.get(“property”, fallback_value)

One other thing is that variables in Python is not predefined.They are generated when you assign a value to them.
So, own[“property”] = value is more than enought to create a variable, and one other thing.
when you create a variable own[“property”] = 1 then Python assumes that this variable will be consist of only integer values and automaticly defines it as integer variable.
That is the case if you create it like this : own[“property”] = True - Python defines it as Boolean value.
So its important to know, that when you create variables you try not to change their types during the game.
I mean if your variable is own[“property”] = 1(integer number), do not change it to own[“property”] = 1.002 (float number) its important for Python, otherwise there might be some wrong calculations.

I mean if your variable is own[“property”] = 1(integer number), do not change it to own[“property”] = 1.002 (float number) its important for Python
I don’t undertstand this.
How do you set a variable to 1, and accept floats later?
something like this?

own["property"] = 1.002
own["property"] = 1

Also, i don’t see what’s wrong with the following? (just asking, I would like to undertstand)

own["property"] = 1
own["property"] += 0.001

There is nothing wrong with changing types, Python does that automatically. Int to Float used to be a problem some python versions ago. Maybe haidme is just remembering old versions. I remember being forced to pass a value of 1.0 instead of 1 to some functions just to avoid a type error. But nowadays the conversion is done smoothly. As soon as you add a float value to an int variable it’s gonna convert the int to a float.

>>> a = 1
>>> type(a)
<type 'int'>
>>> a += 0.001
>>> type(a)
<type 'float'>

Hmm, maybe you are right vibrunazo.
However here is a simple tutorial about using python variables:
http://www.developer.com/article.php/626321

This is what I thought.
Thanks for the confirmation.

The only time a peroperty is static, as far as I can tell, is if the variable is a predefined property on an object (IE you add it with the add property button), other times they can dynamically change type. For example, if a property is added to an object via python in-game, it can go from a string to a kx_gameobject with no issues (handy if you want to store a reference to an object but have the option of storing “NONE” instead)

However, in the case of a sword (or bullet, or anything, really) doing damage, it really makes a lot more sense to have the sword, and not the hit objects, deal with damage- on the sword use a collision sensor and loop through all the hit objects, if they have a property named “HP” (or whatever) reduce this property by an amount. It’s much more compact, and you can change the damage dealt very easily with a property on the sword, instead of having to hard-code it into the script or go through every object the sword might hit and changing a property.

Wow, thanks everybody! I learned alot more about variables from you guys.
@cap’n - I understand what you’re saying, but I’m not sure how to do it.
I’ll go look at the python api and see if I can find out. Or if anyone wants to write an example script, I would be grateful. Thanks again.

I figured out how to edit the other objects property, but how do I find out what object the sword hit, and if it has the HP property?

On the sword, connect a collision sensor to the script- put the property name in the field. In the script add


hit=controller.sensors['sensorname']
for ob in hit.hitObjectList:
     ob['propname']-=damage

Obviously you have to replace sensorname, propname, and damage with the applicable replacements, also replace the five spaces with a tab (blenderartists doesn’t allow tabs).
This will reduce propname by damage amount on every object sensorname hits. Make sure the sensor is only looking for objects with propname, or it’ll crash whenever the sword hits an object that doesn’t have that property.

Thanks very much Cap’n. But hitObjectList is not defined, and I’m not sure what it’s supposed to be.

The collision sensor has a hitObjectList attribute with a list of the objects it collided to. So “sensorname” should be the name of a collision sensor.

Do I need to put sensorname.hitObjectList or something, because when I run that script I get an error saying “hitObjectList is not defined”?

er… yeah, I typo’d and forot a part. I’ve edited the script, should work now.

ok, thanks, it works now.