Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8170337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:06:42+00:00 2026-06-06T21:06:42+00:00

How can I assign a variable by reference in Lua to another one? For

  • 0

How can I assign a variable by reference in Lua to another one?

For example: want to do equivalent of “a = b” where a will then be a pointer to b

Background:
have a case where I have effectively something like this:

local a,b,c,d,e,f,g   -- lots of variables

if answer == 1 then
  -- do stuff with a
elsif answer == 1 then
  -- do stuff with b
.
.
.

PS. For example in the below it appears apparent the b=a is by value. NOTE: I’m using Corona SDK.

a = 1
b = a
a = 2
print ("a/b:", a, b)

-- OUTPUT: a/b: 2   1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:06:42+00:00Added an answer on June 6, 2026 at 9:06 pm

    EDIT: regarding your clarifed post and example, there is no such thing as a the type of reference you want in Lua. You want a variable to refer to another variable. In Lua, variables are simply names for values. That’s it.

    The following works because b = a leaves both a and b referring to the same table value:

    a = { value = "Testing 1,2,3" }
    b = a
    
    -- b and a now refer to the same table
    print(a.value) -- Testing 1,2,3
    print(b.value) -- Testing 1,2,3
    
    a = { value = "Duck" }
    
    -- a now refers to a different table; b is unaffected
    print(a.value) -- Duck
    print(b.value) -- Testing 1,2,3
    

    You can think of all variable assignments in Lua as by reference.

    This is technically true of tables, functions, coroutines, and strings. It may as well be true of numbers, booleans, and nil, because these are immutable types, so as far as your program is concerned, there’s no difference.

    For example:

    t = {}
    b = true
    s = "testing 1,2,3"
    f = function() end
    
    t2 = t -- t2 refers to the same table
    t2.foo = "Donut"
    print(t.foo) -- Donut
    
    s2 = s -- s2 refers to the same string as s
    f2 = f -- f2 refers to the same function as f
    b2 = b -- b2 contains a copy of b's value, but since it's immutable there's no practical difference
    -- so on and so forth --
    

    Short version: this only has practical implications for mutable types, which in Lua is userdata and table. In both cases, assignment is copying a reference, not a value (i.e. not a clone or copy of the object, but a pointer assignment).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pointer which is pointing to an integer variable. Then I assign
I just want to assign a variable to a function so it can be
I want to have a a reference that reads as whatever variable of name
can we assign null value to struct type of variable? struct MyStruct { }
I can't assign RED5_HOME=/opt/red5-1.0.0 since variable appears to empty. Probably some characters in variable
How can I assign the maximum value for a long integer to a variable,
How can we re assign the value of a StringBuffer or StringBuilder Variable? StringBuffer
I have to use a Variable(Query Resultset) in ColdFusion , which will get the
It is not possible to assign an integer value to a reference variable directly,
Normally if I assign a variable some value, and then check their ids, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.