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 PuzzleBuilder · Nov 06, 2017 at 06:22 PM · scripting problemuser interfacecoroutinesdesign-patterns

How to keep program linear and synchronous while getting user input?

I'm trying to convert my Java code for a Monopoly game into a Unity game. The game logic follows a very linear sequence of events that lends itself to synchronous execution.

The main obstacle I'm running into is with user input. Whenever I needed user input in Java, I'd simply use a Scanner object that would halt the execution of the program until an input is given. I'm not sure how to accomplish this in Unity, at least the halting of the code part. I tried making a communication script between the Unity UI and my program logic. I've been successful in using the script to display the UI, and when a button is clicked, I can call a method on the script. The problem is that I don't know how to make the script wait to return the input until after the button method is called.

Here's an example of some code where I need the user's input:

 public override void landOn(Player player){
         bool buyIt = uI.askIfBuyProperty();
         if (buyIt == true) buyProperty();
         ...etc.
     }


Here is the code in my UI communication script. Unfortunately the while loop causes the program to freeze:

 public class UIControllerScript : MonoBehaviour {
     public GameObject popupTemplate;
     private bool answer;
     private bool replied = false;
 
     public bool askIfBuyProperty()
     {
         popupTemplate.SetActive(true);
         while (!replied) {}
         return answer;
 
     }

     // method called when button is clicked
     public void setAnswer(int ans)
     {
         replied = true;
         answer = ans;
         popupTemplate.SetActive(false);
     }
 }


I've considered having having my UI object call methods in my logic code whenever an input is given, but this would require me to restructure my entire project. I would need a new listener method for every UI input. Also, I'd need to somehow keep track of when a player's turn is over, who goes next, etc., because there is no set linear flow through the steps of the game. I really hope there is an easier way.

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
0
Best Answer

Answer by MaxGuernseyIII · Nov 07, 2017 at 02:10 AM

The Observer pattern (which .NET handily supports in the form on events) and the State pattern are how I manage this kind of thing in pretty much every domain - games or otherwise - but you've already excluded refactoring to recognize those patterns in your problem. It seems like, since you've considered it once, I'd implore you to consider it one more time.

If not, I guess I'd try to contort a coroutine into doing what you want. That would still involve some refactoring.

Your code would end up looking something like this:

 public override void landOn(Player player)
 {
   StartCoroutine(LandOnCoroutine(player));
 }
 
 IEnumerable LandOnCoroutine(Player player)
 {
   YesNo buyIt = uI.askIfBuyProperty();
   while (!buyIt.Answered) yield return null;
   if (buyIt.Value == true)
     yield return StartCoroutine(buyProperty());
   ...etc.
 }

You have to write YesNo yourself. You can get fancier by making your "answer receipt" object generic rather than being coupled to a return type of bool and by making it inherit CustomYieldInstruction to integrate more smoothly into a coroutine's cooperative yield mechanism.

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 PuzzleBuilder · Nov 07, 2017 at 06:52 PM 0
Share

Thanks for your answer. I was also considering the coroutine option, but it seems like I'd need to make just about everything into a coroutine, right? Which becomes pretty un-managable. I'm thinking about using the observer pattern, as you recommended. In that case, if my understanding is correct, the Subject object would act as a sort of facade between user input and the game state, and it would contain methods that are triggered by button clicks and such, right? $$anonymous$$y player objects (which keep track of the internal state of things) would then act as observers, and change their states based on notifications from the Subject? I haven't used this pattern before, so I want to make sure that I'm implementing it correctly.

avatar image MaxGuernseyIII PuzzleBuilder · Nov 07, 2017 at 07:01 PM 0
Share

Yes. That's right. For example, you might have an interface like this:

 public interface PurchasingUI
 {
   event Action OnPurchase;
 
   void PurchasingIsPossible(Property p, int price);
   void PurchasingIsNotpossible();
 }

As you land on different cells, change players, or complete the sale of properties, you would keep the purchasing UI up to date on whether or not it was possible to purchase a property. Your $$anonymous$$onoBehaviour would translate that into the visibility and/or enablement of various buttons (e.g.). The appropriate UI artifacts, when interacted with, would then cause the OnPurchase method to be raised, which would cause your game's state to update.

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

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

I want to make a Monster Spawner that spawns a monster in every x seconds (in real time); but I can't get timer to work properly... 1 Answer

How do I access Singleton Pattern Script in Unity3d C# 1 Answer

Mouse Cursor Problem 0 Answers

How to stop one specific instance of a Coroutine 2 Answers

How do I make GUI to appear on a game object instead of rendering it on screen? 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