- Home /
How do I reassign the result of a function back to the original arguments?
I'm sure this is amazingly simple, but can't for the life of me find the answer. I'm trying to write a function that will take three arguments, do some arithmetic on them, then update their values. But I have a slew of the sets of three variables and am trying to pass them all through the same function.
function StatUpdater (stat, statnext, stattotal) {
if (stat = 0) {
stat = do some math;
statnext = some more math;
stattotal = yet more;
}
}
What am I missing to then get those values back onto the original parameters, like if I put
StatUpdater (life, lifenext, lifetotal);
StatUpdater (str, strnext, strtotal);
in my Update(), or call it from somewhere else, then the states of those variables change as intended?
It sounds like your code is totally changed, but just for future reference, the line if (stat = 0) should have a double-equals sign if (stat == 0). Also, if you're mixing languages, compilation order matters: http://answers.unity3d.com/questions/4822/i-need-to-call-a-c-function-from-javascript/4829#4829
Ahh - good catch. It was originally "if (stat <= 10)" but I erased it all for simplicity's sake in the post and missed that. Thanks :)
Heh - as it turns out, my comment is irrelevant - if you had entered a single "=", the compiler would give an error. Apparently C# has fixed that annoying little glitch in C++, and requires a boolean expressions inside the if. Live and learn...
Answer by Extrakun · Jun 10, 2010 at 05:34 AM
As the answer stated above, you need to use pass by reference.
Primitive types (ints, floats and such) are passed by value. You are modifying a copy of the original variable in the memory.
Classes, however, are passed by reference to functions. When you access them in the function, you are accessing the original memory space of the class. So wrap up your stats in a class and pass it to the function. Though if you are doing C# and only changing a few values, using ref could be a better way.
class Stats {
public var health; public var strength;
}
class Test extends MonoBehaviour { public function Start() { s:Stats = new Stats(); s.health = 100; ChangeHealth(s); Debug.Log(s.health); }
public function ChangeHealth(s:Stats) { s.health = 0; } }
The output will be zero.
That clears up a considerable amount, yet I'm still failing at my implementation. If I have dozens of 'stats', I define all those in the class Stats - check. But in the public function, is there not a way to just write it once to effect whichever of the dozen is passed through? Like in your example, nothing would happen to strength when called. If I have to write out each one, I don't need to use a referenced class, I can just use GetComponent() or declare them all static to pass them around and apply the math operations directly on them. Or am I thinking totally backwards?
You have to consider what works, and what helps to organize your code so it is easier to maintain. You can have static variables, but you can only have one instance of that class. Having a data structure to store the stat allow you to reuse it for different classes - players, enemies and what no. If you do not wish to create a class, because you have variable number of stats, try a Hashtable. You can still pass it in for changes, or return a hashtable/result class, or use C#. Lots of way to solve one problem.
Answer by Eric5h5 · Jun 10, 2010 at 06:04 AM
Javascript has no ability to declare reference variables in functions. The only way to pass by reference at the moment is to make a class, since classes are always passed by reference.
Thanks for that. With these answers I at least know what to search for now. I've found plenty on it and noticed you've answered this question here and in the forums multiple times. I saw where you linked to a vote to add this feature and I contributed to the effort.
Answer by Tetrad · Jun 10, 2010 at 05:26 AM
Not sure the JavaScript equivalent (edit: turns out there isn't one unless you write a class wrapper for your data), but what you're looking for is the ref or out keyword. In essence, those keywords allow you to pass value types "by reference" so that when you update them in a function, you're updating the original instance of the variable and not a copy.
In C#:
void Whatever( ref float a, ref float b, ref float c )
{
// whatever
}
Usage:
Whatever( ref a, ref b, ref c );
Whatever( ref a, ref b, ref c );
Thank you for such a quick response. As much as I don't want to mix languages, I think it is going to come to that.