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 /
avatar image
0
Question by Gabsparks · Jul 24, 2018 at 12:08 AM · updateenumstates

Update not behaving like it should with enum states

Hi! I'm making a turn-based RPG and there is this script of the action choices where the char options should be. My problem is: if the player choose one action, the next one should have the first one as the pre-selected choice in the box, but this is not happening. For example, if the char called "X" selects the third option, which is use potion, the next char, let's call "Y", should have the first option as the pre-selected choice, "Attack", regardless of what happened in the previous turn. Just works when the previous turn comes from an enemy. Anyway, a - not so - little bit of my codes: First, I have these states of the battle enumerated by an enum:

     //States of the Battle
     [HideInInspector]
     public enum ActionBoxStates
     {
         BEGINTURN,
         MAIN,
         BLOCKED,
         INFO
     }
     [HideInInspector] public static ActionBoxStates currentState;

The "BEGINTURN" state is basically where the choice '1' will be pre-selected. "MAIN" is where the player will navigate through choices, like attack, use skills, use potion, etc. So, my Update function is like that:

 void Update () {
         Debug.Log("ActionBox: " + currentState);
         //The start point in every new turn
         if(currentState == ActionBoxStates.BEGINTURN) 
         {
             currentState = ActionBoxStates.MAIN;
             selectedOption = 1;
         }
 
         if (BattleManagerScript.BattleStates.PLAYERCHOICE == BattleManagerScript.currentState)
         {
             if(ActionBoxStates.MAIN == currentState)
             {
                 //SELECT SOMETHING
                 if (Input.GetKeyDown(KeyCode.Return))
                 {
                     if (selectedOption == 1)
                     {
                         //I did a lot of stuff here not related to the enum, so I'm cleaning these codes for the question purpose.
                         currentState = ActionBoxStates.BLOCKED;
                         BattleManagerScript.currentState = BattleManagerScript.BattleStates.ENDTURNCONFIRMATION;
                         BattleManagerScript.NextTurn();
                     }
                     if (selectedOption == 3)
                     {
                         //All the same here =)
                         currentState = ActionBoxStates.BLOCKED;
                         BattleManagerScript.currentState = BattleManagerScript.BattleStates.ENDTURNCONFIRMATION;
                         BattleManagerScript.NextTurn();
                     }
                 }
             }
 
             //END TURN
             if (Input.GetKeyDown(KeyCode.Space))
             {
                 currentState = ActionBoxStates.BLOCKED;
                 BattleManagerScript.currentState = BattleManagerScript.BattleStates.ENDTURNCONFIRMATION;
                 confirmationBox.SetActive(true);
             }
         }
 
         if (ActionBoxStates.BLOCKED == currentState)
         {
             fade = true;
         }
         else
         {
             fade = false;
             VerifyMovement();
         }
 
         FadePanel(fade);
     }

The "BattleManagerScript" is where the turns are organized and it's "NextTurn" function simple does that:

 public static void NextTurn(){
     ReOrderList();
         NextBattleState();
         ActionBoxManagerScript.ResetActions();
 
         TimelineManagerScript.ArrangeTimeline();
     }

"Next Battle State" simple organize the BattleManager's enum by "PLAYERCHOICE" and "ENEMYCHOICE" by their tags. I guess there's nothing wrong with it, so I won't post the code here. The "ResetActions" of my main script here is doing this:

 public static void ResetActions()
     {
         if(BattleManagerScript.BattleStates.PLAYERCHOICE == BattleManagerScript.currentState)
         {
             currentState = ActionBoxStates.BEGINTURN;
             anim.Rebind(); //will put the chosen option's arrow in the first choice
             //I'm using the Debug function here and is showing the state BEGINTURN in currentState, so this code is running and the enum state is changing
             Debug.Log("Restart turn with the state: " + currentState);
         }
         else
         {
             currentState = ActionBoxStates.BLOCKED;
             //When it's an enemy turn, the ActionBox will be blocked, and this is also running properly because in enemy's turn the actions are locked.
         }
     }

Well, if the enum state is changing in ResetActions(), and it absolutely is, why on Update it doesn't Debug located in Update clearly shows that it doesn't change, it's already in "MAIN" state. Everything occurs as it should when the turn changes from one enemy to a player's char. The "currentTurn" goes to "BEGINTURN" state like it should when the enemy finishes it's turn, but when it changes to one char to another char, the problem appears.

PS: I tend to overexplain, sorry for it and for my rusty english, it' not my mother tongue.

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

1 Reply

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

Answer by Liaram · Jul 24, 2018 at 03:09 AM

I'm not sure anyone can debug the code based on the snippets here. But since it's a turn-based game I wouldn't change the states inside the update loop since it's not the kind of thing that has to happen 60 times a second. It's more of a risk than a benefit doing it in update since one typo could send the state flip flopping at an alarming rate. Also not sure if you assign the states using integers at some point. If you do I'd recommend declaring the enum like this to make sure the values never change:

 public enum ActionBoxStates
 {
     BEGINTURN = 0,
     MAIN = 1,
     BLOCKED = 2,
     INFO = 3,
 }

Unity stores the enum values as integers for every public variable so moving them around could mess things up. By adding the numbers the stored values will keep on matching. Using [HideInInspector] would not change that. Only [NonSerialized] clears the stored values. Sorry I couldn't be of more help. Happy debugging :-)

Comment
Add comment · Show 2 · 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 Gabsparks · Jul 24, 2018 at 01:45 PM 0
Share

Hi, thanks for the reply! I didn't used integers to assign the states and I guess I won't need to change their values, so I guess I won't need to use your example of declaration. I liked what you said about not changing the states inside the update (I really don't need things changing 60x per second), but I'm not really sure how could I change my code to a less risky version. Anyway, I'll try, if I find a solution I'll share here! =)

avatar image Gabsparks · Jul 24, 2018 at 02:15 PM 0
Share

Just updating: I solved the mystery! I removed the "BEGINTURN":

 //States of the Battle
      [HideInInspector]
      public enum ActionBoxStates
      {
          $$anonymous$$AIN,
          BLOC$$anonymous$$ED,
          INFO
      }
      [HideInInspector] public static ActionBoxStates currentState;

And I made the "selectedOption" a static variable. Doing this, I could change it directly on the function "ResetActions" like this, eli$$anonymous$$ating the need of changing stuff on update (which now only listen if the player pressed something and if is his turn:

 public static void ResetActions()
      {
          if(Battle$$anonymous$$anagerScript.BattleStates.PLAYERCHOICE == Battle$$anonymous$$anagerScript.currentState)
          {
              currentState = ActionBoxStates.$$anonymous$$AIN;
              anim.Rebind();
              selectedOption = 1;
          else
          {
              currentState = ActionBoxStates.BLOC$$anonymous$$ED;
          }
      }


Well, it looks like a simple change, but doing this I had to change a lot in my code, since "selectedOption" was used in many non-static functions. Either way, I achieved the solution and I can feel that my code is more clean now. I'm marking your answer as the acceptable one since you opened the doors for me to get in the answer and was very helpful. I believe it will help other if they get themselves in the same difficult. BTW, it was a happy debugging hehe. Cheers! :D

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

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

apply something on enum 1 Answer

How can I check if I pressed F in the class other than Update()? C# 2 Answers

Enum and Variable with similar name resulting in error 1 Answer

Best Way To Script State-Specific Behavior 1 Answer

How do i check enum state from a length of found GameObjects? 2 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