Generation script picking the wrong asset to spawn

I’m creating foliage generation for my game; however, I’ve run into an issue where it spawns the wrong asset when selecting the biome type.
Furthermore, inside the biome table, I have only the bottom item inside the table that will spawn the right assets.

Here is part of my script

local biome_sets = { -- these are the asset sets I pick
	Flats = FoliageAssets:WaitForChild("DefaultBiome"):GetChildren(),
	Basalts = FoliageAssets:WaitForChild("Sands"):GetChildren(),
}

local biome_list = { -- these are the biomes I use to detect the material, the first one is default
	{nil,biome_sets.Flats},
	{Enum.Material.Sandstone,biome_sets.Basalts}, -- doesn't work...
	{Enum.Material.Grass,biome_sets.Basalts}, -- works
	
}
local BiomeGrowth
for i,v in pairs(biome_list) do	
	if v[1] == ray.Material then
		print(ray.Material)
		BiomeGrowth = biome_list[i][2] -- picks the "biomes" specific assets.
	else
		warn(ray.Material)
		BiomeGrowth = biome_list[1][2] -- picks the default biome assets, "aka grass"
	end
end
local random_asset = BiomeGrowth[math.random(1,#BiomeGrowth)]:Clone() -- this replicates the asset

Here is a pick of that bottom item in the table clearly working.


And here is that same generation but with sandstone, “which is the middle item in the table” not working.

So, tell me if I’m missing anything or if you need more information. Thanks!

I found the problem; the script was looping over the biome even if it did find it inside the table, thus it got overwritten.

local BiomeGrowth
for i,v in pairs(biome_list) do	
	if v[1] == ray.Material then
		BiomeGrowth = biome_list[i][2]
		break
	end
end
if BiomeGrowth == nil then
	BiomeGrowth = biome_list[1][2]
end

I didn’t think of this before took me hours. Sorry if I wasn’t anyone time, but I was getting stuck

I think I may know what is going on with your code. You are currently looping through all the possible materials in the array biome_list. The array will first go into indexes, 1, 2, then 3. However, because v[1] wont always be equivalent to ray.Materal, then the result will automatically default to the default biome assets. Not to mention you should be getting a ton of warnings. I would try this instead:

-- Immediately assing BiomeGrowth to it's default right away.
local BiomeGrowth = biome_list[1][2]
for i,v in pairs(biome_list) do
  -- If the v[1] is equal to ray.Material, then select that material.
  if v[1] == ray.Material then
    print(ray.Material)
    BiomeGrowth = v[2]
    break -- Break the loop right away to save more performance.
  end
end

local random_asset = BiomeGrowth[math.random(1,#BiomeGrowth)]:Clone() -- this replicates the asset

This solution should fix your above problem, but I could go even further. You are currently using a loop to solve this issue, but there is a way you can remove the loop entirely which would be way more performant that this. See, Luau tables can’t just be arrays, but they can also be tables. With this in mind, they could be indexed by any value, not just numbers, and that includes materials. With this in mind, if we change the biome_list variable to this:

local biome_list = {
  default = biome_sets.Flats -- This is the default biome's assets.
  [Enum.Material.Grass] = biome_sets.Basalts,
  [Enum.Material.Sandstone] = biome_sets.Basalts
}

Then we could replace our loop with this:

-- The "or" operator will select biome_list[ray.Material] if the key exists.
-- Otherwise, it will select biome_list.default.
local BiomeGrowth = biome_list[ray.Material] or biome_list.default
local random_asset = BiomeGrowth[math.random(1,#BiomeGrowth)]:Clone()

This should work similarly to the loop method, but this would remove the overhead of the loop, allowing your code to work significantly faster.

1 Like

I should ask the question, why are you using PascalCase for the BiomeGrowth variable, while you are using snake_case for the remaining variables? I’d think if I were you I would write the variable as biome_growth and not BiomeGrowth to remain consistent in formatting.

1 Like

I was learning some python recently, and now my formatting is clashing with the old and the new. Trying to fix that, thanks for the pointer though!

1 Like

I’ll try that bottom one out to cut my code down some.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.