I'm trying to keep the flowers from planting onto each other, making a collision check. I don't want to check all the flowers in the table though, for economic reasons, just the ones that are close.
If it is easier, in terms of the code you have to write, to check all of the flowers then you should do that. Even if that means the code is wasting time checking flowers that are not near others. If later on that ends up creating slow-down or lag
then go back and modify the code, making it more complex so that it only checks necessary flowers.
To put it another way:
write the simplest code even if it seems inefficient. You admit that you are an inexperienced programmer. Therefore, personally, I believe you should focus on writing the simplest code that works. Don’t worry right now if the code is wasting resources. Get the code working the way you want and
then think about optimizing it later.
I also wanted to make a suggestion about the large ‘map’ table. To improve readability (in my opinion) you can put the table in other file and then load the table from there, like so:
Code: Select all
-- Let's say this is in a file called "map.lua"
return {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, ... }
...
}
-- Now this code below would be in your "main.lua" file, or wherever
-- you intend to use 'map'.
local map = require("map.lua")
Notice how the ‘map’ file consists of just a return statement followed by the huge table? The result is that when you require() the file it will return that table, which is why the example assigns the result of require() to a variable. It’s not something you need to do, just a personal suggestion that may help you organize things if you start feeling like there’s too much going on in one file too keep track of.
All that said, take a step back and instead try the simplest approach you can think of for the collision detection, even if it looks horribly inefficient. There is not much value in optimizing code that isn’t doing what you want in the first place.