[Solved] Unintentionally synced tables?

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.
Post Reply
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

[Solved] Unintentionally synced tables?

Post by Skeletonxf »

So I was having loads of issues with two tables where one started as a copy of the other due to Lua's pass by reference I later found out about. Having understood that, the issues don't seem to go away.

I've got a table with user input data in passed to a function and I want to make a second table which is a copy of the first except for it's two nested tables of sieve and sieveDown's x and y values being transformed by hump.camera to the game camera system. The format of inputData is

Code: Select all

inputData = {
	sieve = {},
	sieveDown = {}
}
when empty and when filled looks like this

Image

However that's actually what I wanted the game coord table to look like, because the x and y entries there are the transformed versions rather than pixels from top left of the screen where my mouse is that they should be and start off as.

I've tried editing the second line in the above code to being a function to perform a shallow copy, a deep copy, the current version and the current version without the dummy entry and they all cause the same bug which is evident in the print logs.

Code: Select all

function input.setupGameCamera(inputData)
  local gameCameraInputData = {sieve = {},dummy=false,sieveDown = {}} --copyTable(inputData) --{sieve = {},sieveDown = {},dummy=false} 
  local function queryInRange(x, y)
    return (x > window.gameArea.x and x < window.gameArea.x+window.gameArea.w and
      y > window.gameArea.y and y < window.gameArea.y+window.gameArea.h)
  end
  -- get each table entry v and replace the x and y coords with the
  -- gameCamera versions
  for k, v in pairs(inputData.sieve) do
    if queryInRange(v.x,v.y) then
      --local ox, oy = v.x, v.y
      print("0:"..v.x)
      local gx, gy = gameCamera:worldCoords(v.x,v.y)
      print("1:"..v.x)
      gameCameraInputData.sieve[k] = v
      print("2:"..v.x)
      gameCameraInputData.sieve[k].x, gameCameraInputData.sieve[k].y = gx,gy ---Buggy line
      print("3:"..v.x)
      v.cam = true
    end
  end
  ... ect
Print logs

Code: Select all

0:798 -- x coord of mouse on screen
1:798 -- x coord of mouse on screen
2:798 -- x coord of mouse on screen
3:393 -- x coord of mouse in game camera!
The line this change occurs between is modifying gameCameraInputData not the key-value pair of inputData?
I thought this should only happen if my second line was gameCameraInputData = inputData but it's still happening?

Full Love file attached (first log on screen is inputData, second is gameCameraInputData)
Attachments
tools.love
(116.34 KiB) Downloaded 79 times
Last edited by Skeletonxf on Sun Jul 24, 2016 11:46 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Unintentionally synced tables?

Post by zorg »

Code: Select all

print("1:"..v.x)
gameCameraInputData.sieve[k] = v
print("2:"..v.x)
Just a hunch, but are you sure you don't mean to assign an empty table (= {}) there, instead of v? The moment you assing v to it, you basically shallow-copy the v table, which, from what i understand, is not what you want.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Skeletonxf
Citizen
Posts: 87
Joined: Tue Dec 30, 2014 6:07 pm

Re: Unintentionally synced tables?

Post by Skeletonxf »

Omg that might be it. I want to replicate all the key value pairs there then over ride the x and y which as I've done it currently must be passing the reference of the sub table!
Edit: Aha the very pass by reference I thought was the problem was still the problem.
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: [Solved] Unintentionally synced tables?

Post by pgimeno »

If you want to copy a table:

http://lua-users.org/wiki/CopyTable
zorg wrote:The moment you assing v to it, you basically shallow-copy the v table, which, from what i understand, is not what you want.
Actually, that assigns a reference to the v table, but it does not make a shallow copy. A shallow copy is a copy that is one level deep (it copies all the elements, but if the table contains nested tables, the nested tables are references to the original tables, not copies). At first sight, a shallow copy would have worked because v doesn't have nested tables.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [Solved] Unintentionally synced tables?

Post by zorg »

You're right, i mis-wrote that, i did mean reference; thanks for pointing it out. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 69 guests