Hello everyone,
I’m currently working on a new project that works through Twitch.tv. The Script I’ll be posting grabs commands with int values. Since the text we’re parsing is always a type of “String”, in order to parse these values correctly into variables, I need to replace the command text with nothing, set up a variable to parse to and output just the integer value that the player has specified in chat.
Example: Player types “!aim 50” in chat .
The command “!aim” would get removed from the string, and “50” of type String would be left. I then attempt to convert that string value into an integer value that I can pass into the player’s instantiated prefab.
//First we check if we're in the attack round.
if (rm.phase == "Attack")
{
GameObject playerGO = GameObject.Find(name);
string aimAngle = e.Command.ChatMessage.Message;
aimAngle = aimAngle.Replace("!aim ", "");
int aimAngleInt;
int.TryParse(aimAngle, out aimAngleInt);
//Do a check to see if the angle is within physics boundaries. If not, break.
if (aimAngleInt >= 0 && aimAngleInt <= 360)
{
//If the angle is acceptable, set the aim angle for the player.
//Get the player's instantiated prefab, and set angle on the attached aim script.
playerGO.GetComponent<Aim>().SetAimToAngle(aimAngleInt);
Debug.Log(aimAngleInt);
}
else
{
//The value specified was either too low or too high. Ignore the command.
}
}
else
{
//Do nothing, because we're in the attack round.
}
break;
case "fire":
_client.SendMessage(e.Command.ChatMessage.Channel, $"Firing!");
if (rm.phase == "Attack")
{
string forceValue = e.Command.ChatMessage.Message;
forceValue = forceValue.Replace("!attack ", "");
int forceValueInt;
int.TryParse(forceValue, out forceValueInt);
With every case, except for fire, TryParse works perfectly - the string value is converted to an integer value and the int variable is assigned the int value. As mentioned, on line 36, TryParse returns a value of “0”, which if I understand correctly, means that TryParse failed to parse the string as an integer.
My questions are:
Why would every case but this one work? The code is exactly the same in terms of context, and nearly literally.
What could have changed to cause TryParse to fail in the first place?
It is absolutely necessary that the string value is converted to an integer value to pass into the player prefab’s attached Fire script.
What are some examples of other ways I could convert/parse the string as an integer knowing the answers to the above questions in mind?