Bakerley
|
  |
| Joined: 12 Oct 2009 |
| Total Posts: 12011 |
|
|
| 26 Mar 2016 07:28 AM |
Hi folks! I was just wondering whether there was any significance to using "i,v" in pairs. Does this matter? Can I just do "a,b" and get the same results? I've noticed that different pairs have been used, such as "b,a", "g,c", and "r,t". Again, do these have any significance, or can I make the pairs whatever I want?
Bonus question!!! Is there a way to determine how far the chain of children goes from a model? For example, if there was a part within a model, and a part within that part, is there a way of determining through code that the model has grandchildren?
Thank you so much in advance! |
|
|
| Report Abuse |
|
|
|
| 26 Mar 2016 08:02 AM |
i,v are variables and thus can be anything you want. For an example of how this works, see the two tables below, and what i and v would equl for them.
t1 = {21,22,23} Element 1 - i = 1, v = 21 Element 2 - i = 2, v = 22 Element 3 - i = 3, v = 23 t2 = {val1=21,val2=22,val3=23} Element 1 - i = "val1", v = 21 Element 2 - i = "val2", v = 22 Element 3 - i = "val3", v = 23
Often they are called Key and Value, with key refering to the index (i.e. val1 in the above example) and value being the value represented by the key. Keys are unique, but the values do not have to be.
|
|
|
| Report Abuse |
|
|
|
| 26 Mar 2016 08:06 AM |
I did not read your bonus question but that is something that is simply solved via recursion. Recursion is code that calls itself until a certain condition is met (or if you forgot to set a end condition, until you task manager close ROBLOX) and works well with depth. Example below:
function printAllChildren(thing) for index, child in pairs(thing:GetChildren()) do print(index, child.Name) if(#child:GetChildren() ~= 0) then printAllChildren(child) -- here is where the recursion happens. end end end |
|
|
| Report Abuse |
|
|
Bakerley
|
  |
| Joined: 12 Oct 2009 |
| Total Posts: 12011 |
|
|
| 26 Mar 2016 08:24 AM |
Thank you very much for your help! Both of your answers really helped. I didn't really expect there to be such a simple way to determine the line of children.
|
|
|
| Report Abuse |
|
|
|
| 26 Mar 2016 08:27 AM |
| Not a problem! Recursion is your answer most tasks that would generally be quite long or impossible to do manually. |
|
|
| Report Abuse |
|
|
Tynezz
|
  |
| Joined: 28 Apr 2014 |
| Total Posts: 4945 |
|
|
| 26 Mar 2016 08:30 AM |
Although, Red is right, this is a bit more efficient:
local collectedparts={}
local function scan(p) for index,value in ipairs(p:GetChildren()) do table.insert(collectedparts,value) scan(value) end end |
|
|
| Report Abuse |
|
|
|
| 26 Mar 2016 10:56 AM |
Tynezz, I'm not sure what your basis is for claiming that's for efficient, but I can assure you, it is not.
Imagine the model below:
Model - Part Part Part Part Part
How many times would the function I gave out be called? Once, for the main object, as the secondary parts do not have any children. Your method would be called for an many children there are in the object + 1. In the most ideal situation where no parts have any children, your method will still have to be run 11 different times. |
|
|
| Report Abuse |
|
|
|
| 26 Mar 2016 10:57 AM |
| Please ignore the 11, it would supposed to be n+1 times! |
|
|
| Report Abuse |
|
|
blugote
|
  |
| Joined: 28 Nov 2015 |
| Total Posts: 103 |
|
|
| 26 Mar 2016 11:07 AM |
Props for making a well written thread with a clear question. Don't really see that here nowadays.
|
|
|
| Report Abuse |
|
|
Tynezz
|
  |
| Joined: 28 Apr 2014 |
| Total Posts: 4945 |
|
|
| 26 Mar 2016 11:34 AM |
| ' if(#child:GetChildren() ~= 0) then' this is unneeded |
|
|
| Report Abuse |
|
|
|
| 26 Mar 2016 11:53 AM |
| If you were to implement your version - fair enough - but that does not make it more efficient. Removing a check in favour of executing an operation more does not count as optimized, it's redundant. It means the operation can no longer be expanded upon as each expansion will increase execution time exponentially. |
|
|
| Report Abuse |
|
|