2 quick questions about variables
Moderators: Scripting / Modding Moderators, Moderators for English X Forum
-
- Posts: 235
- Joined: Sat, 16. Feb 08, 23:44
2 quick questions about variables
what does @ in value="@var" mean?
what does ? in value="var?" mean?
what does ? in value="var?" mean?
-
- Posts: 75
- Joined: Tue, 26. Nov 13, 10:07
the ? basically checks whether the variable exists.
value="$var?" is true when $var has been set, and false when it hasn't.
value="$var?" is true when $var has been set, and false when it hasn't.
Streaming while writing mods: twitch.tv/fumblesneeze
-
- Posts: 274
- Joined: Sun, 14. May 06, 11:21
? will check if the value exists
Say you have a pilot entity, and want to check if some variable that may be defined is of a certain value. You could do it like this:
@ will suppress any errors due to the expression it prefixes, and will return null. This allows some shortcuts in your code, so in the above you could do:
which will resolve $pilot.$var to null if it doesn't exist, and then compare null with 5, which will return false.
Hope that helps.
Say you have a pilot entity, and want to check if some variable that may be defined is of a certain value. You could do it like this:
Code: Select all
<do_if value="$pilot.$var?">
<do_if value="$pilot.$var == 5">
<!-- do some stuff -->
</do_if>
</do_if>
Code: Select all
<do_if value="@$pilot.$var == 5">
<!-- do some stuff -->
</do_if>
Hope that helps.
-
- Posts: 235
- Joined: Sat, 16. Feb 08, 23:44
-
- Posts: 1498
- Joined: Thu, 10. Mar 05, 21:04
-
- Posts: 235
- Joined: Sat, 16. Feb 08, 23:44
-
- Moderator (Script&Mod)
- Posts: 14571
- Joined: Sun, 20. Nov 05, 22:45
nope, more like:
(sry, i am bad with words, i prefer example cases ^^)
Code: Select all
<do_if value="$var">
<!-- ERROR to debuglog/will not do -->
</do_if>
<do_if value="not $var">
<!-- ERROR to debuglog/will do -->
</do_if>
<do_if value="@$var">
<!-- will not do -->
</do_if>
<do_if value="not @$var">
<!-- will do -->
</do_if>
<do_if value="$var?">
<!-- Will not do -->
</do_if>
<do_if value="not $var?">
<!-- Will do -->
</do_if>
<set_value name="$var" value="null"/>
<do_if value="$var">
<!-- Will not do -->
</do_i>
<do_if value="not $var">
<!-- Will do -->
</do_i>
<do_if value="@$var">
<!-- will not do -->
</do_if>
<do_if value="not @$var">
<!-- will do -->
</do_if>
<do_if value="$var?">
<!-- Will do -->
</do_if>
<do_if value="not $var?">
<!-- Will not do -->
</do_if>
<set_value name="$var" value="[TRUE]"/>
<do_if value="$var">
<!-- Will do -->
</do_i>
<do_if value="not $var">
<!-- Will not do -->
</do_i>
<do_if value="@$var">
<!-- will do -->
</do_if>
<do_if value="not @$var">
<!-- will not do -->
</do_if>
<do_if value="$var?">
<!-- Will do -->
</do_if>
<do_if value="not $var?">
<!-- Will not do -->
</do_if>
if not stated otherwise everything i post is licensed under WTFPL
Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter
I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help
Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter

I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help

-
- Posts: 235
- Joined: Sat, 16. Feb 08, 23:44
yeah I get that, but what I mean is: imagine you have an expression like "7 == 7" it compares if 7 equals 7 and returns true. but now if you use just "7" it's also a true expression, because a 7 is .. well ... a 7. it's a true statement. if you don't compare it to anything, logically it's gotta be true. however "null" is a false expression.
-
- Moderator (Script&Mod)
- Posts: 14571
- Joined: Sun, 20. Nov 05, 22:45
well, 0 is as far as i know false, too 

if not stated otherwise everything i post is licensed under WTFPL
Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter
I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help
Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter

I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help

-
- Posts: 100
- Joined: Tue, 24. Feb 09, 03:47
IMHO, shortcuts make for long delays.Mad_Joker wrote: @ will suppress any errors due to the expression it prefixes, and will return null. This allows some shortcuts in your code, so in the above you could do:
Code: Select all
<do_if value="@$pilot.$var == 5"> <!-- do some stuff --> </do_if>
Code: Select all
<do_if value="$pilot? and $pilot.$var? and $pilot.$var == 5">
<!-- "and" stops evaluating after the first false -->
<!-- "or" stops evaluating after the first true -->
</do_if>
If $pilot is null it won't move on and try to evaluate $pilot.$var and won't throw an error.
If $pilot.$var is null it won't move on and try to evaluate $pilot.$var == 5 and won't throw an error.
Suppressing warnings for use as a null check will make it much harder to debug because the script will just move along happily giving the illusion that you have a valid object when you don't.
-
- Posts: 1498
- Joined: Thu, 10. Mar 05, 21:04
You say we aren't comparing it to anything, but the experssion:
$var
is equivalent to:
$var == true
The same can be done for any expression. The expression is always reduced to a true or false, and the conditional code is executed or not depending on it.
So, does "7" reduce to true? Logic doesn't mean anything here because this is a nonsense question, but the implementation has rules that will provide an answer. I believe that in this case, anything that isn't actually [TRUE] will evaluate to false.
$var
is equivalent to:
$var == true
The same can be done for any expression. The expression is always reduced to a true or false, and the conditional code is executed or not depending on it.
So, does "7" reduce to true? Logic doesn't mean anything here because this is a nonsense question, but the implementation has rules that will provide an answer. I believe that in this case, anything that isn't actually [TRUE] will evaluate to false.
-
- Posts: 235
- Joined: Sat, 16. Feb 08, 23:44
oh my god, i just tried it and 0 is really considered not a number but same as "null". now that also finally explains to me, why there's an issue with waresamount = 0 in the station mining script. first I thought, it might be a precaution against dividing by 0 .
wth really, they already have "null", why block the 0 like this ...
wth really, they already have "null", why block the 0 like this ...
-
- Moderator (Script&Mod)
- Posts: 14571
- Joined: Sun, 20. Nov 05, 22:45
well, it depends on what you expect.. for example if you want to work with Local Vars of another Extension as Optional part of yours it is quite neat since you dont have to check if the var exists and then compare/evaluate it - you can do both in one step 
but i agree that this should be used with caution if you dont want to waste a few hours bug-hunting.

but i agree that this should be used with caution if you dont want to waste a few hours bug-hunting.
if not stated otherwise everything i post is licensed under WTFPL
Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter
I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help
Ich mache keine S&M-Auftragsarbeiten, aber wenn es fragen gibt wie man etwas umsetzen kann helfe ich gerne weiter

I wont do Script&Mod Request work, but if there are questions how to do something i will GLaDly help

-
- Posts: 274
- Joined: Sun, 14. May 06, 11:21
Actually, I think you are wrong. I tried what you wrote when I started modding, and discovered, that in this scripting language boolean expressions are not short-circuited, i.e. in the expression you posted even if $pilot? fails, $pilot.$var? will be evaluated, and will throw an error. That's the reason the code I posted should be used.bbeach wrote:IMHO, shortcuts make for long delays.Mad_Joker wrote:...
In the example aboveCode: Select all
<do_if value="$pilot? and $pilot.$var? and $pilot.$var == 5"> <!-- "and" stops evaluating after the first false --> <!-- "or" stops evaluating after the first true --> </do_if>
If $pilot is null it won't move on and try to evaluate $pilot.$var and won't throw an error.
If $pilot.$var is null it won't move on and try to evaluate $pilot.$var == 5 and won't throw an error.
Suppressing warnings for use as a null check will make it much harder to debug because the script will just move along happily giving the illusion that you have a valid object when you don't.
Btw, using @ and ? in the same expression gave me strange results, but I never really validated them. It was like @$var? would always return true. I think this is because @ gets precedence, so @$var evaluates to null, and null? returns true.