Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by ukaaron1 · Jul 14, 2015 at 02:34 PM · replay

How do I get my game to remember player input and perform those actions later?

Hi, I am creating a game which involves the player planning his actions before initiating them.

I have a smaller "ghost" object which is controlled using basic translate position buttons.

What I am wondering is if there was a way to get my game to remember which buttons where pressed and in what order?

The hopes is that once the player has planned out his string of actions they will press an "Action" button which will perform the actions for the player in the sequence they had chosen but on the real player object, not the ghost object which they used to plan their turn.

I hope that made sense.

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 ukaaron1 · Jul 14, 2015 at 07:30 AM 0
Share

How would one do that?

1 Reply

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

Answer by InfiniBuzz · Jul 14, 2015 at 03:13 PM

You could use a Queue since you probably want to execute the first pressed action first. Then you need to decide in what type you store the inputs (strings, ints etc). Assuming you use int this would mean:

  • Button1 = 0

  • Button2 = 1

  • and so on...

Using C# this would look something like this:

 // Initialize the queue at start
 Queue<int> userInputs = new Queue<int>();
 ...
 // On user input (nuberOfInput is of type int)
 userInputs.Enqueue(nubmerOfInput);     // Enqueue(0) for Button1
 ...
 // On "Action"
 nextButtonNumber = userInputs.Dequeue();

Note Enqueue() puts one element in the queue and Dequeue() removes and returns the first element in the queue so you have to call it repetitive.

Edit Sticking to the queue attempt (maybe not the most elegant):

  public class PlayerController : MonoBehaviour 
  {
  private Queue<int> playerInput = new Queue<int>();

  public void MovePlayerForward()
  {
      playerInput.Enqueue(0);
  }
 
  public void MovePlayerBackward()
  {
      playerInput.Enqueue(1);
  }
 
  public void MovePlayerLeft()
  {
      playerInput.Enqueue(2);
  }
 
  public void MovePlayerRight()
  {
      playerInput.Enqueue(3);
  }
 
  public void RotateRight()
  {
      playerInput.Enqueue(4);
  }
 
  public void RotateLeft()
  {
      playerInput.Enqueue(5);
  }

  public void OnActionButtonPressed()
  {
      while(playerInput.Count > 0)
      {
            switch(playerInput.Dequeue())
            {
                 case 0:
                     transform.Translate (0, 0, 1);
                     break;
                 case 1:
                     transform.Translate (0, 0, -1);
                     break;
                 case 2:
                     transform.Translate (-1, 0, 0);
                     break;
                 case 3:
                     transform.Translate (1, 0, 0);
                     break;
                 case 4:
                     transform.Rotate (0, 45, 0);
                     break;
                 case 5:
                     transform.Rotate (0, -45, 0);
                     break;
            }
      }
  }

}

You can also put the while in a coroutine or adjust it to fit your needs because now the actions happen pretty fast. Or if you want it turn by turn just remove the while-loop.

In case you also need to record the times between the button presses let me know.

Comment
Add comment · Show 10 · 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 NoseKills · Jul 14, 2015 at 03:25 PM 1
Share

If this is not turn based but rather anlogous movement, you probably also need to record when and how long each button was pressed (record pressed timestamp and released timestamp). If the key presses are used to move physics objects and you need the system to be deter$$anonymous$$istic, you probably have to record and play back all the actions in fixedUpdate() and store the number of fixedUpdate it happened.

avatar image InfiniBuzz · Jul 14, 2015 at 03:39 PM 0
Share

good point! I should add that my answer is based on the statement "... which buttons where pressed and in what order?"

avatar image InfiniBuzz · Jul 14, 2015 at 08:11 PM 1
Share

I updated my answer have a look. The numbers 0 to 5 stand for the different buttons pressed. I hope this is the behaviour you want to achieve.

avatar image InfiniBuzz · Jul 14, 2015 at 08:46 PM 2
Share

Exactly, something like:

 public void OnActionButtonPressed()
 {
     StartCoroutine(StepbyStep());
 }
 
 IEnumerator StepbyStep() {
     while(playerInput.Count > 0) {
         switch(playerInput.Dequeue()) {
              // cases same as before
         }        
         yield return new WaitForSeconds(1.0f);
     }
 }


avatar image NoseKills · Jul 14, 2015 at 09:52 PM 1
Share

Good job helping a dude out! One "best practices" kind of detail that everyone should always stick to I$$anonymous$$O... you could use constants ins$$anonymous$$d of "magic numbers" 0-5. So declare

 public const int $$anonymous$$EY_FWD   = 0;
 public const int $$anonymous$$EY_BAC$$anonymous$$  = 1;
 public const int $$anonymous$$EY_LEFT  = 2;
 public const int $$anonymous$$EY_RIGHT = 3;
 public const int $$anonymous$$EY_ROT_R = 4;
 public const int $$anonymous$$EY_ROT_L = 5;

and use those everywhere

   public void RotateLeft()
   {
       playerInput.Enqueue($$anonymous$$EY_ROT_L);
   }
 ...
     
   switch(playerInput.Dequeue())
   {
       case $$anonymous$$EY_FWD:
          transform.Translate (0, 0, 1);
          break;

It makes everything more readable and if you need to change one of those numbers later (lets say you find out you can't use 0 as a code for forward because of some encoding problems),this way you only have to change it in one place!

Show more comments

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

Timescale Issues(Making a replay function for game ) 0 Answers

Unity 3 animation replay? 2 Answers

Lerping saved position in FixedUpdate causes inconsistent in speed with the reply ! 0 Answers

Replay system for android, need help with precision (framerate problem) 1 Answer

Is it possible to record gameplay via a GameObject Camera and then play that video file in-game? If so, how would I go about doing this? 0 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