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 whtrbt · Jul 19, 2011 at 11:55 AM · inputfreezeturn-based

Input in a turn-based game freezing

Trying to set up a turn-based game with a scheduler that selects which entity is allowed to take a turn.

When it's the player's turn, I want to be able to keep accepting input (and updating the player character entity and GUIs) until the player enters a commit command, at which point the control should be returned to the scheduler so it can assign the next turn to another entity.

I was having the scheduler call a 'take turn' function on the player entity, but I keep implementing infinite loops and so Unity freezes. I am unsure if what I am trying to do requires coroutines and yield, or if I'm just approaching this in the wrong way.

Could someone please describe a sensible method to achieve this. I'm using C#, if you want to provide any sample code.

EDIT: Added the current code I'm using below. I'd tried some even simpler methods before, but discarded them. I'd prefer if someone could explain where my understanding is failing, rather than the problem with this specific code. The player.Move function is currently just assigning the Vector3 to the player character transform's position.

PlayerController.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : EntityController {
 
     public float inputDelay = 10.0f;
     public Entity player;
     
     public bool GetPlayerInput() {
         float t = Time.time - inputDelay;
 
         if (Input.GetKeyUp(KeyCode.Space)) {
             return false;
         }
 
         if (Time.time > t + inputDelay) {
             t = Time.time;
             player.Move(new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")));
         }
         return true;
     }
 }


Manager.cs

 using UnityEngine;
 using System.Collections;
 
 public class Manager : MonoBehaviour {
 
     public PlayerController player;
     bool game = true;
 
     void Start () {
         while (game == true) {
             game = player.GetPlayerInput();
         }
     }
 
 }

Thanks for any help! :)

Comment
Add comment · Show 1
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 almo · Jul 19, 2011 at 12:01 PM 0
Share

Post a little code so we can see where your infinite loop problem is. Use the 010101 button to format it.

2 Replies

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

Answer by BerggreenDK · Jul 20, 2011 at 12:23 AM

I would make it with a State machine pattern.

Like: First you need to enumerate your states:

 enum GameState
 {
     PlayerTurn,
     OponentTurn,
     EndOfGame,
     StartOfGame,
     Pause,
     ... add as many as you need
 } 


Then later inside your Update loops, you implement this statemachine:

 switch(gameState)
 {
   case GameState.PlayerTurn:
      ... check for player stuff here 
      ... use functions/methods to be able to reuse the checks for multiple states)
   break;

   case GameState.OponentTurn:

      ... calculate how the oponent moves
      ... show animations of oponents movement
      ... etc.

   break;

   default: // catch any states you havent programmed yet
       Debug.Log("state uknown: " + gameState);
   break;
 }

This way the global variable gameState will be telling which functions to call each update/frame and your game does not freeze, it just changes behaviours.

Comment
Add comment · Show 3 · 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 whtrbt · Jul 20, 2011 at 01:55 AM 0
Share

Thanks, I will give this a shot tonight.

avatar image whtrbt · Jul 20, 2011 at 09:55 AM 0
Share

Yes, perfect. :)

avatar image BerggreenDK · Jul 20, 2011 at 10:10 AM 0
Share

Great! Glad I could be of help.

avatar image
1

Answer by Dreamora · Jul 19, 2011 at 03:36 PM

You must never use an infinite loop thats not in a coroutine and yields. because when this loop enters, there will be no other code running at all anymore in the engine beside of this, if you don't correctly yield.

Comment
Add comment · Show 1 · 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 whtrbt · Jul 19, 2011 at 11:53 PM 0
Share

I understand that I can't use an infinite loop. I guess I'm trying to ask if there's a simpler way for me to implement this without running into an infinite loop, or if the only way is to use coroutines.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to store player input in turn-based game? 0 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Input axes freeze from opposite input rather than cancel out! 1 Answer

Unity game freeze inside Sandboxie on Windows Server 2019 or newer version because of raw input failed to initialize 0 Answers

Input locks Up - Game Ignores Input. 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