Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What's the right way to pass multiple integers to another script.

Discussion in 'Scripting' started by jsmall81, Feb 12, 2021.

  1. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    I have a "harvester" class that gathers integers as resources. 4 types possible. resourceWood, resourceFood, resourceIron and resourceStone. I have a resourceDisplay gameObject and script that displays current resources. At the moment I only have it show a single integer called resources.

    When my harvester passes the values to my resourceDisplay script, it just adds the total of all 4 integers and totals them up and sends them to be added to my resourceDisplay gameObject.

    Code (CSharp):
    1. player.SetResources(player.GetResources() + resourceFood + resourceIron + resourceStone + resourceWood);
    player.SetResources() just updates that integer with the updated values.
    Code (CSharp):
    1.     public void SetResources(int newResources)
    2.     {
    3.         resources = newResources;
    4.     }
    player.GetResources() just returns the current value for that integer.
    Code (CSharp):
    1.     public int GetResources()
    2.     {
    3.         return resources;
    4.     }
    If I wanted to use this basic structure to update multiple integers, what is the "right" way, do you think? So, when I send the values to my player, it updates all integers, NOT total them together. Would I just use multiple functions using the same code as above? So, I would setup a function for each individual integer that I wanted to add up? Or can I have a function that would pass each integer along to the player script?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You can have more than one parameter on a method.
    Code (CSharp):
    1. public void SetResources(int food, int iron, int stone, etc...) {
    2.   this.food = food;
    3.   this.iron = iron;
    4.   // etc...
    5. }
    Alternatively, you can create a struct:
    Code (CSharp):
    1. public struct ResourceInfo {
    2.   public int iron;
    3.   public int food;
    4.   public int stone;
    5. }
    And use that struct as the single parameter:
    Code (CSharp):
    1. public void SetResources(ResourceInfo resourceInfo) {
    2.   this.food = resourceInfo.food;
    3.   this.iron = resourceInfo.iron;
    4.   // etc...
    5. }
    Even better, replace the various int fields in player with that same struct:
    Code (CSharp):
    1. public class Player :MonoBehaviour {
    2.   // So instead of this...
    3.   int food;
    4.   int stone;
    5.   int iron;
    6.  
    7.   // Do this:
    8.   ResourceInfo resources;
    9.  
    10.   // And then do this:
    11.   public void SetResources(ResourceInfo newResources) {
    12.     // This copies all the data at once!
    13.     this.resources = newResources;
    14.   }
    15. }
     
    Last edited: Feb 12, 2021
    Vryken, Joe-Censored and mopthrow like this.
  3. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    I'm very new to coding in general, so it's probably me making the mistake... but, it's telling me it cannot convert type ResourceInfo to int.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You'll have to share exactly what your code looks like - but it sounds like you have an int variable and you're trying to assign a value of the type ResourceInfo to that variable.
     
  5. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    The way I had it set up to return "resources" was as an integer. I'm testing out this struct stuff you mentioned. I've never done those before. I have a function on my player script that will return the resources integer to whatever calls it.
    Code (CSharp):
    1.     public int GetResources()
    2.     {
    3.         return resources;
    4.     }
    So, I'm assuming since that is what we call the struct, that's where the issue comes from. But, if I wanted to get the int values for each int in that struct, how do I go about calling them from another script? So, if my gameObject has 10 of each int representing the resources, how would I pass that info along to the struct integers in my player script?
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Since you are returning a ResourceInfo instead of int, you must change the return type of the method:
    Code (CSharp):
    1. public ResourceInfo GetResources() {
    2.   return resources;
    3. }
     
  7. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Ok. That makes sense. Now, just because I know you know way more about this than I do. Before I go breaking multiple scripts... In my harvester class, I run this method to "transfer" the resources to my "scoreboard". This is how it is now;
    Code (CSharp):
    1.     public void CmdTransferResources()
    2.     {
    3.             print("CmdTransferResources() Dumping resources");
    4.             RTSPlayer player = connectionToClient.identity.GetComponent<RTSPlayer>(); print("CmdTransfer: Get network player.");
    5.             player.SetResources(player.GetResources() + resourceFood + resourceIron + resourceStone + resourceWood); //Add resources to current amount of player.
    6.  
    7.             resourceFood = 0;
    8.             resourceIron = 0;
    9.             resourceStone = 0;
    10.             resourceWood = 0;
    11.  
    12.  
    13.  
    14.             isFull = false;
    15.             hasResources = false;
    16.             FindNextPosition();
    17.     }
    So, if I restructured this with using the struct as our means of storing our int values, I would have to create that same struct to be used for transferring this info as well, correct? So, for any class that I need to be able to check or store those values, I'd have the struct setup;

    Code (CSharp):
    1.     public struct ResourceInfo //Declaring int for use as resources
    2.     {
    3.         public int food;
    4.         public int iron;
    5.         public int wood;
    6.         public int stone;
    7.  
    8.     }
    Now, if I altered my code for the harvester to transfer resources, would something like this work?
    Code (CSharp):
    1. player.SetResources(player.GetResources() + ResourceInfo);
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    You only need to declare/define the struct once. I suggest putting it in its own file called ResourceInfo.cs which contains only the struct definition. Then you can use that struct type from all of your other classes.
    No, that code doesn't make any sense. First - you wouldn't set the player's resources to... its own resources. Second - get rid of the nonsensical part at the end:
    + ResourceInfo
    .
     
  9. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Yea, like I said I am very very beginner at this and the "tutorials" don't really do a good job of teaching you anything, more of just a copy / past this and watch what happens. :D
     
  10. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Thanks for your help and all the info. I got some reasearching to do.
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    If you have ever worked with any of the built-in struct types in Unity, such as Vector3, this new struct you create will behave a lot like that.