| View previous topic :: View next topic |
| Author |
Message |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Mon Jul 24, 2006 4:35 am Post subject: table instances |
|
|
They really piss me off, those instances.
When I create the table | Code: | Row = {}
block1 = {2,0,1}
Row[1] = block1
Row[2] = block1
Row[1][1] = 15
|
then, Row[2][1] becomes 15 too. That's really annoying. Anyone knows a way to avoid this? _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
be2003
Joined: 20 Apr 2006 Posts: 144
|
Posted: Mon Jul 24, 2006 9:27 am Post subject: |
|
|
idk must be a bug or something. _________________ - be2003
blog |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Mon Jul 24, 2006 9:38 pm Post subject: |
|
|
I guess so. I'm sure I didn't make a mistake. It's a real nuisance. Shine, or whoever, can you fix this? _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Tue Jul 25, 2006 5:08 pm Post subject: |
|
|
tostring(block[1])
use that.. |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Tue Jul 25, 2006 6:04 pm Post subject: |
|
|
| romero126 wrote: | tostring(block[1])
use that.. |
I think I don't get your hint romero... I changed my code to:
| Code: | Row = {}
block1 = {2,0,1}
Row[1] = block1
Row[2] = tostring(block1)
Row[1][1] = 15
print(tostring(block1))
print(Row[2][1])
|
Output =
table: 0x1007eb98
error: source1.lua:7: attempt to index field `?' (a string value) _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
PeterM
Joined: 31 Dec 2005 Posts: 125 Location: Edinburgh, UK
|
Posted: Tue Jul 25, 2006 6:33 pm Post subject: |
|
|
Take this with a huge rock of salt since I'm not a Lua programmer.
Does lua handle non-POD (Plain Old Data) types by reference? If so...
| Code: | Row[1] = block1
Row[2] = block1 |
...would make both rows share the same block1. So you'd have to make a copy somehow. |
|
| Back to top |
|
 |
romero126
Joined: 24 Dec 2005 Posts: 200
|
Posted: Wed Jul 26, 2006 5:47 am Post subject: |
|
|
I used it as a refrence.. not as actual code.
the reason why it has this problem is because its relational variables. Not direct data based variables.
| Quote: | Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.
|
http://www.lua.org/manual/5.0/manual.html#5.4 for more information. learn to research
There are always work arrounds but it still is retarded and half assed. If you really want good code, work with metatables. or Redesign your code so it works logically where all variables are pointers to actual data and not the other way arround. |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Wed Jul 26, 2006 4:06 pm Post subject: |
|
|
ok i'll read some on metatables since i never used any _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
Shine
Joined: 03 Dec 2004 Posts: 728 Location: Germany
|
Posted: Wed Jul 26, 2006 5:36 pm Post subject: Re: table instances |
|
|
| the underminer wrote: | | then, Row[2][1] becomes 15 too. That's really annoying. Anyone knows a way to avoid this? |
You can copy the table:
| Code: |
function copyTable(old)
local new = {}
table.foreach(old, function (item) table.insert(new, item) end)
return new
end
Row = {}
block1 = {2,0,1}
Row[1] = copyTable(block1)
Row[2] = copyTable(block1)
Row[1][1] = 15
|
|
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Thu Jul 27, 2006 2:23 am Post subject: |
|
|
table.foreach() function is now deprecated. Use for i,v in ipairs(t) do ..., instead. See Lua 5.1 ref manual.
| Code: | function CopyTable(old)
local new = {}
-- for i, item in ipairs(old) do table.insert(new, item) end
for i, item in ipairs(old) do new[i] = item end
return new
end
|
_________________ Geo Massar
Retired Engineer
Last edited by KawaGeo on Sat Aug 19, 2006 12:27 am; edited 1 time in total |
|
| Back to top |
|
 |
the underminer
Joined: 03 Oct 2005 Posts: 124 Location: Netherlands
|
Posted: Fri Aug 18, 2006 6:29 pm Post subject: |
|
|
works like a dream. thnx _________________ Behold! The Underminer got hold of a PSP |
|
| Back to top |
|
 |
|