- Home /
Input a string and the computer answers with a command?
Hi, everyone. Sorry about the title, I wasn't sure what to call this question. I am looking for a way to, if the user inputs "Audio.PlaySound(3)" to a text area, then the program will play sound three in array. I want the computer to intelegantly read text, in short. How would I do this?
Answer by yoyo · Apr 12, 2011 at 09:41 PM
Get your user to type in some javascript code, then use eval() to run it.
Put this javascript in your project, then try messing with the command string ...
var command = "Debug.Log(\"Hello World\")";
function Start() { eval(command); }
Yes, much simpler than my previous C# answer, wish I knew javascript better!!
Depending on what you're trying to acheive here and who your users are there is a potential security issue with this as it will execute ANY code the user inputs.
Absolutely. With C# you could isolate the exposed commands to a single assembly, is there any similar feature in javascript?
Answer by Macdude2 · Apr 12, 2011 at 02:11 AM
If you wanted it to be really, really simple, couldn't you just check to see if the user has entered a certain string and then run a function based on that? Kind of like checking for a password? I doubt you would be able to do much more than that, unfortunately, with unity.
Wow... Great idea, I'm totally burned out haha. But this is exactly what you'd have to do... Just compare Strings.
Answer by Justin Warner · Apr 12, 2011 at 01:46 AM
Intelligently read text...
Um... I'm sorry, but computers never intelligently read text...
Maybe you should look up some things:
IBM's Watson.
Text-based games (Cons of).
And that should help you...
Goto start>run> cmd.exe > now try to use normal speech to make the computer do things =).
Just making a point, sorry...
But I don't know how you'd get input from a user and use it as a command, I'd actually like to know, so +1 =).
Well, I think it's not a very positive/accurate vision. We're writing text on a daily bases that is processed some way or the other and do things. Scripting!?! That's a user Input transformed into a command. I have made system where if you type simple words, it does detect it and do things, very much like mouse gestures, or else. Totally possible for a particular and precise use, but impossible if you want a general all purpose system like the one you mentioned, I give you that.
Answer by yoyo · Apr 12, 2011 at 05:21 AM
If you don't mind making your user type in actual code, you could use C# reflection to invoke methods on the fly, something like this:
using System; using System.Reflection;
public class MyMethodInvoker { public static void Invoke(string command) { int iPeriod = command.IndexOf("."); int iParen = command.IndexOf("("); string typeName = command.Substring(0, iPeriod); string methodName = command.Substring(iPeriod + 1, iParen - iPeriod - 1); string param = command.Substring(iParen + 1, command.Length - iParen - 2);
Invoke(typeName, methodName, param);
}
public static void Invoke(string typeName, string methodName, params object[] methodParameters)
{
Type audioType = Assembly.GetExecutingAssembly().GetType(typeName);
MethodInfo methodInfo = audioType.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
methodInfo.Invoke(null, methodParameters);
}
}
For your example, you could then call MyMethodInvoker.Invoke("Audio.PlaySound(3)") and it would parse the string, then look up the Audio type and call the PlaySound method on it. My simple parsing logic above (which could definitely use some error-checking!!) will only parse out a single string parameter and hand it to PlaySound, which could deal with the conversion to integer, or you could make the parser better and handle multiple parameters of different types.
I've tested the code above against this trivial implementation of the Audio class ...
public class Audio
{
public static void PlaySound(string param)
{
Debug.LogError("Here we are in Audio.PlaySound(" + param + ")");
}
}
For more information on this technique, just google "C# reflection".
Obligatory disclaimers ...
- error checking code needed
- this exposes all your global classes -- you would want to put your user-callable methods in a separate pre-compiled assembly
- won't work with javascript
- I believe System.Reflection works in a web build, I don't think it works on iPhone
- probably overkill for what you want, but this sort of thing can come in handy at times
Or dispense with C# reflection and find or implement your own scripting language. lua would be a good candidate.
Is there a way to do this in js? if not, thats ok, but it would be cool to keep everything in one script.
I was going to say "I'm not a Java guy, but I don't think so" ... only if I were a Java guy I would have realized that you can simply call eval("Audio.PlaySound(3)") and it just works! eval() is a built-in method that runs a snippet of Java code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122156im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Changing 3d text through script 1 Answer
iOS performance: GUItext? want to use text string, but how to instantiate and edit it runtime? 0 Answers
Playing multiple sounds with multiple subtitles 1 Answer
Draw GUI Text if clicked out of webplayer 1 Answer
How do i take a string from a gui and show it in the gameworld c# 1 Answer