0

I'll try to explain this the best I can. Let's say I have 2 if statements that both send a Debug.Log message to console, like this:

if(Input.GetKeyDown("c")){
    Debug.Log("C was pressed!");
}

if(Input.GetKeyDown("b")){
    Debug.Log("b was pressed!");
}

We of course know that we'll get these messages when we press the appropriate keys. But what if I want to get another script to trigger these if statements instead of the keyboard doing it?

If I have a list of if statements that are taking in a keyboard input like these ones, is there a way to trigger these if statements through code or would I have to change the condition?

1
  • You want to call Debug.Log("C was pressed!"); though code? Commented Mar 4, 2018 at 21:30

2 Answers 2

2

From your question, I don't think that simulating keyboard input is the way to go here. What I suppose would be better, is putting the statements inside new methods, which can be called from other scripts.

public void HandleInputC ()
{
    Debug.Log("C was pressed (or simulated).");
}
public void HandleInputD ()
{
    Debug.Log("D was pressed (or simulated).");
}

private void Update ()
{
    if (Input.GetKeyDown("c"))
        HandleInputC();
    if (Input.GetKeyDown("d"))
        HandleInputD();
}

This also makes it easier and more readable to make extensions to the individual methods, while keeping an overview on their purpose.

Sign up to request clarification or add additional context in comments.

Comments

2

I hope I understood your problem correctly. Yes, its definitely possible. Let's say you want to make a ghost of your player which you will replay later. So save all keystrokes the player makes in a list and then return keystrokes from this list. The idea is to make a custom KeyStrokeRecorder class which has a method called GetKey. Then instead of calling Input.GetKeyDown call KeyStrokeRecorder.instance.GetKey. See the code below.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class KeyStrokeRecorder : MonoBehaviour {

    public class KeyStroke {
        public enum KeyState {
            Idle,
            Pressed,
            Released,
        }
        public KeyCode keycode; 
        public string time;
        public KeyState state;          
    }       

    public List<KeyStroke> mKeyStrokes;
    public RecorderState recorderState = RecorderState.Idle;
    public static KeyStrokeRecorder instance;

    void Awake(){
        instance = this;    
    }

    void Start () {

    }

    void Update () {
         if( isRecording || isPlayback)
             gameTime += Time.deltaTime;
    }

    float gameTime = 0;    
    bool isRecording = false;       
    bool isPlayback = false;
    public bool GetKey(KeyCode keycode)
    { 
        if( isPlayback )
        {
            for(int i=0; i<mKeyStrokes.Count; i++)
            {
                float tTime;
                float.TryParse(mKeyStrokes[i].time, out tTime);

                if( mKeyStrokes[i].keyname == keycode.ToString() )
                {
                    if( gameTime > tTime-0.5f && gameTime < tTime+0.5f )
                    {
                        if( mKeyStrokes[i].state == KeyStroke.State.Pressed)
                            return true;
                        else
                            return false;
                    } 
                } 
            }    
            return false;
        }
        else if( isRecording )
        {
            KeyStroke keystroke = new KeyStroke();
            keystroke.keyname = keycode.ToString();
            keystroke.time = gameTime.ToString();

            if( Input.GetKeyDown(keycode) )
            {                   
                keystroke.state = KeyStroke.State.Pressed;    
                mKeyStrokes.Add(keystroke);                 
            }
            else if( Input.GetKeyUp(keycode) )
            {                   
                keystroke.state = KeyStroke.State.Released;     
                mKeyStrokes.Add(keystroke);
            }
        }    
        return Input.GetKey(keycode);
    }
}

Hope this gives you some idea.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.