What is wrong with this scope?

Hey guys,

I have an error and I dont know why… maybe you can explain what im doing wrong

Its a scope issue, the code is like so :

[lua]

local checkPossibleMatches – forward declaration

local flag=false 

flag=checkPossibleMatches(“create”)

function checkPossibleMatches(theState)

    – do something

end

[/lua]

I get the following error:

gameplay.lua:604: attempt to call upvalue ‘checkPossibleMatches’ (a nil value)

Since I declared the function at the beginning, why is this error showing up?

Cheers

Roy.

When you write flag = … then you call function which you declare underneath. I think you misunderstood the concept of forward declaration. It’s to say where function will be placed - just that. Code is executed line by line so you must assign function under variable and next call it.

Should be

local checkPossibleMatches -- forward declaration local flag=false checkPossibleMatches = function(theState) -- do something end flag=checkPossibleMatches("create")

Ok man thanks, I know I can simply put it above, I just though If I declare it, I can use it anywhere I want…

Roy.

You can call it but if you call, it must be already there

Example

[lua]
local func

local function doSomething()
func()
end

func = function()
print(‘hello’)
end

doSomething()
[/lua]

When you write flag = … then you call function which you declare underneath. I think you misunderstood the concept of forward declaration. It’s to say where function will be placed - just that. Code is executed line by line so you must assign function under variable and next call it.

Should be

local checkPossibleMatches -- forward declaration local flag=false checkPossibleMatches = function(theState) -- do something end flag=checkPossibleMatches("create")

Ok man thanks, I know I can simply put it above, I just though If I declare it, I can use it anywhere I want…

Roy.

You can call it but if you call, it must be already there

Example

[lua]
local func

local function doSomething()
func()
end

func = function()
print(‘hello’)
end

doSomething()
[/lua]