Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JRA · Jun 10, 2010 at 05:19 AM · variablefunction

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cyclops · Jun 11, 2010 at 12:33 AM 0
Share

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

avatar image JRA · Jun 11, 2010 at 12:44 AM 0
Share

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 :)

avatar image Cyclops · Jun 16, 2010 at 01:38 AM 0
Share

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...

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JRA · Jun 10, 2010 at 11:18 PM 0
Share

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?

avatar image Extrakun · Jun 11, 2010 at 03:46 AM 0
Share

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.

avatar image JRA · Jun 11, 2010 at 04:58 AM 0
Share

Lots of ways indeed. Thanks so much for your help.

avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JRA · Jun 10, 2010 at 11:08 PM 0
Share

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.

avatar image
2

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 );
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JRA · Jun 10, 2010 at 11:09 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

variable doesn't change 1 Answer

variable with int value not passing to command 1 Answer

How do you achieve variables and functions that are global between scenes? What is the BEST way? 1 Answer

Error on my javascript code 1 Answer

How to access variable from another function? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges