generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Question about Tables

Previous Thread :: Next Thread 
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
14 Dec 2016 11:28 AM
Let's say there are 3 players in game and their names are inserted into a table
Names = {"Player1", "Player2", "Player3"}

now let's say Player2's name is removed from the table.
Would a script read the table as

Names = {"Player1", "Player3"} or as Names = {"Player1", "", "Player3"}


and more importantly if I were to use a # operator on it #Names, would that output as 2 or 3?
Report Abuse
Soybeen is not online. Soybeen
Joined: 17 Feb 2010
Total Posts: 21462
14 Dec 2016 11:44 AM
Names = {}
-- populate the table
for _,obj in pairs(game.Workspace:GetChildren()) do
if obj:IsA("BasePart") and string.sub(obj.Name,1,4) == "Part" then
table.insert(Names,obj.Name)
print(obj.Name) -- print what's int the table
end
end

-- scan the table and remove the undesired key
for key,v in next,Names do
if v == "Part2" then
table.remove(Names,key)
end
end

-- print all values in the table
for _,v in next,Names do
print(v)
end


>> prints
Part1
Part2
Part3

Part1
Part3


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
14 Dec 2016 12:38 PM
If you remove it using table.remove, it will shift all the elements right of it to the left. If you just set it to an empty string or nil, it won't.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
14 Dec 2016 01:12 PM
static int tremove (lua_State *L) {
int size = aux_getn(L,1);
int p = luaL_optint(L,2,size);

if (p != size)
luaL_argcheck(L, 1 <= p && p <= size+1,1,"position out of bounds");

lua_rawgeti(L, 1, p);

for ( ; pos < size; p++) {
lua_rawgeti(L,1,p+1);
lua_rawseti(L,1,p);
}

lua_pushnil(L);
lua_rawseti(L,1,p);

return 1;
}


Report Abuse
RubyEpicDragon is not online. RubyEpicDragon
Joined: 02 May 2015
Total Posts: 1211
14 Dec 2016 02:00 PM
Names = {"Player1", "Player3"}
.
Report Abuse
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
15 Dec 2016 09:30 AM
soybeen thank you very much.

also TimeTicks I can't understand any of that D:
I've never heard of any of that stuff before.
what is "p++"? and "if (p != size)"?

lua_rawgeti? is that like some kind of lua spaghetti?
Report Abuse
RubyEpicDragon is not online. RubyEpicDragon
Joined: 02 May 2015
Total Posts: 1211
15 Dec 2016 10:05 AM
did i also help?
Report Abuse
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
15 Dec 2016 10:13 AM
in your own way, and cntkillme did answer my question lol

I was only asking because the game I'm making requires a change in a table when players join or leave, to remove their names from a ready list so that a different function for team sorting doesn't bug out, and I was worried that the function might miscount the players that left, adding their names to the team anyway even though there's nobody there
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
15 Dec 2016 10:52 AM
its C code

essentially what it does (correct me if im wrong), get the index you specify, gets the next index after that, sets the current index you specifie to nil, then sets the next index to that position of the index you spefified. So thats how it "shifts" the table down. Its sorta like bitwise shifting. lshift will shift the bits left, rshift will shift the bits right. Sorry for the bad comparision


Report Abuse
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
15 Dec 2016 12:02 PM
so is this how rbxlua is actually handling it? because i copy pasted this into a script to read it better and it didn't come up with any errors or red lines, which tells me that it's usable o.o

i wanna learn more about this. it looks so professional :O
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
15 Dec 2016 12:39 PM
########################################################################
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
15 Dec 2016 12:40 PM
i literally just copy and pasted the source of table.remove from lua org


Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
15 Dec 2016 03:02 PM
Here's a Lua equiv., it might help you better understand the C code:

local function tremove(tbl, idx)
local size = #tbl
local p = idx or size -- if no idx is given, use end of the array
if p ~= size then -- must be in the range of [1, size+1]
assert(1 <= p and p <= size+1, ("bad argument #1 (%s)"):format("position out of bounds"))
end
local t = rawget(tbl, idx) -- save the value to remove
while p < size do -- shift all elements to the right, left
rawset(tbl, p, rawget(tbl, p + 1))
p = p + 1
end
tbl[p] = nil -- get rid of the last element since it was already copied over left
return t -- return the removed element
end


Report Abuse
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
15 Dec 2016 03:26 PM
im not gonna lie this didn't really help explain it. It's not you, it's me. I didn't understand this either. there are so many things going on here i've never encountered before in my 8 years on this site.

never seen "rawget" or "rawset" before, or the ":format" thingy, and it took me a good 20 minutes to figure out "idx" was a variable lmao
Report Abuse
HalfPinky456 is not online. HalfPinky456
Joined: 10 Apr 2011
Total Posts: 6236
15 Dec 2016 03:29 PM
metatables are going to be your friend from now on
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
15 Dec 2016 03:30 PM
xiao here's a "rewrite" not using those:

local function tremove(tbl, idx)
local size = #tbl
local p = idx or size -- if no idx is given, use end of the array
if p ~= size then -- must be in the range of [1, size+1]
assert(1 <= p and p <= size+1, "bad argument #1 (position out of bounds)")
end
local t = tbl[idx] -- the value that is going to be removed
while p < size do
tbl[p] = tbl[p + 1] -- shift left
p = p + 1
end
tbl[p] = nil -- get rid of the last element since it was already copied over left
return t -- return the removed element
end

Report Abuse
AggressiveCatch is not online. AggressiveCatch
Joined: 17 Jul 2011
Total Posts: 5840
15 Dec 2016 03:30 PM
i've never learned to use metatables and i don't feel like i'm missing out on too much
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
15 Dec 2016 03:31 PM
In terms of roblox, you really aren't.


Report Abuse
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
15 Dec 2016 10:24 PM
I THINK I understand it now, but I'm not sure. idx stands for index, right?

so it looks for index first and if there isnt one, just get the size of the table.
i think lol

idk what assert is, or means, but from all the math it's doing I'm guessing it moves it to the end?
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
15 Dec 2016 10:27 PM
with a quick google you could find that assert throws and error when the given argument is non-truthy.


Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
15 Dec 2016 10:27 PM
an error*


Report Abuse
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
15 Dec 2016 10:32 PM
I tend not to google things that I don't understand on roblox when I can get the answers from people I trust, like cntkillme or others as ##### ## he is. When I google things like that I tend to stick with the first bit of information I find, and 9 times out of 10 it's wrong information. Bad for me lol Besides when I get the answers here, usually people are more than happy to explain things to me when I don't understand. I like learning but yeesh I wish it wasn't so hard to take in new information. Stupid head injury...
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image