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
0
Question by Paricus · Aug 29, 2017 at 09:11 AM · eventsystemeventsdelegatedelegates

Different approach to an upgrade system

Ok, so I've recently given up trying to make this flexible and just hard coded what i needed. However some insight on how I could have done this would be great.

Basically i wanted to create an array of 'upgrades' that i could randomly choose from to show up in the upgrade menu.alt text

The issue was creating a way to store methods or at least references to methods.

I wanted an array of classses which held a:
string (name of the upgrade)
float (value/power of the upgrade)
method (a reference to the appropriate method to carry out the upgrade)

The idea was to choose randomly among the array, instantiate a button, change its text to the name and power of the button and set the buttons listener to the method in the class.

Looked around at unity events and delegates, but didn't get anywhere.

insight on anything i'm missing or should be looking at would be appreciated, thanks.

capture.png (4.6 kB)
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

4 Replies

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

Answer by haruna9x · Aug 29, 2017 at 11:37 AM

Ok, so first, you need to link your updates, how it affects the object. It basically looks like:

 public class BaseClass:
 {
     public int baseValue;
     
     public virtual int Value
     {
     get{ return baseValue;}
     }
 }
 
 public class ModifiableClass:
 {
     public int modifiableValue;
     
     public virtual int Value
     {
     get{ return base.Value + modifiableValue;}
     }
 }

However, you need to know, yes, I want to upgrade the power, so, an enum is nice:

  public enum BaseTypes
     {
     Power,
     Health,
     Defense,
     }
 
 public class BaseClass:
  {
      public int baseValue;
      public BaseTypes baseType;
      
      public virtual int Value
      {
      get{ return baseValue;}
      }
  }

But having problems with the modifier here, it seems to only give you one upgrade, and only plus.

 public enum ModifierTypes
 {
     Add,
     Percent,
 }
 
  public class ModifierClass:
  {
      public int value;
      public ModifierTypes type;
  }
 
  public class ModifiableClass:
     {
         public int modifiableValue;
         public List<ModifierClass> modifiers;
         
         public virtual int Value
         {
         get{ return base.Value + modifiableValue;}
         }
 
     // Here, you can easily calculate modifiableValue based on modifiers
     // However, regardless of what you do, keep your order fixed (multiplication before or preceding).       
     }

Basically, it has allowed you to upgrade a stat. I hope you like it.

Comment
Add comment · 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
0

Answer by IMemeManI · Aug 29, 2017 at 11:19 AM

Never really tried anything like this but you want to store the methods for a upgrade?

Why not just make a new class, doesn't need monobehaviour?

Then just reference that script from your calling script to upgrade, e.g:

public class Methods { public string U_name = ""; public float U_power = 0.0f; //I think you can use the "override" for this public void Upgrade(string name, float power) { U_name = name; U_power = power; //effects here } }

That allows multiple upgrades and manipulation for indevidual ones. For effects simply just add it in - in your calling script for said upgrade but I think you'd have to make the void overrideable.

No idea if this'll work or if this is what you needed but hope this helps in anyway. @Paricus

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 IMemeManI · Aug 29, 2017 at 11:20 AM 0
Share

It's formatted correctly right? I'm on my iPhone rn and it looks like it formatted incorrectly...

avatar image
0

Answer by misher · Aug 29, 2017 at 12:27 PM

Let's suppose your base calss is called "Player" and Powerup class is "Powerup" with one virtual method wich accept Player object. Player has a bunch of properties that can be modified. Now Powerup class has as many derived classes as you need for each individual powerup with ovverrided method to modify player object, you can even make Powerup class a scriptable object so you can store it as asssets (like hp boost with different ampunts)...

Comment
Add comment · 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
0

Answer by Paricus · Aug 30, 2017 at 11:54 AM

thanks for all the answers. They've essentially forced me to look up stuff I've been avoiding :)

I rewrote my code to be some what more flexible:

     Upgrades u;
     public enum MethodEnum { bps, bpc, cost, pDur, pBoost };
     delegate void MethodDel(float val);
 
     [Header ("Upgrade (%)")]
     public string uName;
     public float uPower;
     public MethodEnum uMethod;
     MethodDel method;
 
     [Space(10)]
     public Text buttonText;
 
     private void Start()
     {
         u = Upgrades.upgrades;
         buttonText.text = uName + "\n" + (uPower >= 0 ? "+" : "") + uPower + "%";
         GetMethod();
     }
 
     public void GetMethod()
     {
         switch (uMethod)
         {
             case MethodEnum.bps:
                 method = BPS;
                 break;
             case MethodEnum.bpc:
                 method = BPC;
                 break;
             case MethodEnum.cost:
                 method = Cost;
                 break;
             case MethodEnum.pDur:
                 method = PanicDuration;
                 break;
             case MethodEnum.pBoost:
                 method = PanicBoost;
                 break;
         }
     }
 
     public void Upgrade()
     {
         method(uPower / 100);
     }
 
     public void BPS(float val)
     {
         u.bpsM += val;
     }

This way I can choose which upgrade the button will trigger using an enum (thank you @haruna9x )
The only real draw back however is having to make a method for each upgrade and reference it in a switch statement.
Works for this scale, but i'd imagine it could get annoying the more values you want to upgrade.

Sorry if i'm completely missing something from your answer. Not that amazing of a programmer just yet. Thanks anyway.

Comment
Add comment · 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

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

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

Instance variables and this == null in event handler 1 Answer

Question regarding delegates and events 1 Answer

Physics.Raycast and Debug.DrawRay not working with Google Cardboard / Google VR? Or because within a Delegate? 2 Answers

Is it possible to make custom automatically-called methods like Start and Update? 0 Answers

Unity Events not registering a function due to custom parameters 0 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