Comment on page
Is Variable Exist
The Is Variable Exist block is useful when we need to conditionally execute different parts of the graph based on the presence or absence of certain variables.
Is Variable exist blocks have one input parameter: a string representing the variable name we are inquiring about. They have two executive outputs labelled "True" and "False". If a variable by the given name exists, then the "True" connection will be executed. Otherwise, the "False" connection will be executed.
In the graph snippet above, we use an
Is Variable Existblock to check if a variable already exists by the name "exampleVariable". If such a variable does exist, we access its value using a
Get variableblock, and then output it with a
When the above graph is executed, the Is Variable Exist block checks for the existence of the "userBalance" variable. If the variable exists, it returns "true," and the graph follows the True path. If the variable does not exist, it returns "false," and the graph follows the False path.
The example below is more advanced, but it demonstrates a realistic use of the
Is Variable Existblock:
This graph outputs the change in Bitcoin's price over the last minute, once every minute, so long as the graph is left running. It does this with the use of a
Timeron a 60-second cycle. Once a minute, the timer triggers a
Get CoinGecko Coinblock, which accesses the current price of Bitcoin and then subtracts from that the value of a variable called "priceOneMinuteAgo", and then prints the result. Afterwards, it uses a
Set variableblock to overwrite the value of "priceOneMinuteAgo" with the current price of Bitcoin. That way, when the timer next triggers 60 seconds from now, that variable will then contain a 1-minute-old price of Bitcoin.
The purpose of the
Is Variable Existblock here comes from the observation that, the very first time the
Timerblock triggers and this sequence runs, the variable "priceOneMinuteAgo" won't exist yet, as it hasn't been declared yet with a
Set variableblock. This means that it won't be possible to use this variable in a subtraction operation: the calculated result would end up just being empty. Therefore, we use an
Is Variable Existblock to check if "priceOneMinAgo" exists yet. If so, we execute the full sequence like normal. If not, then we skip the subtraction and printing this cycle, and simply go ahead and use the
Set variableblock to declare and assign to the variable "priceOneMinuteAgo".
The effect of this technique in this example is that the very first time this algorithm cycles, the
Is Variable Existblock will output "False", and then every subsequent cycle it will output "True".