0 votes
by (590 points)
edited by
for example

(if: _cat is undefined)
(if _cat =/=)
(if _cat is "")
(if: _cat isn't)
(if-not: _cat)

i don't know if those would actually work but maybe you get the idea

is there something like this to check whether a variable exists? when i try, of course it gives the error of the variable doesn't exist

1 Answer

+1 vote
by (63.1k points)
edited by

You should use a startup-tagged passage to initialize every variable to a value before using it so you always know what values to test for in these situations. While Harlowe variables default to 0 if they aren't defined,  this functionality is undocumented and could change without warning. Always initialize your variables; it's the safest and most foolproof strategy. 

Which values to use for initialization is up to you, use whatever makes the most sense. Generally, if the value isn't meant to be set yet, you will probably want to use 0 for numbers, an empty string for strings (""), empty (a:), (ds:), or (dm:) macros for complex data, and false for booleans. 

by (590 points)
thanks for the knowledge, i sort of understand and follow this to a degree mostly because of how simple my coding is

do you know if temp variables also follow the undefined default of 0? i dont think it does, was giving me trouble but i might be mistaken
by (159k points)

do you know if temp variables also follow the undefined default of 0?

This is very easy to test for yourself, simple do the following: 

(set: _var to it + 1)
var: _var

... and you will receive the following warning "There isn't a temp variable named _var in this place." for both of the references to the temporary variable.

by (590 points)

right, i would get this error and that was my problem and needed to know if there was a way around it

 

for example i have a section of code i'm trying to make rerun, and the beginning utilizes a temp variable that isn't set until after the code section is ran and when it reruns it would use that value

...
(set: _problemvariablehere to it + 1)
(set: _vowel to (random: 1, 5))
(set: _nonvowel to (random: 1, 14))
(set: _name to it + $nonvowel's (_nonvowel) + $vowel's (_nonvowel))
(if: _problemvariablehere is < _length...

i wanted to make the "(set: _problemvariablehere to it + 1)" work only after checking if the _problemvariablehere isn't set/defines/exists to avoid the error

 

i think i can just find a way to define it outside that section of code perhaps though

 

thanks

by (63.1k points)

i think i can just find a way to define it outside that section of code perhaps though

This is likely the best and easiest solution. In the code you provided, you should likely initialize that variable to something beforehand, but I don't know exactly what the context there is. 

If you need more help, feel free to come back and elaborate a bit on what you want to have happen, what is actually happening, and what code you've already tried and I'm sure we can work it out. 

...