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
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Editing a table by referencing.

Post by Zilarrezko »

So, I hear there's a way to indirectly edit a value in a table, by making a reference to that value. I would like to do this. I want to edit a value in a table by throwing the table's value from a key into a function, having that function create an object with a reference to that value. Then when that reference changes, it changes the referenced value.

That was most likely confusing because letters are very missleading and hard to understand if not used in set structure. Thank goodness we have coding as a medium.

Here's the gist of my set up problem

Code: Select all

colors.colorCode = {255, 255, 255, 255} --Here's something I want to change indirectly

function createobject(param) --let's create a very basic object class
     local obj = {}
     obj.target = param
     return obj
end

function changeparam(data, value) --This will basically change the object's target value, However I want it to also change it's target
     data.target = value
end

anobject = createobject(colors.colorCode[1]) --Giving the object it's target parameter to change
changeparam(anobject, 0) --Will change it's target key's value to 0, yet I want it to change colors.colorCode[1] to 0
Yet I have to go through a whole message system to change the referred value. I heard you can do so by having the param being a table with the value inside it, though that's something I don't want to do. Any way? Thanks in advance guys.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Editing a table by referencing.

Post by Robin »

That's just not how Lua works, so you need to design your code base in such a way that you don't need references.
Help us help you: attach a .love.
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 »

Only tables and functions are passed by reference (I think userdata too). The rest (i.e. numbers and strings) are copied when passed to a function. If you want to change a table's values, and those values are numbers or strings (like your colorCode table), you must retain a reference to that table somewhere - you can't "hold a reference to one of its values" when that value is a string/number.
When I write def I mean function.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Editing a table by referencing.

Post by Robin »

kikito wrote:Only tables and functions are passed by reference (I think userdata too). The rest (i.e. numbers and strings) are copied when passed to a function.
That is not true. 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. Another way to describe it would be "call by value of reference". Basically, the reference is copied and not the value itself.

(Also note that Lua interns strings, so it doesn't really copy them ever.)
Help us help you: attach a .love.
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 »

So basically what I'm hearing is I'm screwed/I'm not necessarily screw, but I have to make weird tables.

I had two ideas to get around it, But I just wanted there to be a simpler approach (like a pointer or something). I do already have what I'm calling "throw and catches" I'm sure I got that from some other programming language (Although I think they used it like asserts for errors). But it just annoyed me to copy the variable, then having to set it in a catch function. Oh well :/ thanks guys.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Editing a table by referencing.

Post by ivan »

So, I hear there's a way to indirectly edit a value in a table, by making a reference to that value
I'm afraid this goes against the gist of Lua and most dynamically typed languages.
When you write "object.target = param[1]" you are copying the number value (param[1]) to (object.target).
I want to edit a value in a table by throwing the table's value from a key into a function, having that function create an object with a reference to that value. Then when that reference changes, it changes the referenced value
In that case, just pass the table and its key to the function (not the value).

Code: Select all

function createobject(data, key)
     local obj = {}
     obj.data = data
     obj.key = key
     return obj
end

function changeparam(obj, value)
     obj.data[obj.key] = value
end

anobject = createobject(colorCode, 1)
changeparam(anobject, 0)
Robin wrote:
kikito wrote:Only tables and functions are passed by reference (I think userdata too). The rest (i.e. numbers and strings) are copied when passed to a function.
That is not true. 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. Another way to describe it would be "call by value of reference". Basically, the reference is copied and not the value itself.

(Also note that Lua interns strings, so it doesn't really copy them ever.)
Another way to determine what is a reference is to ask: is this object ever garbage collected?
tables, userdata, strings and function refs are garbage collected, while booleans and numbers are not.
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 »

The reason I ask is because of how my GUI system is starting to look, 500 lines of code and I'm not done yet, but I haven't done any optimizing. I'll just straight up upload my code so that way you get an idea.

Is it messy? Oh hell yeah. Does it work? Yes, except for the NewTextBox's contentYMax, I'm still trying to figure that equation out. Is it completed? No. Are you going to use GUI library "X"? No, If I did, then I wouldn't have learned about stencils, and I wouldn't have learned how to do sliders, seeing how I can't see examples anywhere on the internet, and reverse engineering LoveFrames yielded diddly squat.

Now that I cleared up some random Q&A/things you really would like to slap me for. I posted the two files in the attachment.
Attachments
menus.lua
Where I create the menu's (When I'm done with the GUI class, I don't want to have to deal with it anymore)
(6.8 KiB) Downloaded 55 times
gui.lua
Base GUI Class
(14.8 KiB) Downloaded 53 times
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Editing a table by referencing.

Post by Robin »

ivan wrote:Another way to determine what is a reference is to ask: is this object ever garbage collected?
tables, userdata, strings and function refs are garbage collected, while booleans and numbers are not.
Conceptually, all types are treated the same. That booleans and numbers are not garbage collected is an implementation detail. Booleans are not really worth GC'ing, because there are only two and you're gonna need one or both of them later at some point anyway. The reason numbers are not GC'd is because the implementation uses tagged pointers, which means the pointers to non-number objects is "hidden" inside doubles (those that are signalling NaNs, which aren't produced by normal Lua code). And I don't even know how LuaJIT does it, that might be completely different.

You never get a different behavior if you pass values of different types to functions in Lua, no matter what you do. Looking at the implementation to see which types are exempt from garbage collection is counterproductive in my opinion, as it only confuses everyone who is not either using Lua's C API or actually developing a Lua implementation.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Editing a table by referencing.

Post by bartbes »

Indeed conceptually it's all (copying) references, but most types are simply immutable, so it doesn't actually matter whether the implementation uses references or not.
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 »

Oh, wait. Could I use the C api to do what I want? LuaJIT and I think Lua have some C api in right?
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest