Global Properties

Hey I had a hopefully simple question that I can’t really seem to figure out. I don’t know too much about python, which is whi I’m having trouble with this.

What I’m trying to do I guess in simplest terms is to access a property from another object. The character in the game has swords and I have it set up so that every time the sword hits the enemy, the enemy loses life ( I didn’t do any of that with Python). The problem is when the character runs, his arms move so he is hitting the enemy without actually swinging the swords. Now obviously in real life this would make sense, but I don’t want the game to be like that. So what I want to do is set up the swords so that they only cause damage when you swing them. My plan was to have a property assigned to the swords and when he swings them, use the frameprop part in the action actuator, so it’ll only cause damage if the value is between 1 and 25 or something like that.

So what I’m looking for is something that will access the property and it’s value from a different object. I want a scrip that I can put on the enemy that will access the property and it’s value on the hero. I hope I’m explaining this right.

Also I should mention I’m using 2.49b if that makes a difference.

For fairly localized logic like passing a value between a sword and an enemy, you would instead use local properties and pass them by getting the object through things like collisionSensor.hitObject.

Once you define the reference to the collision sensor in python and get the hitObject, you can then use that to directly assign a new value to any property that the other object has.

Ok, I read through the other thread and it was interesting, but I’m still not following it. I just don’t know enough about Python. I think your gonna have to spell it out for me a little more because I still don’t see how it would pass from one object to another.

Here is a simple answer:

On the sending object:
make an always sensor, enable true pulse and connect it to a script controller.
make a script called ‘send.py’ and set it in the controller.
assuming the property is called “property”

On the receiving object:
make an always sensor, enable true pulse and connect it to a script controller.
make a script called ‘receive.py’ and set it in the controller.
assuming the property is called “property”

send.py

controller = GameLogic.getCurrentController()
owner = controller.owner
property = owner["property"]
GameLogic.globalDict["property"] = property

receive.py

controller = GameLogic.getCurrentController()
owner = controller.owner
property = GameLogic.globalDict["property"]
owner["property"] = property

Ok, that was very helpful. Now my only question is how do I get it to add or subtract the value of the property.

Connect a collision sensor to a python sensor.

There are explanations in the quotes (#)


import GameLogic

cont = GameLogic.getCurrentController()
own = cont.owner
#get the sensor

collision = cont.sensors["collision"]
#This code should be on the sword. We are trying to find the armature, so if
#the sword isn't parented directly to the armature this script wont work

print("collided")

par = own.parent

#A sensor fire twice (positive and negative) this stops the
# code from subtracting twice
if not collision.positive:
    pass

#A whole lot of bounds. If you have collided, if the attack frame is within the limit, and if the sensor has just collided
elif collision.positive and par["attack_frame"] > 0 and par["attack_frame"] < 26 and collision.status == GameLogic.KX_SENSOR_JUST_ACTIVATED:
    
    #finds the object you just collided with
    hit_obj = collision.hitObject
    
    #subtracts a certain amount of health from the health property on the collided object
    hit_obj["health"] -= 5

Here is the .blend that I tested the code on, collision help.blend (362 KB)

Ok, yeah I’m using the -= and += and for some reason it won’t work for me. Is there something special you have to do with it?

Ok, wait nevermind, I fixed it. Now is there a way to make it only subtract once? Because it’s subtracting twice, which I know is because it’s subtracting when it hits the object and when it stops hitting the object.

Hi Danny,
i went through this kind of situation once and the way i got around that its obviously not the best option but as a temporary solution it works,what i did was i created a cube and set it to wireframe and invisible for game engine,afterwards i positioned it to the enemy s head hieght,attached it to the enemy s dynamic box and set it to ghost then i added a near sensor for the sword which would be something like 3 to 4 so everytime the sword passes through that box it would take an amount of life…but i obviously want to implement something more sofisticated for the future something like a random damage,not sure if its possible in logic bricks but ill defenetly give it a try soon…i hope this helps as a temp solution…

That is actually the best method - using simple collision boxes.