|
| 29 Dec 2015 01:37 PM |
| Because 'local proxy = t:Clone()' doesn't work |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2015 01:41 PM |
If you're trying to clone individual things inside the table you would do
for i,v in pairs(table) v:Clone() end |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2015 01:42 PM |
for i,v in pairs(table) do end
forgot the do |
|
|
| Report Abuse |
|
|
|
| 29 Dec 2015 01:44 PM |
shouldn't it be:
local t={}; local n={} local v=t:children(); for i=1,#v do n[i]=v[i]; end
? |
|
|
| Report Abuse |
|
|
mudkip99
|
  |
| Joined: 17 Jun 2008 |
| Total Posts: 3362 |
|
|
| 29 Dec 2015 01:48 PM |
Since tables are passed by reference and not value, you have to manually copy each element of a table in a loop, something like this is what I use:
local t1 = {0, 1, 2, 3} local t2 = {}
for i, v in ipairs(table1) do t2[i] = t1[i] end
|
|
|
| Report Abuse |
|
|
|
| 29 Dec 2015 01:49 PM |
I did not know he meant literally cloning the actual table in that case you can simply do
table1 = {'bruh'} Table2 = table1 |
|
|
| Report Abuse |
|
|
rvox
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 5380 |
|
| |
|
mudkip99
|
  |
| Joined: 17 Jun 2008 |
| Total Posts: 3362 |
|
|
| 29 Dec 2015 01:53 PM |
Threatboy, the problem with doing that is that you aren't actually copying the array, you are just assigning two different variables to the same set of information (arrays aren't passed by value like other basic data types)
That can cause problems; if you need to modify one of the arrays, you will unintentionally modify both of them. You need to manually copy each element of the array if you want to modify one of them and keep the other the same. |
|
|
| Report Abuse |
|
|
sycips
|
  |
| Joined: 21 Mar 2011 |
| Total Posts: 1368 |
|
|
| 29 Dec 2015 02:03 PM |
@Threatboy101 That would actually set a pointer to the same table... This is what I mean by that:
Table1 = {"TEST"}
print(Table1[1]) --> TEST
Table2 = Table1 Table2[1] = "CHANGED" print(Table2[1]) --> CHANGED
print(Table1[1]) --> CHANGED
By doing Table2 = Table1, both variables are actually THE SAME table. Both variables point the same table!
Now for the solution. There is no table manipulation function to clone a table, so we can create out own function:
function CloneTable(Table) local NewTable = {} for i,item in pairs(Table) do NewTable[i] = item end return NewTable end
NOTE that the items in the table will not change. The only thing this does is creating a new table which point to the same items. BUT, when you change this second table, the first table wont change anymore! (Don't try this with objects in a table. If you remove an object where both tables are pointing to and you remove that object, ofcourse both tables wont point to it anymore...) Let me show you:
Table1 = {"TEST"}
print(Table1[1]) --> TEST
Table2 = CloneTable(Table1) Table2[1] = "CHANGED" print(Table2[1]) --> CHANGED
print(Table1[1]) --> TEST
I hope this explaination helped you and I hope you understood the difference between variables and tables pointing to values and variables actually being values.
Good luck with scripting! :D
~sycips~ |
|
|
| Report Abuse |
|
|