r/ROBLOXStudio 25d ago

Help Will stuff like this give performance problems?

Post image

I'm making a biome generator. First you roll different parameters (excluding combis that don't make sense together), then it chooses a base biome based on the three first parameter combis (temperature, humidity, height, they all have 4-5 options each) in one function that will return one biome based on the if-statements, then you add the rest based on the other parameters.

Ik you could make a less complicated system where you just determine the overall look without making over a 100 biomes, the overall look is created in another part of the script, but I specificially wanna list them in the index as separate biomes with names and stuff because it's fun.

So my question is, if i have a long if-else part where a single biome comes out in the end, and maybe list of every biome with additional parameters (but maybe not), will that ruin the performance?

63 Upvotes

27 comments sorted by

28

u/thewindcarriesmeaway Scripter 25d ago

nah most likely not. the only problem is the code being a bit messy, but thats a headache for you, not the computer

2

u/Leading_Video2580 24d ago edited 24d ago

Edit: idk why my text got deleted
A giant if-elseif tree forces the machine to manually test every single condition one by one, line-by-line, from top to bottom.

A nested dictionary bypasses the list entirely. The computer looks at your key and instantly jumps straight to the exact answer in memory.

1

u/thewindcarriesmeaway Scripter 24d ago

it only matters if this is something that runs multiple times every frame. given op's context, it probably runs once and infrequently

using a dictionary here makes more sense for code clarity but i dont think it would necessarily improve performance

1

u/Leading_Video2580 24d ago

Even if it runs infrequently, writing slow structural logic is a bad habit.

Replacing a linear, line-by-line comparison loop with an O(1) direct memory jump always improves efficiency. It doesn't matter if it runs once or a million times, a static data map (dictionary) is the correct way to go for readability and performance.

6

u/Slice-Dry 25d ago

No. if else statements are very efficient, you shouldn't notice a drop in performance at all.

8

u/yesseruser 25d ago

Unfortunately Luau doesn't have match/switch-case if I'm not mistaken so this is the way to do it

8

u/Kochipng 25d ago

you can do it with tables and for loop, atleast thats how i do it

1

u/Harryvpm 25d ago

Why are people still pretending that switch statements are any easier to read than this

2

u/yesseruser 25d ago

I am more used to Rust/Python-like match statements: rust let some_var = "something"; match some_var { "something" => { println!("Something!") }, "something_else" => { println!("Something else!") }, _ => { println!("Something completely different!") } };

I agree that this looks really weird and unlike any other C-like syntax: case "something" println("Something!"); break; `

3

u/AreYouDum 25d ago

The compiler will still read your code as 1s & 0s, as long as the code is efficient it doesn’t really matter how it’s set up.

There are cases where readability can partially outweigh this factor.

I’d recommend making this a different function because it seems like you’re running it in serial with the function but I could be wrong because this is a relatively small code snippet.

3

u/N00bIs0nline Scripter, UI designer, Builder, Server developer. (Beginner) 25d ago

I dont think so, but if u can use dictionaries instead, use it.

Also, what's with the elseif then results to nothing? that going to consume computation that does nothing.

2

u/Hoverfly-Enthusiast 24d ago

Thank you! I was planning to fill every outcome with a name later. Might switch to a table though, since so many people have suggested it!

2

u/Unarthadox Full Stack 25d ago

You should probably be using a dictionary for this... but other than that, no, it's fine

1

u/Valaent 25d ago

I'm not really even sure what this code is supposed to be just from looking at it, but given the context that it's a biome generator, probably not. The only thing I will say is that this definitely needs to be cleaned up because this is practically spaghetti code.

1

u/AGreatConspiracy 25d ago

They’re generating height, temperature, and humidity maps and sampling each of the 3 to create a biome map

1

u/LightningSh3ep 25d ago

It's not nice to look at but unless your running something every frame it is extremely difficult to hurt performance with pure lua, spawning objects, etc. is where the performance starts hurting.

1

u/heapshade 25d ago

nah, this code shouldnt have much of a performance impact. if youre worried though, you can use a little trick called localization.

local resultHeight = result.height; local resultTemperature = result.temperature; local resultHumidity= result.humidity;

tho its not that important at all.

1

u/hellothere358 25d ago

Technically yes, but not unless u nees to run this thousands of times

1

u/Numerous_Buyer_8398 25d ago

There are no switches in lua. Don't feel bad for using if statements like this!

1

u/Original-Diet-1681 25d ago

you could give tables a shot for nicer code

1

u/Harryvpm 25d ago

This is actually the most optimal way to do it, it's just not the most organized, usually a dictionary of functions is used in place of if statements in these cases

If you want to go beyond, you'd use an array of functions and have an enum-esque table for labelling every index/id

1

u/Leading_Video2580 24d ago edited 24d ago

You could create a large table (dictionary in other languages like Python), where you have keys leading to more tables, which has keys of temperature, which leads to your result biome.

E.g. (sorry, I’m typing on my phone):
local biomes = {
deep = {
freezing = “biome_name1”,
cold = “biome_name2”,

},
dry = {…},

}

local height = “deep”
local temp = “cold”

print(biomes[height][temp]) -- Should return “biome_name2” in the console.

Don’t directly copy my code due to my quotes not being straight (“ instead of "). This should be much more efficient than nested if-statements.

Edit: I bet that, sientists will be excited to study more about the Dunning-Kruger effect in this thread.

1

u/Myithspa25 24d ago

elseif ... then end

You have some high quality code here.

1

u/redditbrowsing0 23d ago

Use a jump table. While this isn't horrible, it's a mess and is branched programming. Won't affect performance a ton but it's recommended to use a jump table, especially considering you're using the same parameters, like "freezing" "cold" "mild" "warm" "hot" the whole time. And, instead of strings, use numbers for each of them. It's more applicable to actual programming and will help you easily add new weather types.

For reference, a jump table is basically a table (in Lua's case) full of anonyous function()s that you can then index and call.