Assigning Variables with Python

Ok, I have a question about a python script I wrote. I used to to determine whether or not an enemy can see a player. It’s a radar sensor, and if the player enters that area, the enemy attacks him (swings at him to be more specific) if the player is not in that area, the enemy walks towards the player until he’s in the area again. It’s pretty simple and straightforward, but my question comes from when I tried to assign a variable (I think that’s what it is) to the property. Here is what I wrote:

controller = GameLogic.getCurrentController()
owner = controller.owner

sen = controller.sensors[‘Near’]

activated = sen.positive

if activated == True:
owner[‘near’] = True

if activated == False:
owner[‘near’] = False
This works fine, but if I change it to this:
controller = GameLogic.getCurrentController()
owner = controller.owner

sen = controller.sensors[‘Near’]

activated = sen.positive

near = owner[“near”]

if activated == True:
near = True

if activated == False:
near = False

Basically when I’m assigning owner[“near”] to near, it isn’t working. There’s probably a really simple reason as to why, but I’m not really sure.

Now I know what your thinking (or maybe I don’t, I can’t read minds). If it works, then why would you want to change it? Well, basically I’m just curious as to WHY it isn’t working. If anyone knows, I’d appreciate some info. Again, I’m using 2.49b if that makes a difference. Thanks.

Dan

Well… It is working, You set the variable near. But You probably expect owner[‘near’] to change but it will not. You assign the value of owner[‘near’] to near and then You change near - that will leave owner[‘near’] untouched.

Go with Your first example or end the later with a line: owner[‘near’] = near

Oh, ok that’s what I thought. That seems kinda strange though, I thought the whole purpose of assigning near to owner[“near”] was so that they would be the same thing?

They got the same value, but are different containers for that value that can be changed independently. Think code like this:

oldx = x
x = someNewValue()
diff = x - oldx

Would not work well if oldx changed every time we change x.

LaH’s got it right. Essentially, the point is that variables won’t automatically change the same with other variables, as this might cause issues (it would with me, at least). To fix your code, you would just use owner[‘near’] = near below where you assign near a value (so that what near is gets assigned to the object variable).

Ohhhhhh, ok. So if I change the value of near, it won’t change the value of owner[“near”], but if I change the value of owner[“near”] it WILL change the value of near, right?

No, it usually won’t. There’s special circumstances, but essentially, if you assign a variable to a value, it will only be that value after that point, and changing the source won’t do anything to the variable. For example:


a = 1
b = a # a is 1, b is 1
a = 2 # a is 2, b is 1
b = a # a is 2, b is 2

I would suggest you learn Python through the help file - it’s very useful. I’ve also heard things about ‘Byte of Python’. Once you get it, learning to use the BGE with Python will be simple.

Here’s my little bit:

There are two parts to this question. One is how to get a property from an object, and the other is how to set a property in the object.

So. To get a property:

somenumber = obj["property"]

And to set a property:

obj["property"] = somenumber

This is always the case, and if it isn’t working then one of a few things could be wrong:

  1. A typo
    Something like this is so easy to do
num = obj["property"]
num = num + 62
<b>mun</b> = num -15
obj["property"] = num

  1. Your maths/manipulation is flawed
    divide by 0, having if/then statements that are always true etc

One last thing to mention is getting numbers that aren’t properties.
Say you want to get the objects colliding with the player, you can get that with some fancy python (hitob = obj.hitObjectList if you wanted to know) but remember that you cannot set something like that. IT IS READ ONLY