So I have a module to choose names for a building. I thought of having a system like so:
local buildingTypes = { ["Corporation"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Shop"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Food"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Utility"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}} } function buildings:ChooseName() local buildingType = buildingTypes[math.random(1, #buildingTypes)] -- ERROR LINE local word1 = buildingType[1][math.random(1, #buildingType[1])] local word1 = buildingType[2][math.random(1, #buildingType[2])] local name = "%s %s" for i = 1, 2, 1 do name:Format(name, i, buildings[buildingType..i[math.random(1, #buildings.buildingType..i)]]) end return(name) end
It errors saying that "bad argument #2 to 'random' (interval is empty)"
Since you're using a dictionary instead of a numeric table, you can't do buildingTypes[someNumber]
, you also can't use #buildingTypes
The operator #
means number of items. You can use it to get the number of items in tables/strings, but you can't use it to get the number of items in a dictionary. If you do, it'll return nil. That's why the script is erroring, because you're trying to do math.random(1, nil)
There is a way of doing what you want to do though:
If you really wanted to get a random item in a dictionary, you could store the keys in a separate table and then get a random key from that table.
local buildingTypes = { ["Corporation"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Shop"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Food"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Utility"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}} } local keys = {} --populate the keys table with the buildingType keys for key in pairs(buildingTypes)do table.insert(keys, key) end --get a random key: local randomNumber = math.random(1, #buildingTypes) local randomKey = keys[randomNumber] --get building at random key: local randomBuilding = buildingTypes[randomKey]
If we restructure the table a bit, we can have each building
be a table instead. This way we can use math.random()
to get a random position in the table:
added a variable called MAX_OCCUPANTS as an example, add/remove whatever you want.
local buildingTypes = { {Type = "Corporation", MAX_OCCUPANTS = 320}, {Type = "Shop", MAX_OCCUPANTS = 420}, {Type = "Food", MAX_OCCUPANTS = 100}, {Type = "Utility", MAX_OCCUPANTS = 10 }, } local randomPosition = math.random(1, #buildingTypes) local randomBuilding = buildingTypes[randomPosition]
@royaltoe With some minor modifications, your system worked! Thank you! For anyone interested, here's the code:
llocal buildingTypes = { ["Corporation"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Shop"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Food"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}}, ["Utility"] = {[1] = {"Some stuff here"}, [2] = {"Some stuff here"}} } local function randType() local keys = {} for i, v in pairs(buildingTypes) do keys[#keys + 1] = v end return keys[math.random(1, #keys)] end function buildings:ChooseName() local buildingType = randType() local prefixes = buildingType[1] local suffixes = buildingType[2] local prefix = tostring(prefixes[math.random(1, #prefixes)]) local suffix = tostring(suffixes[math.random(1, #suffixes)]) local name = string.format("%s %s", prefix, suffix) return(name) end