Friendlyskies AoI Forum

You are not logged in.

Announcement

#1 August 23, 2009, 10:49 pm

ecannon
Serial Contributor
From: Northern California
Registered: May 7, 2007,
Posts: 301
Reputation :   
Website

beanshell script bug

I have discovered an interesting quirk in the beanshell scripting language. Every variable has global scope. This has some positves, functions can refer to objects (such are random number generators) declared outside of themselves. A number of negative include the fact that it is really easy to mess yourself up by i.e. using variable in a function that is a loop counter for a loop calling the function. A really easy way to confuse yourself.

A simple workaround is to pre or post append a unique function identifier to each variable used in a function.

I thought I might pass on this interesting piece of information to save people some time debugging their script in AOI.


check out the new renderfarm software at http://gc-films.com/renderfarm/! Version 0.3 now out.
Watch the movie rendered in AoI http://unicornschool.org/imagination/imagination360.avi!

Offline

 

#2 August 24, 2009, 12:46 am

TroY
Penguin?
Registered: October 29, 2007,
Posts: 181
Reputation :   

Re: beanshell script bug

Hum, interesting, this was new to me. But there's another way around it, which (I think) is a little better: Declare all your variables. smile

This prints out "10, 10":

Code:

i = 5;
void work()
{
    i = 10;
    print(i);
}
work();
print(i);

This one prints "10, 5" as expected:

Code:

int i = 5;
void work()
{
    int i = 10;
    print(i);
}
work();
print(i);

Actually, it seems that this is "correct" behaviour as the manual reveals (see also). So in the first example, the outer "i" is simply a "global" variable by definition. You can surely argue about this. It makes sense once you know it -- but it's not really intuitive if you ask me. roll

If you want to continue working with "untyped" variables, you can use "var" instead of a real type like "int". This will work as expected without having to assign a proper type:

Code:

var i = 5;
void work()
{
    var i = 10;
    print(i);
}
work();
print(i);

(I think properly typed variables save you from a lot of trouble, however -- but that's a matter of taste.)

Offline

 

#3 August 24, 2009, 12:05 pm

ecannon
Serial Contributor
From: Northern California
Registered: May 7, 2007,
Posts: 301
Reputation :   
Website

Re: beanshell script bug

well, that makes sense. I started scripting with the information that beanshells is Java but you don't have to declare variables. I guess I just assumed that it was like Python, where variables have scope unless declared otherwise. Thanks for the information TroY, I am sure others will find it useful as well.


check out the new renderfarm software at http://gc-films.com/renderfarm/! Version 0.3 now out.
Watch the movie rendered in AoI http://unicornschool.org/imagination/imagination360.avi!

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson