Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by juniperspark · Nov 27, 2016 at 07:56 PM · functionspassing

Passing parameters in function efficiency?

Hi guys,

Forgive me for a basic question, but i'm not sure on how clean this way of coding is, could you tell me what would be the most efficient way, or if there is no difference?

If I have a large monster class and I am writing an attack that will hit the monsters target and another attack that will hit the monsters target, but also hurt itself, would it be more or less efficient to pass the monster class in the function? it saves me a lot of coding space to have it passed, but if I have 50 similar functions would that not be inefficient?

 class MonsterData
 {
     var name                : String;
     var gameObject            : GameObject;
     var target                : int;
     var curHealth            :float;
     var maxHealth            :float;    
     var curEnergy            :int;
     var maxEnergy            :int;
     var curStrength            :float;
     var curSpeed            :float;
     var curDefence            :float;
     var curAccuracy            :float;
     var curCritical            :float;
     var attackSlot1        : AttackData;
     var attackSlot2        : AttackData;
     var attackSlot3        : AttackData;
     var attackSlot4        : AttackData;
     var attackSlot5        : AttackData;
 
     // etc etc..
 }
 
 //----------------------------------
 // ******* VERSION 1:
 //----------------------------------
 
 var targetMonster         : MonsterData;
 var attackingMonster     : MonsterData;
 
 
 function RegularAttack()
 {
     // camera movement, target monsters, damage calculations...
     
     TakeDamageTarget();
 }
 
 function RecoilAttack ()
 {
     // camera movement, target monsters, damage calculations...
     
     TakeDamageTarget();
     TakeDamageSelf();
 }
 
 function TakeDamageTarget()
 {
     targetMonster.gameObject.GetComponent.<Animation>().Play("takeDamage");
     SlideHealth(targetMonster);
 }
 
 function TakeDamageSelf()
 {
     attackingMonster.gameObject.GetComponent.<Animation>().Play("takeDamage");
     SlideHealth(attackingMonster);    
 }
 
 // other functions such as PoisonSelf() / PoisonTarget(), FaintSelf / FaintTarget() etc...
 
 //----------------------------------
 // ******* VERSION 2:
 //----------------------------------
 
 var targetMonster         : MonsterData;
 var attackingMonster     : MonsterData;
 
 
 function RegularAttack()
 {
     // camera movement, target monsters, damage calculations...
     
     TakeDamage(targetMonster);
 }
 
 function RecoilAttack ()
 {
     // camera movement, target monsters, damage calculations...
     
     TakeDamage(targetMonster);
     TakeDamage(attackingMonster);
 }
 
 function TakeDamage(monster : MonsterData)
 {
     monster.gameObject.GetComponent.<Animation>().Play("takeDamage");
     SlideHealth(monster);    
 }
 
 // other functions such as PoisonSelf() / PoisonTarget(), FaintSelf / FaintTarget() etc...
Comment
Add comment
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

1 Reply

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

Answer by ElijahShadbolt · Nov 27, 2016 at 08:16 PM

I would use Version 2, where you pass the specific MonsterData you wish to change, because you'll have to write half as much code for exactly the same effect. Since MonsterData is a Class Type and not a Value Type, the parameter will actually be a reference to the class, not the whole class itself. This should have no impact on performance or efficiency compared to Version 1.

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 juniperspark · Nov 27, 2016 at 09:55 PM 1
Share

Fantastic, that is just the answer i was looking for. Thank you.

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

57 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pass arrays from one object to another? 2 Answers

How can you add a function to a variable. Such as string.ToUpper() ? 1 Answer

My function is being called even when it shouldn't 0 Answers

SteamVR PLAYER SETUP 0 Answers

Dragging an object to variable: Problems in the editor! 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