Home > Net >  Is it possible to turn only one bool to true and putting other bools in an array to false without us
Is it possible to turn only one bool to true and putting other bools in an array to false without us

Time:10-15

I was wondering if only one bool could return on true, but the other bools would be set on a false inside of an array. I really want to make this code alot better, but I don't quite know how to make it better. So is it possible to do that without alot of if statements or using:

using System.Collections;
using System.Collections.Generic;
using NodeCanvas.Tasks.Actions;
using UnityEngine;

[System.Serializable]
public class tableClass
{
    public DotlingFaceTalker[] FaceTalkers;
}

public class WillTheyTalk : MonoBehaviour
{

    [Header("List from Table class: ")] public List<tableClass> backgrounddotlingGroups;

    private void Start()
    {
        foreach (var t in backgrounddotlingGroups)
        {
            for (var j = 0; j < t.FaceTalkers.Length; j  )
            {
                t.FaceTalkers[j] = GetComponentInChildren<DotlingFaceTalker>();
            }
        }
    }

    public void IsCheckingifTalking()
    {
        const int groupOf2 = 2;
        const int groupOf3 = 3;
        const int groupOf4 = 4;
        foreach (var t in backgrounddotlingGroups)
        {
            for (var j = 0; j < t.FaceTalkers.Length; j  )
            {
                Debug.Log(j);
                switch (j)
                {
                    case groupOf2:
                        GroupsOf2();
                        break;
                    case groupOf3:
                        GroupsOf3();
                        break;
                    case groupOf4:
                        GroupsOf4();
                        break;
                }
            }
        }
    }

    private void GroupsOf2()
    {
        foreach (var t in backgrounddotlingGroups)
        {
            for (var i = 0; i < 2; i  )
            {
                if (t.FaceTalkers[0].conversationRunning == t.FaceTalkers[0]._sentenceTimer.UpdateState() &&
                    t.FaceTalkers[0].mouthOpen == t.FaceTalkers[0]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[1].conversationRunning = t.FaceTalkers[1]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[1].mouthOpen = t.FaceTalkers[1]._wordTimer.UpdateState() == false;
                    return;
                }

                if (t.FaceTalkers[1].conversationRunning == t.FaceTalkers[1]._sentenceTimer.UpdateState() &&
                    t.FaceTalkers[1].mouthOpen == t.FaceTalkers[1]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[0].conversationRunning = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[0].mouthOpen = t.FaceTalkers[0]._wordTimer.UpdateState() == false;
                    return;
                }
            }
        }
    }

    private void GroupsOf3()
    {
        foreach (var t in backgrounddotlingGroups)
        {
            for (var i = 0; i < 3; i  )
            {

                if (t.FaceTalkers[0].conversationRunning == t.FaceTalkers[0]._sentenceTimer.UpdateState() &&
                    t.FaceTalkers[0].mouthOpen == t.FaceTalkers[0]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[1].conversationRunning = t.FaceTalkers[1]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[1].mouthOpen = t.FaceTalkers[1]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[2].conversationRunning = t.FaceTalkers[2]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[2].mouthOpen = t.FaceTalkers[2]._wordTimer.UpdateState() == false;
                    return;
                }

                if (t.FaceTalkers[1].conversationRunning == t.FaceTalkers[1]._sentenceTimer.UpdateState() && t.FaceTalkers[1].mouthOpen == t.FaceTalkers[1]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[0].conversationRunning = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[0].mouthOpen = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[2].conversationRunning = t.FaceTalkers[2]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[2].mouthOpen = t.FaceTalkers[2]._sentenceTimer.UpdateState() == false;
                    return;
                }

                if (t.FaceTalkers[2].conversationRunning == t.FaceTalkers[1]._sentenceTimer.UpdateState() &&
                    t.FaceTalkers[2].mouthOpen == t.FaceTalkers[2]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[0].conversationRunning = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[0].mouthOpen = t.FaceTalkers[0]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[1].conversationRunning = t.FaceTalkers[1]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[1].mouthOpen = t.FaceTalkers[1]._wordTimer.UpdateState() == false;
                    return;
                }
            }
        }


    }

    private void GroupsOf4()
    {
        foreach (var t in backgrounddotlingGroups)
        {
            for (var i = 0; i < 4; i  )
            {

                if (t.FaceTalkers[0].conversationRunning == t.FaceTalkers[0]._sentenceTimer.UpdateState() && t.FaceTalkers[0].mouthOpen == t.FaceTalkers[0]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[1].conversationRunning = t.FaceTalkers[1]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[1].mouthOpen = t.FaceTalkers[1]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[2].conversationRunning = t.FaceTalkers[2]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[2].mouthOpen = t.FaceTalkers[2]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[3].conversationRunning = t.FaceTalkers[3]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[3].mouthOpen = t.FaceTalkers[3]._wordTimer.UpdateState() == false;
                    return;
                }

                if (t.FaceTalkers[1].conversationRunning == t.FaceTalkers[1]._sentenceTimer.UpdateState() && t.FaceTalkers[1].mouthOpen == t.FaceTalkers[1]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[0].conversationRunning = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[0].mouthOpen = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[2].conversationRunning = t.FaceTalkers[2]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[2].mouthOpen = t.FaceTalkers[2]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[3].conversationRunning = t.FaceTalkers[3]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[3].mouthOpen = t.FaceTalkers[3]._wordTimer.UpdateState() == false;
                    return;
                }

                if (t.FaceTalkers[2].conversationRunning == t.FaceTalkers[1]._sentenceTimer.UpdateState() && t.FaceTalkers[2].mouthOpen == t.FaceTalkers[2]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[0].conversationRunning = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[0].mouthOpen = t.FaceTalkers[0]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[1].conversationRunning = t.FaceTalkers[1]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[1].mouthOpen = t.FaceTalkers[1]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[3].conversationRunning = t.FaceTalkers[3]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[3].mouthOpen = t.FaceTalkers[3]._wordTimer.UpdateState() == false;
                    return;
                }
                
                if (t.FaceTalkers[3].conversationRunning == t.FaceTalkers[3]._sentenceTimer.UpdateState() && t.FaceTalkers[3].mouthOpen == t.FaceTalkers[3]._wordTimer.UpdateState())
                {
                    t.FaceTalkers[0].conversationRunning = t.FaceTalkers[0]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[0].mouthOpen = t.FaceTalkers[0]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[1].conversationRunning = t.FaceTalkers[1]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[1].mouthOpen = t.FaceTalkers[1]._wordTimer.UpdateState() == false;
                    t.FaceTalkers[2].conversationRunning = t.FaceTalkers[2]._sentenceTimer.UpdateState() == false;
                    t.FaceTalkers[2].mouthOpen = t.FaceTalkers[2]._wordTimer.UpdateState() == false;
                    return;
                }
            }
        }
    }
}

I was just wondering, because I really want to make my code alot better... thank you in advance...

CodePudding user response:

Create a function that takes an index of the bool array. What you need to do is pass the index to that function and that function will set that index to true, and others to false. For example

public void SetTrue(int index){ 
  
for (int x=0; x<arraySize; x  )   
 if (arry[index]==x){   
     array[x]=true;   
     continue;// this break the current iteration.          
}   
    array[x]=false;
 
}    
// you can do it better by passing choice as well. 
public void SetBool(int index, bool choice ){    

for (int x=0; x<arraSize; x  )    
    if (arry[index]==x){    
    array[x]=choice ;    
    }    
    continue;// this break the current iteration.        
    }    
    array[x]=false;    
 
}

Thanks.

  •  Tags:  
  • c#
  • Related