Search Unity

Iterating Array From Point

Discussion in 'Scripting' started by TSC, Jul 27, 2017.

  1. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    How's it going everyone.

    I'm looking to forward cycle through a set of object, deactivate the current object and active the next. Also be able to do this from any point of the iteration (not having to start at zero each time), - Start at the last known active object.


    Here's the End Goal:

    Have a set of objects[6]

    Deactivate Last Active Object

    Activate Next Object

    Forward looping capabilities (Change to next)
    Remember last Active Object capabilities

    If curActive is greater than total length
    Deactivate Last Object in array
    reset to beginning of the array

    Set first in array to active.

    Continue This loops.

    I used this approach not sure what I'm failing to realize or implement here:

    Start:
    iGO = CW;


    gameObjects [] go's;
    int CW;
    int iGO;

    method(){
    int cW;
    cW = iGO;
    for (i =cW; i < go's.length; i++ ){
    CW++;
    iGO++;
    go's.SetActive(false);
    go's[i + 1].SetActive(true);

    }
    methodSetLastOff();
    }


    methodSetLastOff(){
    int Cw;
    Cw = go's[].Length;
    go's[Cw -1].SetActive(false);
    go's[0].SetActive(true);
    }

    Any help with the solution for this will be greatly appreciated.

    Thank you and as always stay productive.

    --Grand Rio
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    First thing: Use [code ]code tags[/code], they make things easier to read, and including an "i" in brackets won't mangle the remainder of your post as it's done here.

    Second thing: Give your variable real names, especially in pseudocode. It makes your code much easier to understand. You're not being charged per letter, and you have access to autocomplete.

    As for your actual problem - you seem to be trying to use as few words as you can to explain what you want, and it's not good for clarity. I really can only take a guess at what you're trying to accomplish.

    What I think you want is: an array of objects and an index, and at any arbitrary point, you want to be able to change that index, and deactivate the previous object and activate the new one. Is that about right? If so, you don't need to loop at all. One method would be to make your index a property that handles the deactivation/activation when it gets changed:
    Code (csharp):
    1. public int activeIndex {
    2. get {
    3. return _activeIndex;
    4. }
    5. set {
    6. if (myArray != null && myArray.Length > 0) {
    7. //keep us in the range of the array, looping around as needed
    8. while (value < 0) { value += myArray.Length; }
    9. while (value > myArray.Length) { value -= myArray.Length; }
    10. myArray[_activeIndex].SetActive(false);
    11. myArray[value].SetActive(true);
    12. _activeIndex = value;
    13. }
    14. else {
    15. _activeIndex = value;
    16. }
    17. }
    18. }
    19. private int _activeIndex = 0;
    20.  
    21. public GameObject[] myArray;
    Then, at any time, you can set activeIndex to any number (or call ++ or -- on it, too) and it'll handle everything.
     
  3. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    True indeed apologies for such brief and rushed posting @StarManta.

    It's a weapon script with a weapon change method that use one button to change weapons only forward cycle and if the cycle goes beyond the total length, reset to the first in the array. Also remember what object was active last so when the instance occurs again set the last used weapon active

    So you're saying the active return method will do just fine???

    This is the script;

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class WeaponController : MonoBehaviour {
    7.  
    8.     public enum weaponType{ unArmed, HandGun, SubMachine, Heavy, Assualt, Special }
    9.     public  weaponType weaponT;
    10.  
    11.     public enum weaponTypeBurst{ unArmed_Burst, HandGun_Burst, SubMachine_Burst, Heavy_Burst, Assualt_Burst, Special_Burst }
    12.     public  weaponTypeBurst weaponTburst;
    13.  
    14.     public int curWeapon;
    15.     public int iCH;
    16.     public GameObject[] curWeapon_Go;
    17.  
    18.     public CustomEvents CustomEventsScript;
    19.  
    20.     public GameObject Text_Ammo_;
    21.     public Text Text_Ammo_text;
    22.     public GameObject Text_Clips_;
    23.     public Text Text_Clips_text;
    24.     public GameObject Player_;
    25.     public Player PlayerScript;
    26.  
    27.  
    28.     public int ReadyAmmo  = 30;        //  show this on GUI
    29.     public int ammoDifference;         // dont show this on GUI
    30.     public int ammo = 30;                 //  show this on GUI        // monetize ammo type
    31.     public int clips = 1;                //  show this on GUI          // monetize clip type
    32.     public int magCapacity = 7;     //  show this on GUI        // monetize clip extensions, change magCapacity per weapon
    33.  
    34.     public int ReloadAmmo_Needed;
    35.  
    36.     public void start(){
    37.         ammoTrack ();
    38.  
    39.     }
    40.  
    41.     public void ammoTrack(){
    42.         if(curWeapon != 0 ){
    43.             clips = ammo / magCapacity;
    44.             ammoDifference = ammo - (clips * magCapacity);
    45.             ReloadAmmo_Needed = magCapacity - ReadyAmmo;
    46.         }
    47.     }
    48.     public void SetWeapon(){
    49.         switch(curWeapon){
    50.             case 0: // unarmmed
    51.                 weaponT = weaponType.unArmed;
    52.                     if(weaponT == weaponType.unArmed){
    53.                         weaponTburst = weaponTypeBurst.unArmed_Burst;
    54.                         magCapacity = 0;
    55.                         clips = 0;
    56.                 }
    57.                 break;
    58.  
    59.             case 1: // handgun
    60.                 weaponT = weaponType.HandGun;
    61.                     if(weaponT == weaponType.HandGun){
    62.                         weaponTburst = weaponTypeBurst.HandGun_Burst;
    63.                         magCapacity = 14;
    64.                     }
    65.                 break;
    66.  
    67.             case 2: // subMachine
    68.                 weaponT = weaponType.SubMachine;
    69.                     if(weaponT == weaponType.SubMachine){
    70.                         weaponTburst = weaponTypeBurst.SubMachine_Burst;
    71.                         magCapacity = 30;
    72.                     }
    73.                 break;
    74.            
    75.             case 3: // heavy
    76.                 weaponT = weaponType.Heavy;
    77.                     if(weaponT == weaponType.Heavy){
    78.                         weaponTburst = weaponTypeBurst.Heavy_Burst;
    79.                         magCapacity = 14;
    80.                     }
    81.                 break;
    82.             case 4: // assualt
    83.                 weaponT = weaponType.Assualt;
    84.                     if(weaponT == weaponType.Assualt){
    85.                         weaponTburst = weaponTypeBurst.Assualt_Burst;
    86.                         magCapacity = 30;
    87.                        
    88.                     }
    89.                 break;
    90.             case 5: // special
    91.                 weaponT = weaponType.Special;
    92.                     if(weaponT == weaponType.Special){
    93.                         weaponTburst = weaponTypeBurst.Special_Burst;
    94.                         magCapacity = 1;
    95.                     }
    96.                 break;
    97.  
    98.             default :
    99.                 weaponT = weaponType.unArmed;
    100.                     if(weaponT == weaponType.unArmed){
    101.                         weaponTburst = weaponTypeBurst.unArmed_Burst;
    102.                         magCapacity = 0;
    103.                           clips = 0;
    104.                 }
    105.                 break;
    106.         }
    107.     }
    108.  
    109.  
    110.     public void Reload(){
    111.        
    112.                 if(ReadyAmmo == magCapacity){
    113.                     return;
    114.                 }
    115.                 if(ReadyAmmo == 0 && clips == 0 && ammo == 0 && ammoDifference == 0){
    116.                     return;
    117.                 }
    118.  
    119.                     if (ReadyAmmo > 0 && ReadyAmmo < magCapacity) {
    120.                         CustomEventsScript.PlayerEventTrigger (6); // play reload sound
    121.                         if (ammo < magCapacity) {
    122.                             ammo -= ammoDifference;
    123.                             ReadyAmmo = ammoDifference;
    124.                             ammo = 0;
    125.                         } else {
    126.                             ammo -= ReloadAmmo_Needed;
    127.                             ReadyAmmo = ReadyAmmo+ ReloadAmmo_Needed;
    128.                         }
    129.                     }
    130.                     if (ReadyAmmo == 0) {
    131.                         CustomEventsScript.PlayerEventTrigger (6);  // play reload sound
    132.                         if (ammo < magCapacity) {
    133.                             ammo -= ammoDifference;
    134.                             ReadyAmmo = ammoDifference;
    135.                             ammo = 0;
    136.                         } else {
    137.                             ammo -= ReloadAmmo_Needed;
    138.                             ReadyAmmo = ReadyAmmo+ ReloadAmmo_Needed;
    139.                            
    140.                         }
    141.                         //}
    142.                     }
    143.     }
    144.  
    145.     public void ChangeWeapon(){
    146.         int cW;
    147.         cW = curWeapon;
    148.         for(int i = cW; i < curWeapon_Go.Length; i++){
    149.             curWeapon++;
    150.             i = iCH;
    151.             //curWeapon_Go[i + 1].SetActive(true);
    152.             curWeapon_Go[i].SetActive(false);
    153.             SetWeapon ();
    154.         }
    155.         ChangeWeapon_LastOff ();  
    156.         SetWeapon ();
    157.  
    158.     }
    159.     public void ChangeWeapon_LastOff(){
    160.          int LastGo;
    161.         LastGo = curWeapon_Go.Length;
    162.         curWeapon_Go[iCH].SetActive(false);
    163.         //curWeapon_Go [i + LastGo - 1].SetActive (false);
    164.         curWeapon_Go [0].SetActive (true);
    165.         curWeapon = 0;
    166.     }
    167.  
    168.  
    169.     public void ChangeWeapon_Set(int CurWeapon){
    170.         switch(CurWeapon){
    171.             case 0: // unarmmed
    172.             curWeapon_Go[0].SetActive(true);
    173.             curWeapon_Go[1].SetActive(false);
    174.             curWeapon_Go[2].SetActive(false);
    175.             curWeapon_Go[3].SetActive(false);
    176.             curWeapon_Go[4].SetActive(false);
    177.             curWeapon_Go[5].SetActive(false);
    178.             break;
    179.         case 1: // handgun
    180.             curWeapon_Go[0].SetActive(false);
    181.             curWeapon_Go[1].SetActive(true);
    182.             curWeapon_Go[2].SetActive(false);
    183.             curWeapon_Go[3].SetActive(false);
    184.             curWeapon_Go[4].SetActive(false);
    185.             curWeapon_Go[5].SetActive(false);
    186.  
    187.             break;
    188.         case 2: // subMachine
    189.             curWeapon_Go[0].SetActive(false);
    190.             curWeapon_Go[1].SetActive(false);
    191.             curWeapon_Go[2].SetActive(true);
    192.             curWeapon_Go[3].SetActive(false);
    193.             curWeapon_Go[4].SetActive(false);
    194.             curWeapon_Go[5].SetActive(false);
    195.  
    196.             break;
    197.         case 3: // heavy
    198.             curWeapon_Go[0].SetActive(false);
    199.             curWeapon_Go[1].SetActive(false);
    200.             curWeapon_Go[2].SetActive(false);
    201.             curWeapon_Go[3].SetActive(true);
    202.             curWeapon_Go[4].SetActive(false);
    203.             curWeapon_Go[5].SetActive(false);
    204.             break;
    205.         case 4: // Assualt
    206.             curWeapon_Go[0].SetActive(false);
    207.             curWeapon_Go[1].SetActive(false);
    208.             curWeapon_Go[2].SetActive(false);
    209.             curWeapon_Go[3].SetActive(false);
    210.             curWeapon_Go[4].SetActive(true);
    211.             curWeapon_Go[5].SetActive(false);
    212.             break;
    213.         case 5: // Special
    214.             curWeapon_Go[0].SetActive(false);
    215.             curWeapon_Go[1].SetActive(false);
    216.             curWeapon_Go[2].SetActive(false);
    217.             curWeapon_Go[3].SetActive(false);
    218.             curWeapon_Go[4].SetActive(false);
    219.             curWeapon_Go[5].SetActive(true);
    220.             break;
    221.         default :
    222.             curWeapon_Go[0].SetActive(true);
    223.             curWeapon_Go[1].SetActive(false);
    224.             curWeapon_Go[2].SetActive(false);
    225.             curWeapon_Go[3].SetActive(false);
    226.             curWeapon_Go[4].SetActive(false);
    227.             curWeapon_Go[5].SetActive(false);
    228.             break;
    229.         }
    230.     }
    231.  
    232.     public void BuyAmmo(){
    233.         if(PlayerScript.CharMoneyOT > 100){
    234.             PlayerScript.CharMoneyOT -= 100;
    235.             ammo += 30;
    236.             CustomEventsScript.ShopEventTrigger (1);        //  enough, purchase sound
    237.         }
    238.     }
    239.  
    240.     public void SubtractAmmo(){
    241.                 if(ReadyAmmo > 0){
    242.                     ReadyAmmo--;
    243.                 }  
    244.     }
    245.     public void SetAmmo(){
    246.         switch(curWeapon){
    247.             case 0:
    248.                 ReadyAmmo = 0;
    249.                 break;
    250.             case 1:
    251.                 if (ammo > magCapacity) {
    252.                     ReadyAmmo = magCapacity;
    253.                 } else {
    254.                     ReadyAmmo = ammoDifference;
    255.                 }
    256.                 break;
    257.             case 2:
    258.                 if (ammo > magCapacity) {
    259.                     ReadyAmmo = magCapacity;
    260.                 } else {
    261.                     ReadyAmmo = ammoDifference;
    262.                 }
    263.                 break;
    264.             case 3:
    265.                 if (ammo > magCapacity) {
    266.                     ReadyAmmo = magCapacity;
    267.                 } else {
    268.                     ReadyAmmo = ammoDifference;
    269.                 }
    270.                 break;
    271.             case 4:
    272.                 if (ammo > magCapacity) {
    273.                     ReadyAmmo = magCapacity;
    274.                 } else {
    275.                     ReadyAmmo = ammoDifference;
    276.                 }
    277.                 break;
    278.             case 5:
    279.                 if (ammo > magCapacity) {
    280.                     ReadyAmmo = magCapacity;
    281.                 } else {
    282.                     ReadyAmmo = ammoDifference;
    283.                 }
    284.                 break;
    285.             default:
    286.                 ReadyAmmo = 0;
    287.                 break;
    288.             }
    289.     }
    290.     // Use this for initialization
    291.     void Awake () {
    292.         Text_Ammo_text = Text_Ammo_.GetComponent<Text>();
    293.         Text_Clips_text = Text_Clips_.GetComponent<Text>();
    294.         PlayerScript = Player_.GetComponent<Player> ();
    295.         ChangeWeapon_Set (curWeapon);
    296.         iCH = curWeapon;
    297.         SetWeapon ();
    298.         SetAmmo ();
    299.     }
    300.    
    301.     // Update is called once per frame
    302.     void Update () {
    303.         Text_Ammo_text.text = "Ammo: " + ReadyAmmo  +"/"+ ammo + "";
    304.         Text_Clips_text.text = "Clips: " + clips + "/" + magCapacity + "";
    305.         ammoTrack ();
    306.         //SetWeapon ();
    307.     }
    308. }
    309.  
     
  4. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Change Weapon block starts at line 145..
     
  5. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Thank you, ok I gave that a test run and I was going to use something similar to that; However it's only one issue as it stands. I currently have one button responsible for this task of changing weapons forward cycle. If I had a button for each weapon then your method along with the other method I thought of would work just fine. I need to do this with one button and track the last used weapon that way when the instance occurs again the iteration will start from the last used weapon.
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I don't think you even need a loop, surely you only need to deactivate the players current weapon, and active the next, only dealiong with 2 GO's; not the wholearray of them
    Code (CSharp):
    1. void CycleWep (){
    2.   array[current].setActive(false);
    3.   current = (current+1)%array.Length;
    4.   array[current].setActuve(true);
    5. }
     
    Kiwasi likes this.
  7. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    and then add and if in there to rest back to array[o] ???
     
  8. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Works great Now I just have to add the rest of things to do. so I ask is it the modulus addition that automatically makes it reset to zero?
     
  9. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
  10. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    there no need to manulaly reset to zero
    % length wraps the value automatically to the length of the array