Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Mar 2012 08:09 PM |
If I do....
do local atable = {Hehe = "Hello", Thing={OtherThing={}}} function FindThingInTable(table) for a, b in pairs(table) do if b.Hehe=="Hello" then return b.Thing.OtherThing; end end end
local x = FindThingInTable(atable) x.OtherOtherThing = "DFDF" end
Would:
print(atable.Thing.OtherThing.OtherOtherThing)
result in "DFDF"?
Can I return an index of a table and use it? |
|
|
| Report Abuse |
|
|
pwnedu46
|
  |
| Joined: 23 May 2009 |
| Total Posts: 7534 |
|
|
| 29 Mar 2012 08:15 PM |
| a would be the index, b returns the value: "Hello" in this case. |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Mar 2012 08:22 PM |
Hmm...... so if I return a then would I end up with...
atable.Thing.OtherThing |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2012 08:35 PM |
No.
First of all:
"function FindThingInTable(table)"
"table"
STOP OVERWRITING LIBRARIES
Second, 'b' for the first repetition would be the string "Hello", which does not have a 'Hehe' property of any sort, causing an error. The second iteration (were it to actually get to the second iteration), 'b' would equal atable.Thing, which does not contain a 'Hehe' index either.
Third:
local x = FindThingInTable(atable)
atable isn't define, it went out of scope.
As for the stuff about being able to change a table outside of the scope in which it was defined, you can do that as long as you have a reference to the table. That print line would have to be inside the scope of the table, though, else it would just error. |
|
|
| Report Abuse |
|
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
| |
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
| |
|
|
| 29 Mar 2012 08:41 PM |
"He can use getfenv(0).table"
No, he can't. He could use getfenv(2).table I think, but it's much easier and much better practice to NOT OVERWRITE THINGS.
"Nope its IN the scope."
...
Do you have any idea at all what you're talking about?
|
|
|
| Report Abuse |
|
|
oxcool1
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15444 |
|
| |
|
|
| 29 Mar 2012 08:50 PM |
| May I ask why you used 'do' at the beginning? |
|
|
| Report Abuse |
|
|
Quenty
|
  |
| Joined: 03 Sep 2009 |
| Total Posts: 9316 |
|
|
| 29 Mar 2012 08:53 PM |
Just to set the scope. That WAS an example, not my real code, which isn't NEARLY that sloppy.
Anyway, thanks guys. :D |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2012 08:54 PM |
"But its defined, its still inside the scope."
Pretty sure you have no idea what you're talking about. Which scope are you saying it's inside? Because it most definitely isn't defined in the scope where he tried to use it from.
"Yes, he can. Level 0 is the script's environment."
No, it's not. |
|
|
| Report Abuse |
|
|
|
| 29 Mar 2012 08:56 PM |
Nevermind. Haven't used Lua in too long. You're right about the getfenv(0) thing.
However, I still don't understand what the heck you're talking about with the scopes. |
|
|
| Report Abuse |
|
|