- Home /
start counting number of line in input field after a specific line and read it
Hello, I'm new in unity and i'm making a game to test programming skill something like codecombat and i have a question please i want to start counting the lines in a input field after a specific line for example i have a input field that have this lines
Hero.left(2)
Hero.right(2)
REPEAT
{
Hero.Down(3)
}
i want it if he find this word "REPEAT" to start counting the number of line from "{" to "}" (counting the number of lines inside the " { } " and read it ).
this is my all the code in the script that i'm using
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager1_4 : MonoBehaviour
{
public InputField mainInputField;
//public float speed;
public GameObject Player;
public Button Click_me;
public float smoothing = 1f;
public Transform TargetRight;
public Transform TargetRight1;
public Transform TargetRight2;
public Transform TargetDown;
public Transform TargetDown1;
public Transform TargetDown2;
int count;
public float targetTime = 2f;
private readonly Queue<Transform> _commands = new Queue<Transform>();
// Start is called before the first frame update
void Update()
{
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f)
{
targetTime = 0f;
timerEnded();
}
}
public void SubmitName()
{
int countligne = 0;
var lines = mainInputField.text.Split('\n');
// mainInputField.text = "";
foreach (var line in lines)
{
countligne++;
switch (line.ToUpper())
{
case "REPEAT(3)":
for (int i = 0; i < 3; i++)
{
var lines_reapeat = mainInputField.text.Split('\n');
Debug.Log(lines_reapeat);
foreach (var line_rep in lines_reapeat)
{
switch (line_rep.ToUpper())
{
case "DOWN(1)":
// adds a new item to the end of the Queue
_commands.Enqueue(TargetDown);
break;
case "RIGHT(1)":
_commands.Enqueue(TargetRight);
break;
case "}":
break;
}
}
}
//i want this line to start calculation from "{" to "}" and run the right donw command inside the "{" "}" but it's always calculating the same inputfieald
break;
case "DOWN(1)":
// adds a new item to the end of the Queue
_commands.Enqueue(TargetDown);
break;
case "RIGHT(1)":
_commands.Enqueue(TargetRight);
break;
}
}
StartCoroutine(WorkCommands());
mainInputField.text = "";
}
private IEnumerator WorkCommands()
{
// block input
Click_me.interactable = false;
// run this routine until all commands are handled
while (_commands.Count > 0)
{
// returns the first element and at the same time removes it from the queue
var target = _commands.Dequeue();
// you can simply yield another IEnumerator
// this makes it execute and at the same time waits until it finishes
yield return MovementCoroutine(target);
}
// when done allow input again
Click_me.interactable = true;
CountNumbre();
}
// Update is called once per frame
private IEnumerator MovementCoroutine(Transform target)
{
var startPos = Player.transform.position;
var targetPos = target.position;
var timePassed = 0f;
do
{
var lerpFactor = Mathf.SmoothStep(0, 1, timePassed / smoothing);
Player.transform.position = Vector3.Lerp(startPos, targetPos, lerpFactor);
timePassed += Time.deltaTime;
yield return null;
}
while (timePassed < smoothing);
Player.transform.position = targetPos;
// just to be sure there is no over or undershooting
// in the end set the correct target position
}
void CountNumbre()
{
count++;
var startPos = Player.transform.position;
var timePassed = 0f;
Debug.Log("Nombre de fois" + count);
if (count >= 3)
{
Click_me.interactable = false;
Debug.Log("You finish all your chance");
}
}
void timerEnded()
{
if (Player.transform.position == TargetRight2.position)
{
Debug.Log("Processing Speed = 100");
Application.Quit();
}
else
{
Debug.Log("Processing Speed = 0");
}
}
}
and this is the function i use in splitting lines and read what the player has been write
public void SubmitName()
{
int countligne = 0;
var lines = mainInputField.text.Split('\n');
// mainInputField.text = "";
foreach (var line in lines)
{
countligne++;
switch (line.ToUpper())
{
case "REPEAT(3)":
for (int i = 0; i < 3; i++)
{ //i want this line to start calculation from "{" to "}" and run the right donw command inside the "{" "}" but it's always calculating the same inputfieald
var lines_reapeat = mainInputField.text.Split('\n');
Debug.Log(lines_reapeat);
foreach (var line_rep in lines_reapeat)
{
switch (line_rep.ToUpper())
{
case "DOWN(1)":
// adds a new item to the end of the Queue
_commands.Enqueue(TargetDown);
break;
case "RIGHT(1)":
_commands.Enqueue(TargetRight);
break;
case "}":
break;
}
}
}
break;
case "DOWN(1)":
// adds a new item to the end of the Queue
_commands.Enqueue(TargetDown);
break;
case "RIGHT(1)":
_commands.Enqueue(TargetRight);
break;
}
}
StartCoroutine(WorkCommands());
mainInputField.text = "";
}
please anyone can help me in this i'm stuck and sorry for my English.
Your answer
![](https://koobas.hobune.stream/wayback/20220612203819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Inputfield text to String variable 1 Answer
Public text to multi line 1 Answer
Unity 5 input field unable to store user input 1 Answer
Input Fields not actually taking in user data on android build 0 Answers
Getting pixel font to look decent 1 Answer