1
function example()
    help = "no"
end

meme = example()

print(meme.help)

This code throws a run-time error. I don't know what to do.

I'm trying to teach myself lua and I know this can be done in java, but I can't get it working in lua.

4
  • puu.sh/djClh/1d4312f5cf.png Commented Dec 6, 2014 at 19:24
  • im using the corona sdk Commented Dec 6, 2014 at 19:25
  • You're not returning any value from example(), see my answer. Commented Dec 6, 2014 at 19:27
  • Key concept: There are two ways to create something new in Lua: evaluating a table constructor {} creates a new table value; evaluation a function definition example = function() end creates a new function value. Commented Dec 7, 2014 at 4:10

2 Answers 2

1

You're not returning in your function.

If the function doesn't return anything example() doesn't have a value, thus you're receiving a nil value error:

enter image description here

Using as the code is being returned, using help.meme will not work. As it is the only variable returned you can simply use this in your use case:

The below code will fix this:

function example()
    help = "no"
end

example()

print(help)
Sign up to request clarification or add additional context in comments.

13 Comments

puu.sh/djCCK/5eb36e5abe.png now it prints nil instead of throwing an error
@JosephFazekas I modified the code, it will now return correctly.
Thanks this helps, but I dont only want the code to print the value. I want to be able to change it as well. @cybermonkey
@JosephFazekas You're probably looking for a table or global value. Simply change meme = example() with meme = "[whatever text you want here]".
@JosephFazekas Please review my code modification, that will work. I'll also be grateful if you could upvote and mark my accept my answer (click the tick by the question).
|
0

It is not very clear what you are trying to achieve, but from your question it seems that you want to emulate the object-oriented features of Java.

Keep in mind that Lua has no "instance variables" since it has no classes. Lua object oriented approach is uncommon and, unlike Java, is sort of prototype-based object orientation.

A very basic approach to creating an object with instance variables and methods is the following:

-- this is the "instance"
myobject = {}
-- this defines a new "instance variable" named `help`
myobject.help = "this is the help text"

-- this defines a getter method for the instance
-- the special name `self` indicates the instance
function myobject:GetHelp()
    return self.help
end

-- this defines a setter method for the instance
function myobject:SetHelp( help_text )
    self.help = help_text
end

print( myobject:GetHelp() )
myobject:SetHelp( "new help text" )
print( myobject:GetHelp() )

You can discover more about object orientation in Lua browsing the links in Lua WIKI's page about object oriented programming.

1 Comment

THANK YOU SO MUCH I didn't even realise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.