user3602803
user3602803

Reputation: 109

How to call an object inside a function?

How to call an object inside a function? Example: The pencil object is inside a function, and after closing it, I have to use

pencil:removeEventListener ("touch", moveLapis)

How do I do that?

For when I call it normally, an error saying that the pencil is nil

Upvotes: 1

Views: 220

Answers (1)

Basilio German
Basilio German

Reputation: 1819

You can't. Objects inside functions can only be called inside that scope, to be able to call it from outside, you would have to move your pencil object to outside the function, or add a reference outside the function to it.

For example:

local pencil
local function myFunction()
   pencil = newPencil()
end

if pencil then
    pencil:removeEventListener ("touch", moveLapis)
end

Of course, you would have to check if pencil has a value or some kind of verification before calling the function, to avoid an error.

Upvotes: 3

Related Questions