Common scripting errors


Unset variables evaluation


...
set $sum 0;
unset $sumword;
if (!$sumword) echo $_window Executed.
else echo $_window Not executed.
...
At the first look this sequence of commands should print 'Executed',
because $sumword is unset and evaluates to $_null in that case.
It is not true : Just because KVirc tries to find the variable $sumword ,
can't find it , so looks if there may be variables INSIDE the word ,
That means that $sumword may be intentionally a variable followed by some letters
like : $sum + word.
(Why not? ...you could just use $number.000 to write for example 2.000).
So KVirc will find $sum that is a correctly set (to 0) variable , and will evaluate !$sumwork to : '0work' , that is TRUE!!!
(all non numeric strings are assumed to be true(except of $_null and $_false)).
The correct version of this one is :
...
set $sum 0;
set $sumword $_null;
if (!$sumword) echo $_window Executed.
else echo $_window Not executed.
...
or this one:
...
set $sum 0;
unset $sumword;
if (!$%ext_isset[$sumword]) echo $_window Executed.
else echo $_window Not executed.
...

Use local variables

Global variables may become a problem in complex scripts.
For example :
Assume to have set a global variable in an alias.
If you use a message box somewhere later,it will block the execution of your alias...
But will not block the execution of other aliases and events .
Another event may cange the value of the variable while you're waiting the user to press ok in the message box.
So when your alias continue the execution the variable will not have the expected value.
It's a good idea to have a small amount of global variables , only the really needed ones,
and keep it 'under control'..(Always know what's happening them).
Aliases and Events have its own local variables : $1,$2...$9.
For example : Event_OnKick passes you only $1,$2 and $3 as parameters , so $4...$9 are free local variables easy (and faster) to use.
You can use $1...$3 too , if you don't care about the values passed as parameters..
Same thing for aliases.
Remember also to UNSET the global variables when not neede anymore.
It will speed up the command parsing and avoid many unexpected (and hard to find) problems.

Index