|
Riesz Novice
Joined: 16 May 2006 Posts: 35 Location: Plymouth, England
|
Posted: Sat Sep 22, 2007 8:08 am
[v1.34 - Bug?] #Var/#NewVar inside #If gets created in wrong class |
I have been trying to create a variable inside of a conditional statement.
alias:
#if !(%1Listener)
{
#newvar %1Listener
#var %1TestVariable {""} {} {Other|Listener}
}
#additem %1Listener %2
I want to add an item to (for instance) potionListener, but don't want to overwrite existing values so first I wish to check whether it exists beforehand. My error is that when it hits the #NewVar/#Var it creates the variable in the root class. Removing the conditional, variables are correctly placed into the required class.
Thanks for any info that you can give! |
|
_________________ Elaria, Lusternia. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sat Sep 22, 2007 2:51 pm |
Your #if is structured incorrectly. Currently, it's doing "if not %1listener" - %1listener is a literal string, and since it's always going to be a literal string, which evaluates to true, then your if will never execute. However, since #additem creates a variable if one doesn't exist, and you haven't specified the class in the #additem command, the variable is created in the root. You probably don't actually need to fix this problem, since #additem will automatically create a new variable if there isn't one, but if you do want to do other things as well (here you're creating a TestVariable named after them), then you'll want to fix this code.
I'm assuming you're wanting to test if the variable exists before you do certain things - the proper structure for your expression is then (!(@{%1Listener})). The @{} structure accesses a variable's value - it tests if it exists in this case. If that's not what you're after, please post more information about what you expect the script to do.
Also, you need to be very careful when doing implicit concatenation in this way. It thankfully hasn't caused any problems here, but it may do in the future. Instead of putting %1Listener, use %concat(%1,"Listener"). It makes your code easier to understand, too.
Your bracket structure is also a bit weird - it's not causing problems, but it may in other cases. Usually, multiline #ifs are formatted
#if (expression) {
command
command
} {
command
command
} |
Just something else to watch out for that might cause you problems in the future.
Finally, you don't need braces {} and quotes "" when you define an empty variable as you've got on your #var %1TestVariable line. They can be used interchangeably, but the former evaluates its contents and the latter saves a literal value. You don't need both. |
|
|
|
Riesz Novice
Joined: 16 May 2006 Posts: 35 Location: Plymouth, England
|
Posted: Sat Sep 22, 2007 3:20 pm |
That's rather stupid of me. The missing @{} is merely a typing error. I use that format a fair bit already. I agree with you on %concat though, that hadn't occured to me and I prefer it.
The multiline if doesn't cause me problems in other areas of the code. It's a syntax that I prefer and should really be just a formatting choice. I'll keep an eye on it in case it causes errors in future.
I know I don't need the "" as well too, I was just playing around with various syntaxes to get it to work as expected. So, rephrased then...
#if !(@{%1Listener})
{
#var %1TestListener {} {} {Other|Listener}
#newvar %1Listener
}
#additem %1Listener %2
Neither of these options create the variable in the desired class. Outside the #if, they do. |
|
_________________ Elaria, Lusternia. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sat Sep 22, 2007 3:32 pm |
Not much I can say on this one except that it's working for me. I ran this:
#alias test {#if !(@{%1Listener})
{
#var %1TestListener {} {} {Other|Listener}
#newvar %1Listener
}
#additem %1Listener %2}
test something omglol |
in the untitled session and it created @somethingTestListener in the Other\Listener class, and somethingListener in the root like it should. Does that fail for you?
EDIT: I really should've mentioned this at the beginning! I'm using 2.03 - it's very possible that there was a bug here, but there's not any more. Install 2.03 in its own directory and see if it's working for you in the new version. |
|
|
|
Riesz Novice
Joined: 16 May 2006 Posts: 35 Location: Plymouth, England
|
Posted: Sat Sep 22, 2007 6:07 pm |
Utilising #var this test places the listener variable in the correct directory. I'm not sure what problem I had with it before strangely, but the 'problem' with #newvar persists.
You say that #newvar creates the object in the root 'like it should'. The documentation for #newvar says that it should create a variable in the current directory. In this case, it is not be the same directory as the #newvar which goes against the documentation (unless there's a bit that I don't see). Perhaps it's creating it in the dir where the alias (containing #newVar) is being run from?
Either way I can proceed with my script, but confirmation on #newvar functionality would be nice. |
|
_________________ Elaria, Lusternia. |
|
|
|
Fang Xianfu GURU
Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sat Sep 22, 2007 7:39 pm |
#newvar creates a new variable in the current class. The current class is set using the #class command - just using another command with the class set doesn't change the current class. What you're looking for is this:
#class Listener
#newvar %1Listener ""
#class 0 |
in my testing, I found that you must always give a value with #newvar, same as with #var, even if that value's blank, for it to do anything at all.
The help file has an example of how to use #newvar. |
|
|
|
Riesz Novice
Joined: 16 May 2006 Posts: 35 Location: Plymouth, England
|
Posted: Sat Sep 22, 2007 8:05 pm |
That'd be it then, I haven't seen that implementation until now. It's good to know!
|
|
_________________ Elaria, Lusternia. |
|
|
|
|
|