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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

use functions as array???

Discussion in 'Scripting' started by PersianKiller, Feb 5, 2018.

  1. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    t's my code.it's working fine but if I add more guns it should checks lots of ifs ,so I want to optimize it

    Code (CSharp):
    1.  
    2.     using System.Collections;
    3.  
    4.     using System.Collections.Generic;
    5.  
    6.     using UnityEngine;
    7.  
    8.     public class test : MonoBehaviour {
    9.  
    10.     public int whatGun;
    11.  
    12.     public void Drop(){
    13.  
    14.     if(whatGun==0){
    15.  
    16.     dropgun1 ();
    17.  
    18.     }
    19.  
    20.     else if(whatGun==1){
    21.  
    22.     dropgun2 ();
    23.  
    24.     }
    25.  
    26.     else if(whatGun==2){
    27.  
    28.     dropgun3 ();
    29.  
    30.     }
    31.  
    32.     else if(whatGun==3){
    33.  
    34.     dropgun4 ();
    35.  
    36.     }
    37.  
    38.     }
    39.  
    40.     public void dropgun1(){
    41.  
    42.     gameObject.AddComponent<DropGun1> ();
    43.  
    44.     }
    45.  
    46.     public void dropgun2(){
    47.  
    48.     gameObject.AddComponent<DropGun2> ();
    49.  
    50.     }
    51.  
    52.     public void dropgun3(){
    53.  
    54.     gameObject.AddComponent<DropGun3> ();
    55.  
    56.     }
    57.  
    58.     public void dropgun4(){
    59.  
    60.     gameObject.AddComponent<DropGun4> ();
    61.  
    62.     }
    63.  
    64.     }
    65.  
    Code (CSharp):
    1.  
    i


    • how can I use functions as array? like this
    Code (CSharp):
    1.          public void Drop(){
    2.           dropgun[whatGun] ();
    3.       }
    4.  


    • for example if whatGun=3 it runs
    Code (CSharp):
    1.  
    2. public void dropgun4(){
    3.      gameObject.AddComponent<DropGun4> ();
    4. }
    5.  
    hope you guys get what I want ,any help other solution or whatever would be appreciated.
     
    Last edited: Feb 5, 2018
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Consider using switch statements, or just having a single dropgun function that takes a gun value.

    Basic switch example, and passing a value to a function:
    Code (csharp):
    1.  
    2. public void Drop(int gunToDrop)
    3. {
    4.     switch (gunToDrop)
    5.     {
    6.         case 1:
    7.             dropgun1();
    8.             break;
    9.         case 2:
    10.             dropgun2();
    11.             break;
    12.         case 3:
    13.             dropgun3();
    14.             break;
    15.         default:
    16.             Debug.Log("Oops, shouldn't get here");
    17.             break;
    18.     }
    19. }
    20.  
    21. public void ExampleCallingFunction()
    22. {
    23.     Drop(2);  //drop gun # 2
    24. }
    25.  
    I'd also suggest not having separate DropGun# components. Have a single DropGun component and then customize its settings after you add it.

    Code (csharp):
    1.  
    2. gameObject.AddComponent<DropGun>();
    3. GetComponent<DropGun>().Initialize(2);  //Initialize the DropGun component as a gun #2
    4.  
     
    Last edited: Feb 5, 2018
    PersianKiller likes this.
  4. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114


    thanks for response,so I have 10 weapons with 10 completely different drop functions.let's say I just have a script and have 10 drop function in this script.i just can use something like this to detect which gun is holing by the player

    if(whatGun==0){
    dropgun1();

    }
    else
    .
    .
    .

    I don't really know how to use an array to run functions

    like this

    dropgun[whatGun]; //0 run dropgun1();,1 run dropgun2();

    I hope you get what I want to do sorry for bad English. :)
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I've never used an array to run functions (is that even possible?). I'd just use a switch to run functions.
     
    PersianKiller likes this.
  6. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114

    hmmm I did it too ,but if it was possible it was very nice :) thanks for response.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    On the topic of whether it's possible, sure you could have an array of delegates.
     
  8. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    can you show me an example please?
     
  9. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
    I'm a little confused, You want to add a component to the gun to be able to drop it? and they're all seperate components so 10 unneeded scripts? try generalise it to 1 script named "DropGun" and attach it to each gameObject.

    Also, how do you recieve the "whatGun" integer, is it acuried from another script?

    Are the guns stored somewhere? in a list/array? how do you switch between them?
     
    TaleOf4Gamers likes this.
  10. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
    it's not only about dropping a gun or whatever ,I got the answer in this post.thanks for response :)

    https://forum.unity.com/threads/change-script-name-between.515957/#post-3382881

    it's very nice and easy and it's exactly what I wanted :)
     
  11. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Learn enums. You don't want to go
    == 0
    == 1

    etc. That's terrible. Learn enums (they can be cast to a number anyway if you really need to). Will greatly enhance code legibility in this case.
     
  12. PersianKiller

    PersianKiller

    Joined:
    Jul 16, 2017
    Posts:
    114
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think the answer you got was probably better than creating an array/list of functions, anyways - for your use case.

    For the record, though -- learning the available options in programming is very helpful. Then you can pick the best option for the task.
     
    PersianKiller likes this.