# Function calls reseting variables

This topic is 4055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

selfobjectarray = []
start_x = start_y = start_z = 0.0

class selfobject:
def __init__(self):
global start_x, start_y, start_z
self.x = start_x
self.y = start_y
self.z = start_z

def main():
#stuff to call the following line goes here
objectmake(start_x, start_y, start_z)

def objectmake(start_x, start_y, start_z):
global selfobjectarray
selfobjectarray.append( selfobject() )

I have this code and each time it is called, with:
for i in selfobjectarray:
#Use these numbers for something
i.x
i.y
i.z

...it resets the self variables to the start variables. When the start variables change, so do all the values of "self.x", "self.y", and "self.z". I CAN see why this happens, but I only want it set once! Is there a good way to do that? Thank you, Geometrian [Edited by - Geometrian on May 14, 2007 2:19:13 PM]

##### Share on other sites
Post the actual code that gives you the problem. You've posted a simplified version that actually hides the issue in question and doesn't demonstrate how you're changing those values.

However: When you say something like "start_x = start_y = start_z = 1.0", you're saying they are all the same variable. If you were able to change the 1.0, then all those variables would point to the new value. You're not actually able to do that in Python, but if the item was a mutable object (like a class instance, or a list) then you'd see that sort of behaviour. This is because assignment is not a copying operation in Python, it's a referencing operation, much like changing a pointer's target in C++.

##### Share on other sites
It's true, I added the line "start_x = start_y = start_z = 0.0". In my program, they are all defined differently. Like I said, I can see why this behavior should happen with this code- but I want to change this code so that it doesn't.

I want to call the function "objectmake()" multiple times, adding class objects to the array "selfobjectarray". Each of these objects will have it's own starting value x, y, and z. When I add 0.06 to each of these values each time through the loop, I should get

start_x+(0.06*n)
start_y+(0.06*n)
start_z+(0.06*n)

where n is the number of loops completed. This works fine. However, If the start_x, start_y, and start_z change at all you get:

[new]start_x+(0.06*n)
[new]start_y+(0.06*n)
[new]start_z+(0.06*n)

In effect, the value of start_x, start_y, and start_z should "freeze" and remain unchanged for the object. Again, I must stress the point that I know why this code doesn't work, and I need help changing it to do what I previously stated. I posted every bit of code that has to do with this, and I obviously need to add something, I just don't know what or how.

G

##### Share on other sites
Well, I don't know why this code doesn't work. Numbers are immutable.

You didn't post the code where you actually change the values of any variables.

Also, you only need to declare variables as global if you are going to assign a new value to a global variable. You can use the value already present in the global variable without this declaration. The global scope is sort of read only by default. Forcing the programmer to explicitly declare global variables that will be assigned to in the local scope tends to make code easier to read.

In your original post, the code would behave exactly the same way if you removed the "global" statements.

##### Share on other sites
Quote:
 Original post by GeometrianAgain, I must stress the point that I know why this code doesn't work, and I need help changing it to do what I previously stated. I posted every bit of code that has to do with this, and I obviously need to add something, I just don't know what or how.

No, as Vorpy says, you missed out the code that actually performs the changes, and the code that shows you the results that you believe to be wrong, and instead only posted the code where you believe the problem to be. For example, you haven't shown code that demonstrates how you 'add 0.06 to each of these values'. You may not think it's important, but it almost certainly is.

##### Share on other sites
Well, I'm not on my home computer right now, and my home computer's internet died last night, so I'll try to get the exact code to you ASAP. For now, I can tell you how it's defined. Here's a situation:

I'm a person, and I have starting values start_x, start_y, start_z. Now he throws something- the "object". The object is defined as starting at start_x, start_y, and start_z. Then it flies away. I walk in a different direction. changing the current values of start_x, start_y, and start_z. However, the objects trajectory is calculated from the original values of start_x, start_y, and start_z I was at when I threw the object.

start_x, start_y, and start_z are defined as equaling my current position. I need to make it equal my old position. This code does not do that. The code was just a "guess" at how to do it. Unless you post something here, I'll tell you exactly how I define start_x, start_y, and start_z when I get a chance.

##### Share on other sites
At the beginning of the program start_x, start_y, and start_z are defined as globals by:

start_x = start_y = start_z = 0.0

Then the starting point moves around with:

Where speed is the velocity the starting point is going and groundyrot is the direction it's going. The last line I'm not happy with, because the entire thing needs to be in spherical coordinates and it's not. (It approximates it OK). Basically, this code moves the starting point of the object and I know it works from other parts of my program.

Once I've created the object heading a certain direction, I move it.

for i in selfobjects:

Right now, I set objectyrot = groundyrot and objectxrot selfxrot so the object will go in the same direction of travel as its origin is going. Now perhaps you see the problem? If The origin's direction changes and so the object's direction changes. I'm not sure this explanation helps, as this code shouldn't work, and I need to know how it should. I know the problem, I know what needs to be done, now can you tell me how I might fix it?
Thanks,
G

##### Share on other sites
OK. I just got tipped that the thing I'm looking for is a problem in my code- apparently that which draws and moves it. The problem I thought I had doesn't exist. Thanks though...
Bye.
G

• 11
• 20
• 12
• 9
• 34
• ### Forum Statistics

• Total Topics
631399
• Total Posts
2999847
×