Changes to Instance Reflected by All Class Members

Monkey Forums/Monkey Programming/Changes to Instance Reflected by All Class Members

zoqfotpik(Posted 2013) [#1]
I have an issue where some but not all changes made to instances of a class occur in all class members.

The problem in question is gravity. I add a gravitational constant to class instances.

But when I create new instances of the class, the y velocity continues to increase! In other words, instance 2 has the y velocity of instance 1.

Any idea why this might be? The code is a little too dispersed to expect people to sift through...

Velocity is the only field that is being replicated this way.


muddy_shoes(Posted 2013) [#2]
At least post the class in question and an example of where you "add a gravitational constant".


mteo77(Posted 2013) [#3]
funnily enough i do remember to have something similar when i was programming in blitz 3d....if every time you add an instance the velocity keep getting added it's because the code that set the variable somehow doesn't have a specific value.
Bit difficult to explain though,is gravity a global?
is the code that set the velocity tied to a specific instance or is general?


mteo77(Posted 2013) [#4]
when you say that the velocity increase, do you mean the velocity of the LAST instance or the velocity of every instance on screen?


mteo77(Posted 2013) [#5]
if it's the velocity of every instance it could be down to a simple "velocity = velocity + suchAndsuch" so if velocity is a global it keep getting added.
from my experience if a class is properly set up with fields that aren't shared it's very unlikely that one instance of an object interfere with the next one (if it make any sense)


mteo77(Posted 2013) [#6]
usually if the class members interfere with each other it's because one of their fields is a global that get used by everybody, so when an instance try to add to that global the next one get the global increased by the previous value.
sorry for spamming


zoqfotpik(Posted 2013) [#7]
It is just absurd what is happening.

Mteo it probably is that something is global, I will take a look again after I sleep.

Muddyshoes if I sound retarded at times it is because I only post after extremely difficult troubleshooting when I am frazzled, and intractable bugs usually happen late in the day for me, I should post after I rest.


muddy_shoes(Posted 2013) [#8]
I didn't imply that you sounded retarded, only that you've posted no code at all to give us a clue what you're doing.


Nobuyuki(Posted 2013) [#9]
also make sure that your instance variable isn't being shadowed by or referenced in a higher scope which only has a single instance.


Gerry Quinn(Posted 2013) [#10]
"The code is a little too dispersed to expect people to sift through..."

Indirectly, this may be the root of the problem!

There are other possibilities, though. Maybe you are just initialising new objects with the previous object's y-velocity when you meant to use a default value.

A print statement at the end of the object creation / initialisation may help.


zoqfotpik(Posted 2013) [#11]
Thanks for the responses. This is a class that handles particles and doesn't in any way affect gameplay. And since its short I am just going to rewrite it from scratch. Oftentimes that's easier than troubleshooting.