playMaker

Author Topic: Convert String to KeyCode?[SOLVED]  (Read 8373 times)

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Convert String to KeyCode?[SOLVED]
« on: May 02, 2013, 06:21:43 PM »
Hello, I've been trying to come up with a solution to this for some time but it appears to be more complex than I anticipated.

Basically I have a randomized string variable that can be from "A" to "Z" or "1" to "9". I use this string to update a textmesh with its value. I would like to have a KeyDown event that uses this same string as value.

What I figured is that I need to convert the string into a keycode, and then use that on the Get Key Down action. Is this possible? Am I going about this the wrong way?

Thanks!
« Last Edit: May 27, 2013, 01:15:32 AM by jeanfabre »

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Convert String to KeyCode?
« Reply #1 on: May 03, 2013, 12:09:15 PM »
This is what I'm trying to do, I can't get it to interpret the bit after "Input.GetKeyDown(KeyCode." as the value of the variable.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Input)]
[Tooltip("Sends an Event when a Key is pressed.")]
public class GetKeyDown_STRING : FsmStateAction
{
[RequiredField]
public FsmString Key;
public FsmEvent sendEvent;
[UIHint(UIHint.Variable)]
public FsmBool storeResult;

public override void Reset()
{
sendEvent = null;
Key = null;
storeResult = null;
}

public override void OnUpdate()
{
bool keyDown = Input.GetKeyDown(KeyCode.(Key));

if (keyDown)
Fsm.Event(sendEvent);

storeResult.Value = keyDown;
}
}
}

terri

  • Sr. Member
  • ****
  • Posts: 386
    • terrivellmann.tumblr.com
Re: Convert String to KeyCode?
« Reply #2 on: May 03, 2013, 02:20:13 PM »
Posting what my solution was, in case someone else is ever in the same spot and finds my question:

My problem was trying to use KeyCodes. I ended up adding every single letter in the Input manager, and then using a OnButtonPress instead of an OnKeyPress.

The final action was pretty much the one I posted earlier but with this small change:
Code: [Select]
bool keyDown = Input.GetButtonDown(Key.Value);

tam

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Convert String to KeyCode?
« Reply #3 on: May 24, 2013, 06:17:40 PM »
I'm about to start making a button mashing game when the action button changes. I think I'm having the same problem as you due to the Get Key Down actions can't take in variables.
Thanks for the tip about GetButtonDown.

My plan at the moment is to pre-define a selection of keys and then have one of these randomly chosen every X presses. When a key is chosen, some sort of GUI element will change to display the key, while the GetButtonDown listens for that key being pressed. If the key is right, the action will happen, if the key is wrong some sort of negative feedback will happen.
I'll post in here if I figure that out :)

tam

  • Playmaker Newbie
  • *
  • Posts: 11
Re: Convert String to KeyCode?
« Reply #4 on: May 24, 2013, 07:19:38 PM »
OK, that was pretty easy actually.

For testing the functionality I made a plane with a 3D Text attached. The correct key to press is displayed in the 3D Text. If you press the correct key, the plane is translated -1 in Y.

First I set up my inputs in the input manager (Edit > Project Settings > Input) I just deleted everything and then set the size to 3 for my 3 keys required. I named these the letter and then set the input as the letter.

Then in my FSM I've got a Select Random String action with 3 strings set to each of my letters. This is then saved to a variable called ButtonKey_string.

Then I've got a Set Property action to set the text component of the 3d text to the value of ButtonKey_string. (Read this for how to use Set Property with 3d text - http://hutonggames.com/playmakerforum/index.php?topic=2050)

Then I've got a Get Button Down looking for the button name ButtonKey_string which sends a transition to a new event that does my action. In this case, moves my plane -1 in the Y. And that's the basics covered.