Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
mineman123

Help with creating and calling functions

Question

Hey there,

Trying to make a simple program that calls a function that i have made further down in the script. However, when i try to call the function, it results in an error : attempt to call global 'waitForMessage' (a nill value).    Any help would be greatly appreciated

 

- from a scripting newb

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

easiest way to help you is if we can see your code. Errors are much easier to find in a code than just generally...

But if i have to guess I'd say you have declared your function waitForMessage as local and you are calling it therefore before it is even declared..

Link to post
Share on other sites
  • 0

In Lua, variables declared local are only local after this declaration. For example, in this code block:

local function early_use()
    return later()
end

local function later()
    return 42
end

print(early_use())

The use of later in the early_use function refers to the global later, because it has not yet been declared local, and in Lua all variables are global by default. Because there is no such global, the function will error out.

 

To change this, you need to declare later as local before you use it the first time. You also need to remove the local from the definition of the later function - otherwise it will create a new local variable that is also named later, but is not the same as the first later. It is good practice to put a comment on both the forward declaration and the definition afterwards - it is not very clear what the local later does if you don't know the code, and the function definition later on might look global if you don't remember the early local declaration.

local later -- Forward declaration, function is defined later

local function early_use()
    return later()
end

-- later was declared as local earlier
function later()
    return 42
end

print(early_use())

This code block should properly print 42.

 

Of course all of this should only be done if you actually have to use a variable before it is assinged a value. If you can swap the two function definitions without breaking the code, you should absolutely do that instead.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.