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 /
  • Help Room /
avatar image
0
Question by Pateeto · Jun 05, 2018 at 07:33 AM · coroutines

Decision making coroutine via input

I'm working on a sidescroller game that has the character getting across events. This triggers the text when he collides with the area of the event and waits for the input ( 2 possible options ) explaining the event. I have different lists with the functions for the events, each function has a simple IF statement with a GetKeyDown to switch between one or another option. That calls another function that makes some changes on sliders that tracks the points in that area, shows a text with the outcome and prompts the player to click to continue, which resets the loop and starts again, pulling another random event from the lists depending on the points in the sliders.

My first approach was to set a bool for the event being active on FALSE and then getting into an UPDATE function once it's on TRUE. Of course...pulling the events from a list and using Update causes the problem of calling all the events from the list. So I got to know the coroutines! How would I translate the fact that I'm trying to listen the input of one or another key while the event is active, outside the UPDATE function? I've tried changing the events function into IEnumerators, ending with YIELD RETURN NULL. That caused unity to freeze :P... Here is the sample code (I'm kind of new to game programming, sorry if the code is messy):

 {
 
     private static System.Random rnd = new System.Random();         //Construyo el Random de system para pasar el int que va a elegir un index de la lista.
     public static EventManager events;                              //Instancia pública para llamar desde otros scripts.
     bool managerEventOn = false;                                    //Booleano para controlar si hay un evento en marcha o no.
     public int moralPoolSizeTier1 = 10;                             //Tamaño del pool para los eventos nivel 1 de Moral.
     private Vector2 objectPoolPosition = new Vector2(-15, -25);     //A holding position for our unused columns offscreen.
     List<Action> InsanityList;
     List<Action> moralList;
     List<Action> wealthList;
     List<Action> encountersList;
 
     public void Start()
     {
         InsanityList = new List<Action>();
         InsanityList.Add(() => seTeQuemaElAsado());
 
         moralList = new List<Action>();
         moralList.Add(() => eventVillagerInNeed());
         moralList.Add(() => seTeQuemaElAsado());
 
         wealthList = new List<Action>();
 
         encountersList = new List<Action>();
     }
 
     //Empieza la prueba 
     IEnumerator WaitForKeyDown(KeyCode keyCode)
     {
         while (!Input.GetKeyDown(keyCode))
             yield return null;
     }
 
 
 
 
     private void Update() 
     {
         Player playerActions = GameObject.Find("Player").GetComponent<Player>();
         while (managerEventOn)
         {
             int r = rnd.Next(moralList.Count);
             moralList[r]();
             Time.timeScale = 1;
         }
         
 
     }
 
     public void switchingEvent()
     {
         managerEventOn = true;
 
         return;
     }
 
 
 
     public void eventVillagerInNeed() //Antes de IEnumerator era Public Void
     {
         Player playerActions = GameObject.Find("Player").GetComponent<Player>();
         managerEventOn = true;
         Text eventText;
         //Setea el texto del evento en cuestión.
         Text textitos = GameObject.Find("eventText").GetComponent<Text>();
         textitos.text = "Un aldeano pide tu ayuda. Se lo ve herido... ";
         //El jugador elige entre el bien y el mal...
         if (Input.GetKeyDown("c"))
         {
             textitos.text = "Le das un garrotazo en la nuca y le robás toda la papa (Moral -2). Click para continuar.";
             //Los stats que cambian y cómo cambian.
             playerActions.modifyStat("reduceMoral", 2);
             managerEventOn = false;
         }
 
         else if (Input.GetKeyDown("v"))
         {
             textitos.text = "Le das mucho amor y ayudás al pobre hombre (Moral +4). Click para continuar.";
             //Los stats que cambian y cómo cambian.
             playerActions.modifyStat("addMoral", 4);
             managerEventOn = false;
         }
         return;
     }
 
     public void seTeQuemaElAsado()
     {
         Player playerActions = GameObject.Find("Player").GetComponent<Player>();
         managerEventOn = true;
         Text eventText;
         //Setea el texto del evento en cuestión.
         Text textitos = GameObject.Find("eventText").GetComponent<Text>();
         textitos.text = "El asadito se te esta prendiendo fuego... ";
         //El jugador elige entre el bien y el mal...
         if (Input.GetKeyDown("c"))
         {
             textitos.text = "Apagas el fuego con la pija (Demencia +3). Click para continuar.";
             //Los stats que cambian y cómo cambian.
             playerActions.modifyStat("reduceSanity", 3);
             managerEventOn = false;
         }
 
         else if (Input.GetKeyDown("v"))
         {
             textitos.text = "Buscas agua y apagas el incendio como un ser humano (Demencia -1). Click para continuar.";
             //Los stats que cambian y cómo cambian.
             playerActions.modifyStat("addSanity", 50);
             managerEventOn = false;
         }
          return ;
     }
 
 
 
 }
Comment
Add comment · Show 2
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 MagnaDever · Jun 05, 2018 at 07:46 AM 0
Share

why you can't check keydown in update?

avatar image Pateeto · Jun 05, 2018 at 08:22 AM 0
Share

Ideally, one event would be picked from its list and it would be used for the rest of the flow, but it keeps changing events from inside the list. I couldn't manage to make it use the first value retrieved randomly alone.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pateeto · Jun 05, 2018 at 08:17 AM

Right now, what happens when running on Unity is that the events flick infinitely in a loop when the character meets an event. It is like it's bringing all the events in the list while being on TRUE so it never stops on the first event it gets from the list.

Comment
Add comment · 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

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

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

Do something for 10 sec 2 Answers

Why Is This Coroutine Freezing my Unity5 editor? 2 Answers

Unity 5.6 - Get list of animations 1 Answer

StopCoroutine() Not Working 2 Answers

Spawner within range 1 Answer


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