cy221
cy221

Reputation: 1147

How to retrieve another variable within table in Lua?

how can I use title in shortdesc?

This fails:

function descriptor()
    return {
        title = "This";
        shortdesc = title .. " is my text."
    }
end

Upvotes: 3

Views: 58

Answers (2)

Doug Currie
Doug Currie

Reputation: 41220

Paul is correct. You can also construct the table in pieces:

function descriptor()
    local t = {}
    t.title = "This";
    t.shortdesc = t.title .. " is my text."
    return t
end

Upvotes: 4

Paul Kulchenko
Paul Kulchenko

Reputation: 26794

You can't; you can use a local variable though:

function descriptor()
    local title = "This"
    return {
        title = title;
        shortdesc = title .. " is my text."
    }
end

Upvotes: 4

Related Questions