- Home /
Why can't i set my integer to -1?
Using Javascript, i've made a for loop with the condition
i = i + faktor
i'm trying to get faktor a negative value in order for the loop to run backwards but whatever i do e he won't let me set it to a value below zero
Code:
var knopfbreite = 75;
var knopfhoehe = 35;
class Dashboard
{
var KMH : GameObject;
var RPM : GameObject;
var daten : TextAsset;
var richtung : int = 4;
var zeilen : String[];
var werte : String[];
var speedwerte : String[];
var i : int = 4;
var merkwert: int = i;
var faktor : int = 1;
var kmh_rotation = 0;
var rpm_rotation = 0;
function Berechne(faktor : int)
{
zeilen = daten.text.Split("\n"[0]); // eine dateizeile pro array-feld
for(i=merkwert; i< zeilen.length-1; i = i+faktor)
{
if(i < 4)
{
break;
}
werte = zeilen[i].Split(";"[0]); //ein wert pro array-feld
kmh_rotation = int.Parse(werte[4]); // grobe zuweisung einfacher int-werte
rpm_rotation = int.Parse(werte[8]); // grobe zuweisung einfacher int-werte
kmh_rotation = Mathf.Clamp(kmh_rotation / 300.0 * 180.0, 0.0, 180.0); // umrechnung Absoluter wert --> rotationswert
rpm_rotation = Mathf.Clamp(rpm_rotation / 8000.0 * 240.0 , 0.0, 240.0); // umrechnung Absoluter wert --> rotationswert
merkwert = i;
print("faktor : " + faktor); // not being executed, but play / fast forward / first value / last value / stop works
if(faktor > 0)
{
yield WaitForSeconds(1.0/faktor);
}else{
yield WaitForSeconds(1.0);
}
}
}
}
var Objekt : Dashboard;
function Start()
{
}
function Update()
{
switch (Objekt.richtung)
{
case 0 : print("idle");
break;
case 1 : print("Erster Wert!");
Objekt.i = 4;
Objekt.merkwert = 4;
Objekt.werte = Objekt.zeilen[4].Split(";"[0]);
Objekt.kmh_rotation = 0;
Objekt.rpm_rotation = 0;
StopAllCoroutines();
break;
case 2 : print("Rewind!");
StopAllCoroutines();
Objekt.faktor = -2;
StartCoroutine(Objekt.Berechne(Objekt.faktor));
print(Objekt.faktor);
case 3 : print("Play!");
Objekt.faktor = 1;
StopAllCoroutines();
StartCoroutine(Objekt.Berechne(Objekt.faktor));
Objekt.richtung = 0;
break;
case 4 : print("Stop!");
Objekt.richtung = 0;
StopAllCoroutines();
break;
case 5 : print("Forward!");
Objekt.richtung = 0;
Objekt.faktor = 5;
StopAllCoroutines();
StartCoroutine(Objekt.Berechne(Objekt.faktor));
break;
case 6 : print("Letzter Wert!");
Objekt.i = Objekt.zeilen.length-2;
Objekt.merkwert = Objekt.zeilen.length-2;
Objekt.werte = Objekt.zeilen[Objekt.zeilen.length-2].Split(";"[0]);
Objekt.kmh_rotation = 0;
Objekt.rpm_rotation = 0;
StopAllCoroutines();
break;
case 7 : Application.Quit();
}
if(Objekt.richtung == 1)
{
StopAllCoroutines();
Objekt.KMH.transform.rotation = Quaternion(0,0,0,0);
Objekt.RPM.transform.rotation = Quaternion(0,0,0,0);
StopAllCoroutines();
}
Objekt.KMH.transform.rotation = Quaternion.Lerp(Objekt.KMH.transform.rotation, Quaternion.Euler(0,0,Objekt.kmh_rotation), Time.deltaTime);
Objekt.RPM.transform.rotation = Quaternion.Lerp(Objekt.RPM.transform.rotation, Quaternion.Euler(0,0,Objekt.rpm_rotation), Time.deltaTime);
}
function OnGUI()
{
if(GUI.Button(Rect(10,10,knopfbreite,knopfhoehe),"Start"))
{
Objekt.richtung = 1;
}
if(GUI.Button(Rect(85,10,knopfbreite,knopfhoehe),"Rewind"))
{
Objekt.richtung = 2;
}
if(GUI.Button(Rect(160,10,knopfbreite,knopfhoehe),"Play"))
{
Objekt.richtung = 3;
}
if(GUI.Button(Rect(235,10,knopfbreite,knopfhoehe),"Stop"))
{
Objekt.richtung = 4;
}
if(GUI.Button(Rect(310,10,knopfbreite,knopfhoehe),"Forward"))
{
Objekt.richtung = 5;
}
if(GUI.Button(Rect(385,10,knopfbreite,knopfhoehe),"End"))
{
Objekt.richtung = 6;
}
if(GUI.Button(Rect(460,10,knopfbreite,knopfhoehe),"Quit"))
{
Objekt.richtung = 7;
}
}
any help is appreciated
thanks in advance
bio
Its unclear what you are trying to do. Pleas provide the code for the entire loop, ins$$anonymous$$d of just the single line. And it will be a lot easier to help you.
Answer by biohazard · Jul 22, 2011 at 11:11 AM
I solved the issue.
Since i am adjusting the factor of speed in a TextField, i'm setting factor using int.Parse on the input of the textfield.
For the rewind function, i simply invert the result, making both forward and rewind using the same input field and making the behaviour depend on which button is pressed to interpret the input.
Here's the code :
var knopfbreite = 75;
var knopfhoehe = 35;
class Dashboard
{
var KMH : GameObject;
var RPM : GameObject;
var daten : TextAsset;
var richtung : int = 4;
var zeilen : String[];
var werte : String[];
var speedwerte : String[];
var i : int = 4;
var merkwert: int = i;
var eingabestring : String;
var faktor : int = 0;
var kmh_rotation = 0;
var rpm_rotation = 0;
function Berechne(faktor : int)
{
zeilen = daten.text.Split("\n"[0]); // eine dateizeile pro array-feld
for(i=merkwert; i< zeilen.length-1; i = i+faktor)
{
Debug.Log(i);
if(i < 4)
{
break;
}
werte = zeilen[i].Split(";"[0]); //ein wert pro array-feld
kmh_rotation = int.Parse(werte[4]); // grobe zuweisung einfacher int-werte
rpm_rotation = int.Parse(werte[8]); // grobe zuweisung einfacher int-werte
kmh_rotation = Mathf.Clamp(kmh_rotation / 300.0 * 180.0, 0.0, 180.0); // umrechnung Absoluter wert --> rotationswert
rpm_rotation = Mathf.Clamp(rpm_rotation / 8000.0 * 240.0 , 0.0, 240.0); // umrechnung Absoluter wert --> rotationswert
merkwert = i;
if(faktor > 0)
{
yield WaitForSeconds(1.0/faktor);
}else{
yield WaitForSeconds(1.0);
}
}
}
}
var Objekt : Dashboard;
function Start()
{
}
function Update()
{
switch (Objekt.richtung)
{
case 0 : print("idle");
break;
case 1 : print("Erster Wert!");
Objekt.i = 4;
Objekt.merkwert = 4;
Objekt.werte = Objekt.zeilen[4].Split(";"[0]);
Objekt.kmh_rotation = 0;
Objekt.rpm_rotation = 0;
StopAllCoroutines();
break;
case 2 : print("Rewind!");
Objekt.richtung = 0;
StopAllCoroutines();
StartCoroutine(Objekt.Berechne(Objekt.faktor));
print(Objekt.faktor);
break;
case 3 : print("Play!");
Objekt.faktor = 1;
StopAllCoroutines();
StartCoroutine(Objekt.Berechne(Objekt.faktor));
Objekt.richtung = 0;
break;
case 4 : print("Stop!");
Objekt.richtung = 0;
StopAllCoroutines();
break;
case 5 : print("Forward!");
Objekt.richtung = 0;
StopAllCoroutines();
StartCoroutine(Objekt.Berechne(Objekt.faktor));
break;
case 6 : print("Letzter Wert!");
Objekt.i = Objekt.zeilen.length-2;
Objekt.merkwert = Objekt.zeilen.length-2;
Objekt.werte = Objekt.zeilen[Objekt.zeilen.length-2].Split(";"[0]);
Objekt.kmh_rotation = 0;
Objekt.rpm_rotation = 0;
StopAllCoroutines();
break;
case 7 : Application.Quit();
}
Objekt.KMH.transform.rotation = Quaternion.Lerp(Objekt.KMH.transform.rotation, Quaternion.Euler(0,0,Objekt.kmh_rotation), Time.deltaTime*Mathf.Abs(Objekt.faktor));
Objekt.RPM.transform.rotation = Quaternion.Lerp(Objekt.RPM.transform.rotation, Quaternion.Euler(0,0,Objekt.rpm_rotation), Time.deltaTime*Mathf.Abs(Objekt.faktor));
}
function OnGUI()
{
if(GUI.Button(Rect(120,10,knopfbreite,knopfhoehe),"Start"))
{
Objekt.richtung = 1;
}
if(GUI.Button(Rect(450,50,knopfbreite,knopfhoehe),"Rewind"))
{
Objekt.richtung = 2;
Objekt.faktor = -(int.Parse(Objekt.eingabestring));
}
if(GUI.Button(Rect(230,10,knopfbreite,knopfhoehe),"Play"))
{
Objekt.richtung = 3;
}
if(GUI.Button(Rect(340,10,knopfbreite,knopfhoehe),"Stop"))
{
Objekt.richtung = 4;
}
Objekt.eingabestring = GUI.TextField(Rect(340,50,knopfbreite,knopfhoehe),Objekt.eingabestring);
if(GUI.Button(Rect(230,50,knopfbreite,knopfhoehe),"Forward"))
{
Objekt.richtung = 5;
Objekt.faktor = int.Parse(Objekt.eingabestring);
}
if(GUI.Button(Rect(460,10,knopfbreite,knopfhoehe),"End"))
{
Objekt.richtung = 6;
}
if(GUI.Button(Rect(570,10,knopfbreite,knopfhoehe),"Quit"))
{
Objekt.richtung = 7;
}
}
Maybe someone else needs this.
Thanks to Christian H Pedersen for the push into the right direction.
Answer by CHPedersen · Jul 22, 2011 at 07:18 AM
It's unclear from the snippet you posted why you're having trouble setting faktor
to something negative, but if the purpose is to iterate backwards instead of forwards, you can simply start out at the array's length-1 (or whatever it is you're iterating through) and then subtract from the iterator instead of adding to it. Like this:
int[] someArray = new int[20];
for (int i = someArray.Length-1; i >= 0; i--)
{
// Access elements backwards
}
Does that help?
the for loop is inside a coroutine and i'm trying to set faktor using GUI.Buttons to change the faktor's value.
by making the faktor a global variable and remembering the counter's value in a variable, i can start a new coroutine with old parameters
PLAY, PAUSE, REWIND JUST AS I NEED IT
No. The range of a normal int is -2,147,483,648 to 2,147,483,647. Only if the integer is unsigned (uint) does it prevent negative values.
Your answer
Follow this Question
Related Questions
changing a coroutine's argument at the coroutine's runtime? 1 Answer
What's wrong with this Loop? 2 Answers
[SOLVED]Does this loop actually end? 1 Answer
A node in a childnode? 1 Answer
Issue with yield 0 Answers