weird bug

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
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

weird bug

Post by PGUp »

First code

Code: Select all

--tts = table to string
function tts(tile)
local str = "{"
for i,v in pairs(tile) do
if v == "x" then
str = str .. tile[i] .. ","
else
str = str .. "-,"
end
if i == 3 or i == 6 then
str = str .. "\n"
end
end
print(str .. "\n")
end


function love.load()

for i=1, 9, 1 do
local tl2 = 

{"", "", "",
 "", "", "",
"", "", ""}

tl2[i] = "x"
tts(tl2)
end
end
from the code above, i got the result that i was expecting, each tile has an "x" in it, and its not more than 1

Second code

Code: Select all

--tts = table to string
function tts(tile)
local str = "{"
for i,v in pairs(tile) do
if v == "x" then
str = str .. tile[i] .. ","
else
str = str .. "-,"
end
if i == 3 or i == 6 then
str = str .. "\n"
end
end
print(str .. "\n")
end


function love.load()
tl = 

{"", "", "",
 "", "", "",
"", "", ""}

for i=1, 9, 1 do
local tl2 = tl
tl2[i] = "x"
tts(tl2)
end
end
similar code, but i added a new table "tl" so i can assign "tl2" to an empty table faster,
i was expecting the same result, but instead its completely different, what is happening here?
-
User avatar
pgimeno
Party member
Posts: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: weird bug

Post by pgimeno »

Table variables are references to objects. If you do

Code: Select all

a = {"x"}
b = a
b[1] = "y"
print(a[1])
you get "y", because both a and b are references to the same object. If you do this:

Code: Select all

print(a, b)
you will see the same reference twice (e.g. in my case, "table: 0x562a3e7e6cd0 table: 0x562a3e7e6cd0"), which reveals they refer to the same object.

That means you can't have an empty table around for the purpose you're attempting to use it for: when you assign it to two variables, both will have the same reference, and the value you write to one will be written to both. One way to do it is the way you did the first time, but see also http://lua-users.org/wiki/CopyTable for some methods of copying tables.

If performance is at stake, consider also reusing the table and clearing it with a loop, see https://stackoverflow.com/questions/162 ... l#16241048
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

Re: weird bug

Post by PGUp »

pgimeno wrote: Tue May 15, 2018 5:11 pm Table variables are references to objects. If you do

Code: Select all

a = {"x"}
b = a
b[1] = "y"
print(a[1])
you get "y", because both a and b are references to the same object. If you do this:

Code: Select all

print(a, b)
you will see the same reference twice (e.g. in my case, "table: 0x562a3e7e6cd0 table: 0x562a3e7e6cd0"), which reveals they refer to the same object.

That means you can't have an empty table around for the purpose you're attempting to use it for: when you assign it to two variables, both will have the same reference, and the value you write to one will be written to both. One way to do it is the way you did the first time, but see also http://lua-users.org/wiki/CopyTable for some methods of copying tables.

If performance is at stake, consider also reusing the table and clearing it with a loop, see https://stackoverflow.com/questions/162 ... l#16241048
Weird, but thanks anyway
-
Post Reply

Who is online

Users browsing this forum: No registered users and 94 guests