forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

table instances

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Mon Jul 24, 2006 4:35 am    Post subject: table instances Reply with quote

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
View user's profile Send private message
be2003



Joined: 20 Apr 2006
Posts: 144

PostPosted: Mon Jul 24, 2006 9:27 am    Post subject: Reply with quote

idk must be a bug or something.
_________________
- be2003
blog
Back to top
View user's profile Send private message
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Mon Jul 24, 2006 9:38 pm    Post subject: Reply with quote

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
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Tue Jul 25, 2006 5:08 pm    Post subject: Reply with quote

tostring(block[1])

use that..
Back to top
View user's profile Send private message
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Tue Jul 25, 2006 6:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
PeterM



Joined: 31 Dec 2005
Posts: 125
Location: Edinburgh, UK

PostPosted: Tue Jul 25, 2006 6:33 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Wed Jul 26, 2006 5:47 am    Post subject: Reply with quote

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
View user's profile Send private message
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Wed Jul 26, 2006 4:06 pm    Post subject: Reply with quote

ok i'll read some on metatables since i never used any
_________________
Behold! The Underminer got hold of a PSP
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Wed Jul 26, 2006 5:36 pm    Post subject: Re: table instances Reply with quote

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
View user's profile Send private message
KawaGeo



Joined: 27 Aug 2005
Posts: 191
Location: Calif Mountains

PostPosted: Thu Jul 27, 2006 2:23 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Fri Aug 18, 2006 6:29 pm    Post subject: Reply with quote

works like a dream. thnx
_________________
Behold! The Underminer got hold of a PSP
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group