ThemeForest

Variables Question

412 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
AlexH says

Hey, why is it that this works:

number = 10;
button.onRelease = function(){
    trace(number)
    }
and this doesnt:
var number = 10
button.onRelease = function(){
    trace(number)
    }
4 years ago
17 posts
  • Has been a member for 4-5 years
  • Sold between 1 and 100 dollars
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
Zeros says

button.onRelease = function () { };

var number = 10;
trace (number);

Place the variable inside the function ;)

4 years ago
1897 posts
  • Has been a member for 4-5 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Attended a Community Meetup
  • Envato Staff
  • Support Staff
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 50 and 99 items
  • Canada
  • Referred between 1 and 9 users
TPN says

var number = 10;
button.onRelease = function()
{
    trace(number)
}

Works Fine | Add a semicolon @ the end of 10

4 years ago
TPN is an Envato staff member
412 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
AlexH says

strange, i figured it was always the case, i’m sorry, what I meant was:

function definer(){
var number = 10;
trace("the value is really defined");
}
definer();
button.onRelease = function()
{
    trace(number); //says undefined
}

And the question is, how can I get the value of number in the onRelease function?

4 years ago
1552 posts Rockstar Developer
  • Has been a member for 5-6 years
  • Envato Staff
  • Reviewer
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • Australia
  • Referred between 10 and 49 users
RimmonTrieu says

cause number in definer() function is local scope which means number does not exist outside definer();

Putting var before number means you declare a new variable which its scope is inside definer();

You should declare number as global scope, that should works for the case.

4 years ago
RimmonTrieu is an Envato staff member
412 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
AlexH says

Okay, that makes sense, that means that when you delete “var” it becomes a global variable?

Thank you all for your answers :)

I really need to get some sleep now, it’s 0:31 here now, so I’ll check this out tomorrow :x

4 years ago
68 posts
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • Romania
  • Referred between 1 and 9 users
alexandrup says

You have multiple choices here:

1. you can just declare it first in your script
//declare here, then just use it
var myVar:Number;
.....
function foo(){
      myVar = ....
      ... = myVar;
}

function bar(){
     myVar = ....
     .... = myVar
}

foo();
bar();

2. you can declare it as _global. by prefixing a variable with _global. you will have access to that var anywhere in your application, provided you already assigned a value to it. Also, _global variables are ALWAYS typeless, you cannot declare var _global.myVar:Number, it will give you an error.

//note that here i don't do any declaring
function foo(){
     //use it here first time  - you must assign it a value
     _global.myVar = ....
    //note the *_global* prefix
}

function bar(){
   //here just use it in any way...
    .... = _global.myVar;
}

foo();
bar();

The correct way is always declaring any variables first in your script, and as much as possible avoid typeless variables – it’s very good practice. Also, using _global is not recommended, because you always must have control over your application, so you won’t use it before it’s declared (or assigned a value).

4 years ago
412 posts
  • Has been a member for 5-6 years
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
AlexH says

Ok, thanks. Really helped me on this :)

I feel like such a |\|o0b

4 years ago
by
by
by
by
by