Jump to content




Can't figure out what is deleting my table


2 replies to this topic

#1 CCJJSax

  • Members
  • 262 posts

Posted 20 August 2019 - 04:07 AM

I have a shortened version of my code, but it does the same weirdness. I have table (t) that only has one item in it, which is an array. After I run it through highScore() it changes to 0 even though I only reference it as tab, then again through pTab. The goal of highScore() is to return an list of all items in t in order by score. Why is this happening?

local function highScore(tab, spot)
local pTab = tab
local scores = {}
local winner
while #pTab > 0 do
  winner = {key = 0, score = 0}
  for k,v in pairs(pTab) do
   -- print(v.score)
   if v.score > winner.score then
    winner = {key = k, score = v.score}
   end
  end
  scores[#scores+1] = pTab[winner.key]
  table.remove(pTab, winner.key)
end
return scores
end
t = {
  {
    player = "TEST",
    score = 0,
  },
}
print(#t) -- prints 1 as it should
h = highScore(t)
print(#t) -- prints 0 when it should still be 1


#2 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 20 August 2019 - 05:27 AM

See here.

A table in Lua is only a reference, like objects in Java. This means that any changes you do to the parameter in the highScore function as a side effect changes the original value.
If you really want to copy a table instead of doing
pTab = tab
you need to use something like this function:
function copyTable(src, dest)
  for k, v in pairs(src) do
	if type(v) ~= "table" then
	  dest[k] = v
	else
	  dest[k] = {}
	  copyTable(v, dest[k])
	end
  end
end

Also you could just sort the scores table like this:
table.sort(scores, function(a, B)/>/> return a.score > b.score end)

If you use that in the highScore function that will of course as a side effect sort the "t" table. But if the original order of the scores doesn't matter you can just do it like this. Else you need to first copy the table using the function above and then sort the new table.

Edited by Luca_S, 01 September 2019 - 05:33 AM.


#3 CCJJSax

  • Members
  • 262 posts

Posted 20 August 2019 - 06:06 AM

View PostLuca_S, on 20 August 2019 - 05:27 AM, said:

See here.

A table in Lua is only a reference, like objects in Java. This means that any changes you do to the parameter in the highScore function as a side effect changes the original value.
If you really want to copy a table instead of doing
pTab = tab
you need to use something like this function:
function copyTable(src, dest)
dest = {}
for k, v in pairs(src) do
  if type(v) ~= "table" then
	dest[k] = v
  else
	dest[k] = {}
	copyTable(v, dest[k])
  end
end

Also you could just sort the scores table like this:
table.sort(scores, function(a, B)/>/> return a.score > b.score end)

If you use that in the highScore function that will of course as a side effect sort the "t" table. But if the original order of the scores doesn't matter you can just do it like this. Else you need to first copy the table using the function above and then sort the new table.

That's actually what I ended up doing. just did a pairs iteration and cloned the table that way, then learned about table.sort. I swear I've done stuff like highScore numerous times in the past without issue. Thanks for helping out!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users