Editing a table by referencing.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Editing a table by referencing.

Post by kikito »

Robin wrote:The way values are passed in Lua is the same for all types, and it behaves in some ways as by value and in other ways as by reference. It's generally called call by object identity.
Thanks for this, I didn't know.
Zilarrezko wrote:Oh, wait. Could I use the C api to do what I want? LuaJIT and I think Lua have some C api in right?
I think you should stop building on top of what you have done so far. Doing things for learning is fine, but the fact that something helped the learning process doesn't make it a good foundation. It sounds like your code is asking you to stop building features and start trimming / reorganizing things instead.
When I write def I mean function.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Editing a table by referencing.

Post by Plu »

Let me preface this by saying you should listen to Kikito; what you're trying to do isn't really the Lua way and you'll be working against the language more than with it, which is never a good thing.

That said; there are ways. There are always ways. You can wrap an integer or string in a closure:

Code: Select all

makeNewInt = function( initial )
  local value = initial
  return function( arg )
    if arg then
      value = arg
      return arg
    else
      return value
    end
  end
end

a = makeNewInt( 6 )
a() -- 6
a( 7 ) -- 7
a() -- 7
b = a -- copies a reference to the closure
b() -- 7
b( 9 ) -- 9
a() -- now also 9
Due to all the function call overhead, the performance here is pretty poor; don't do this in time-critical code. But it does work, and for some cases might even be useful.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: Editing a table by referencing.

Post by Zilarrezko »

Yeah closures was the other idea I had, but I don't like them. Alright, whatever, back to my occasional problem solving.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests