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: Table iteration weirdness

Previous Thread :: Next Thread 
AntiFiter is not online. AntiFiter
Joined: 14 May 2009
Total Posts: 12290
30 Jul 2017 12:35 PM
So I have a table of gun names (and their data) set up like this

Guns = {
["Beretta M9"] = {},
["MP7"] = {}
...
}

At a certain point I noticed that I had no control over which gets dispalyed first in a loop like this,
for i,v in pairs (Guns) do
-- Display info in a gui
end
So I ended up added an index into their table so the script that sets up the gui would know;
["Beretta M9"] = {Index = 1, ...}

I'm a little confused why simply changing the order of THAT table didn't work, whereas this other table of skins is easy to change:

Skins = {"None", "Smoke", "Black Steel", "Chrome"}

I can easily change the order an i,v in pairs (Skins) loop displays them in, but that didn't work on the Weapons table. Why is this? They both used type of loop before I added an Index variable to the guns table.
Report Abuse
KEVEKEV77 is not online. KEVEKEV77
Joined: 12 Mar 2009
Total Posts: 6961
30 Jul 2017 12:38 PM
for i,v in pairs randomly goes through string values,



maybe for i,v in next will fix you up.
Report Abuse
MICHEAL1988351 is online. MICHEAL1988351
Joined: 26 Jan 2015
Total Posts: 582
30 Jul 2017 12:57 PM
@KEKVEK next and pairs are equivalent in the fact that pairs is simply an iterator factory that returns the `next` iterator function.

@anti If you have an index property inside each value in your table, you can create a function that gets a value for its index property:

local function GetValueForIndex(ind)
for i,v in pairs(my_table) do
if v.Index == ind then
return i, v; --return the guns name, and data
end
end
return false;
end

--now lets say that my_table has 10 indexes, starting at 1 and ending at 10:
for i = 1, 10 do
local gun, data = GetValueForIndex(i);
print(gun, data);
end
Report Abuse
AntiFiter is not online. AntiFiter
Joined: 14 May 2009
Total Posts: 12290
30 Jul 2017 12:59 PM
How can I set up a dictionary and have it run in order without having to put in an Index? In order of my table while editing

table = {
["Beretta M9"] = {info},
["MP7"] = {info},
...
}


for i,v in ...
Report Abuse
MICHEAL1988351 is online. MICHEAL1988351
Joined: 26 Jan 2015
Total Posts: 582
30 Jul 2017 01:09 PM
@anti A simple solution would be to store the string keys in an ordered array so that you can then get the order of which value you assigned first. consider:

local ordered_keys = {};
local gun_data = {};
local function AddData(ind, value)
ordered_keys[#ordered_keys + 1] = ind;
gun_data[ind] = value;
end

AddData("Scar-H", 123);
AddData("Beretta M9", 321);

Now you can iterate over gun_data in the order that you created each element like so:
for i = 1, #memory do
print(memory[i], gun_data[memory[i]]);
end
Report Abuse
AntiFiter is not online. AntiFiter
Joined: 14 May 2009
Total Posts: 12290
30 Jul 2017 01:12 PM
A simpler way would be

[1] = {Name = "Beretta M9", ...}

But a dictionary in this case is VERY important because of using indexing like this

Table.Guns[nameofgun] ..
Report Abuse
MICHEAL1988351 is online. MICHEAL1988351
Joined: 26 Jan 2015
Total Posts: 582
30 Jul 2017 01:15 PM
@Anti using my method you can still use your dictionary like a regular dictionary, it just allows you to iterate through the dictionary in order of the first created object to the last by means of an extra array.
Report Abuse
ChaoticRequiem is not online. ChaoticRequiem
Joined: 09 Dec 2011
Total Posts: 678
30 Jul 2017 01:28 PM
@OP don't listen to any of them, use ipairs and things should go in order every time
Report Abuse
AntiFiter is not online. AntiFiter
Joined: 14 May 2009
Total Posts: 12290
30 Jul 2017 01:39 PM
for WeaponName,v in ipairs(Library.Items) do

This doesn't work with dictionary tables with string names
Report Abuse
ChaoticRequiem is not online. ChaoticRequiem
Joined: 09 Dec 2011
Total Posts: 678
30 Jul 2017 01:44 PM
didn't notice it was a dictionary mb, i wouldn't use a dictionary for that in general but your call
Report Abuse
Isosta is not online. Isosta
Joined: 10 May 2015
Total Posts: 14729
30 Jul 2017 01:53 PM
gunlist = {"Beretta M9", "MP7"}

Guns = {
["Beretta M9"] = {},
["MP7"] = {}
...
}



for iteration,variable in ipairs(gunlist)
--display info of Guns[variable]
end


Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
30 Jul 2017 01:56 PM
local variable =
{
{"name", {"other", "table", "values"}},
{"name2", {"more", "values", "blah"}}
}

--You shouldn't need to index by the name if you expect them to be in order.
--however if you did............

local order = {}

for i,v in pairs(variable) do
local name = v[1]
order[name] = i
end

local get_data = function(name)
return variable[order[name]][2]
end

--usage:

local tab = get_data("name2")

print(table.concat(tab, ", "))
-->more, values, blah
Report Abuse
amanda is not online. amanda
Joined: 21 Nov 2006
Total Posts: 5925
30 Jul 2017 01:57 PM
disregard my post completely use what Isosta said
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