Author Topic: Loop returns True when the varible/token is updated within it  (Read 2000 times)

Kuk1a

  • Newbie
  • *
  • Posts: 2
Hey, I'm trying to create a loop to iterate through indexed variables to count them.
The idea is that when the variable is set it will add +1 to the indexer and keep iterating, otherwise -1 and break.
For some reason I'm unable to do that although the variable does update and eventually returns "Not set", but the loop however just keeps on going.

Example:
Code: [Select]
Set integer [~i] value to 0
Start Indefinite Loop
    Begin Text Compare : [{TXT:Variable.[{INT:~i}]}] Has Been Set
        Set integer [~i] to [~i] plus 1
    Else
        Set integer [~i] to [~i] minus 1
        Loop Break
    End Condition
End Loop

The variable is "Variable.[1/2/..]"

I've tried to do this multiple ways (loop with condition, variable instead of token, copying the value to a variable, etc.) unfortunately each time with the same result,
the variable updates but the loop keeps going (returns "true" each iteration).
I really don't understand where's the issue and would love some help on this one.

Thanks!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Loop returns True when the varible/token is updated within it
« Reply #1 on: April 04, 2022, 08:35:00 AM »
If you're using tokens in the "Variable Name / Token" field, the input is considered to be literal text, not a variable name.

You're currently checking whether literal text has been set, which I suppose it has.


To retrieve the value of a variable within that field, you'll want to wrap the input in another "{TXT:}" token, which will return the literal value of the variable whose name is represented by the other tokens.

Note, this again returns a literal value, therefore the "Has Been Set" operator does not apply.
Instead, you'd want to compare against the literal text "Not set" (this does mean there is no method for differentiating between a variable set to the literal text "Not set", or a variable that actually has not been set, when using tokens to derive a variable name)

Kuk1a

  • Newbie
  • *
  • Posts: 2
Re: Loop returns True when the varible/token is updated within it
« Reply #2 on: April 04, 2022, 09:05:14 AM »
Thanks mate! I see what you mean
I went with this:

Code: [Select]
Set integer [~i] value to 0
Start Loop While : [{TXT:Variable.[{INT:~i}]}] Does Not Equal 'Not set'
    Set integer [~i] to [~i] plus 1
End Loop
Set integer [~i] to [~i] minus 1

works perfectly!