So I have an in-game chat. I’m currently making Developer commands, and need help. I want to be able to do /name “name” but I can’t figure out how to make it only use the text AFTER the /name part. Any ideas?
MSDN string. MSDN is a very valuable resource.
Specifically look at string.Split.
I looked at the examples and JS is not supported. Is there any other way?
You’re not using JavaScript, you’re using UnityScript.
UnityScript works fine with all .net functions/classes.
Yes, it is. It’s not entirely clear to me what you’re trying to do from your initial post, but if string.split isn’t what you want, you might also take a look at string.Substring
Yeah sorry, Never been on the MDSN Microsoft website so i’m a little overwhelmed. Basically, I need it to only read the text AFTER /name. So it would be something like /name jgodfrey, and it would set your name as jgodfrey.
Then you would use string.Split (" ") // a space
Then use the first string returned to decide what the action would be (/name)
And the second string would be its parameter (jgodfrey)
That assumes that you input string will only contain a single space. If that the case, this should work. That said, there are any number of ways to do this. Here’s another one…
string st = "/name jgodfrey";
string newSt = st.Remove(0, 6);
So, that’ll remove 6 characters from the original string, starting at char position 0. So, it effectively removes "/name ". Of course, this assumes that you’re always want to remove 6 leading characters. The method you choose is very dependent upon what you know about your data. Do you want to remove a consistent number of chars? Do you want to remove everything to the left of some unique character? Based on your requirements, you should choose an appropriate method.
I’d probably use StartsWith to determine the command, and Substring to get the rest. There’s a huge amount of functionality in Mono/.NET that you can use in Unity, so it’s a good idea to get familiar with MSDN. (Change the framework on the site to .NET framework 3.5, since that’s most similar to what you use in Unity.)
–Eric
The way I have always done it, is to loop through all the elements. The first being the action to take, and all the rest to be the (optional) parameters. The way I showed was a dumbed down version.
I run all the parameter pieces through a ‘converter’ to decide whether they are applicable, and if they are, to turn them into meaningful pieces of data (if need be. Strings are sometimes the desired data type) and to then pass them through a method (which was chosen by the first string)
That way, there could be an unlimited amount of parameters or actions.
I ended up going with jGodFrey’s way. That was simple, yet effective. Thanks everyone for helping!