variable has to be before function?

Okay so i just come across this in my code, Its probably some error but when ever i try to do something like



local test = 1
function spoopy()
return test + 1
end


this would work, And great but if I do something like this


local test = 1

function testOne()
	"SOME CODE"
end

function testOneONE()
	"SOME CODE"
end

function testOneONEONE()
	"SOME CODE"
end

function testOneONEONEONE()
	"SOME CODE"
end

function testOneONEONEONEONE()
	"SOME CODE"
end

function Magic()
	return test + 1
end

The function magic says test is a nil value, or that it does not exist, Only if I create the variable directly above the function does it see it, Why is this?

Here: https://dl.dropboxusercontent.com/u/26074909/tutoring/includes/file_only_works_on_autorefresh_possible_reasons.lua.html

Now, for your error it doesn’t work because “Some Code” is an error, therefore the function Magic never gets called. Second, everything in Lua is stored in a function, if you define function test( ) … end and test = 1; after that, they’re both in the same global table, the last test will be given priority so if the function is defined last then test is a function, if the variable is defined last then test is the variable.

But the function does get run, And the error is that the variable does not exist, And it should, Other functions can use it fine, There is no spelling or typos, I have been checking for the last day now. And I apreciate what you said, But i did not get any of it >.>

There must be an error somewhere else in the code that makes the file not run or something.

As you can see there’s not an error in the code you provided.

-edit- someone edited their post O_o… There is an error in what the OP posted because each function had “XXXX” and not print( “XXXX” )

There is an error in the code you provided. Run it here: http://www.lua.org/demo.html

Next, try what I said using type( ) to see if it is a function, or variable…


function test( )
	print( "test" );
end

print( type( test ) ); -- Variable containing data-type "function"...

test = 1;

print( type( test ) ); -- Variable containing data-type "number"...

test = function( )
	print( "test" );
end

print( type( test ) ); -- Variable containing data-type "function"...

Then, compare the reference / memory address like so:


function test( )
	print( "test" );
end

print( _G.test, _G[ "test" ], test );

ALL 3 will be identical because everything in Lua is stored in tables… You can create variable variables and variable function-names by using the naming conventions to your advantage…

You can’t do function _G[ “blah” ] = function( ) end BUT you can do _G[ “blah” ] = function( ) end which is the same as doing _G.blah = function( ) end just like you can do this in a loop: _G[ “variable” … i ] = i; and print out variable1, 2, 3 or however many were defined ( variable variables )…

If you set a variable somewhere, you may be changing something else even if you’re not intending it to happen. Just look at everything as though they’re simply indexes / keys to a table, make sure you don’t overwrite one that pre-exists…

Also, if you create a local variable, the scope of that variable is anything that is defined after it and it may NOT break out of the scope. If you define it after a function then the function will not know of it because it is out of the scope, and it will modify or create a global variable if you try using it in a function, so, yes, locals need to be defined prior to being used. Globals also need to be defined, but the function can be defined to modify the global prior to the variable being created but the variable must be created prior to the function being called / executed.

Hopefully this gives you the answer you’re looking for.