|
| 04 May 2013 04:33 PM |
So I need to add an extra column to a table I have. I've got a sort of problem I've been working on for a while, posted about it previously, and was given a script example I didn't understand with two things:
_G and table.insert.
Simply stated, I want some clarification on the purposes of these things, what they do, all that kind of stuff, because I never use something I don't understand. |
|
|
| Report Abuse |
|
|
|
| 04 May 2013 04:38 PM |
_G is a global function
table.insert inserts something into a table for example
ok = {} for i, v in pairs(game.Workspace:GetChildren()) do table.insert(ok,v.Name) end
|
|
|
| Report Abuse |
|
|
|
| 04 May 2013 06:47 PM |
What is the purpose of a global function, then?
As well as that, how would I reference the new information I would have in the generated column? How would I define said information in the first place? |
|
|
| Report Abuse |
|
|
|
| 04 May 2013 06:51 PM |
global functions:
lua*learners.org*/tutorial?tut=155
remove *'s
|MassiveGman| "I reject your reality and substitute my own" |
|
|
| Report Abuse |
|
|
|
| 04 May 2013 06:53 PM |
lualearners.org
?
I don't see what's wrong with the link, but just use 'lua.bz' I guess. |
|
|
| Report Abuse |
|
|
|
| 04 May 2013 07:34 PM |
| I don't understand the purpose behind a global function. Wouldn't you be able to do all of that with a normal function? |
|
|
| Report Abuse |
|
|
|
| 04 May 2013 07:35 PM |
_G is a table not a function....
You can see this by: print(type(_G))
|
|
|
| Report Abuse |
|
|
|
| 04 May 2013 08:00 PM |
... A misnomer can be an annoying thing indeed.
So let's say I'm using getChildren() but I need to add an additional column to it. I'm assuming that in this case, I need to use _G because the table getChildren() returns is read-only- meaning that _G will be an editable copy of it.
In this case, what if I need more than one table? And why shouldn't I define my own tables like how I define my own values (assuming it's possible)? |
|
|
| Report Abuse |
|
|
|
| 04 May 2013 08:05 PM |
| Adding an additional column can be emulated by wrapping the original table in another table, and adding another table index to the wrapper as your 'new column'. |
|
|
| Report Abuse |
|
|
|
| 04 May 2013 10:19 PM |
I suppose I'm asking the wrong questions.
What is _G? What I know is that it's a 'global table'. What I don't know is when it should be used, and what for.
How do I add things to a table and reference them later? I'll take your method for emulation, so long as it works the same, but if I can't do what I need to then it doesn't matter either way. |
|
|
| Report Abuse |
|
|
|
| 05 May 2013 10:37 AM |
Theres 2 types of _G.
Serverside _G, and clientside _G.
Serverside _G runs in the server, and is used by scripts. Clientside _G runs in each client(each client has their own) and is used by localscripts.
Here are a few examples:
You could for example have this in a script:
LOL = "asd" function _G.MYFUNC() print(LOL) end
and this in another script:
_G.MYFUNC()
>LOL
|
|
|
| Report Abuse |
|
|
|
| 05 May 2013 10:38 AM |
| It would print "asd", not "LOL", sorry im dumb |
|
|
| Report Abuse |
|
|
|
| 05 May 2013 03:13 PM |
Now I'm really confused. I've been told it's a function, then a table, and then a function again.
And would I need it at all if I was using only a single script for something? |
|
|
| Report Abuse |
|
|
|
| 05 May 2013 03:18 PM |
| If it's just in one script, no. |
|
|
| Report Abuse |
|
|