Nested Conditions vs. Individual Functions

Hi - I have what would be considered a newbie question I believe. I am writing code for a game that spawns various power ups using a module I wrote. Within my module code, there are functions that takes a number of options, such as the type of power up, the x and y coordinates, the respawn delay, and a few other parameters.

My question is - is it better to have one function that has a bunch of conditions in it to check for the power up type, or should I have a separate function for each power up.

For example:

function M.powerUp(ptype,x,y,respawn) if ptype == "attack" then --do something elseif ptype == "shield" then --do something else --do something end end

OR:

function M.attackPowerUp(x,y,respawn) -- do something end

Just trying to be as memory efficient as possible when spawning a bunch of items at one time for a level.

Thanks for any feedback!

Hi @bwilhelm,

Personally, I would use one function to handle all power-ups unless doing so would result in like 50+ conditional checks and you’re going to be spawning dozens or hundreds of power-ups in one time-sensitive call (i.e. during gameplay). In that case, it might be better to separate them out by type and call the appropriate function when needed.

Hope this helps,

Brent

Thanks Brent. The idea was to have a table for each level that contained the XY coordinates, type, and respawn information for the powerups that would be loaded with each level. I would loop through the array/table and use the powerUp() function to create them all when the level is loading. 

Great, that should work fine. Doing this process as the level “loads” is the perfect time.

Great - thanks Brent!

in your second code sample you’d still need a similar if statement “somewhere” to properly dispatch to all those individual functions, so little to gain either way.  but you can also use table indexing to accomplish something like a “switch” statement (essentially a deep if-elseif-else).  for example, and only for ideas, not as finished code, just to show you might not need “if” at all::

local leveldef01 = { { ptype="attack", x=123, y=234, respawn=true }, { ptype="shield", x=234, y=345, respawn=false }, -- etc, one line for each powerup instance } local SpawnFunctions = { attack = function(x,y,respawn) print("I'm making a 'attack' thingie",x,y,respawn) end, shield = function(x,y,respawn) print("I'm making a 'shield' thingie",x,y,respawn) end, -- etc, one function for each unique ptype } local function levelLoader(leveldef) for k,v in ipairs(leveldef) do SpawnFunctions[v.ptype](v.x,v.y,v.respawn) end end levelLoader(leveldef01)

hth

Thanks Dave - that’s a great example of what I am trying to accomplish. In your example it would seem then that having individual functions for each powerup would be the way to go? 

you can likely use that (or similar) table-indexing technique “somewhere”, but you’ll have to figure out if/how it “fits” your problem.  say, if you have lots of common code across all types of power-ups, then maybe one shared function to set up all that shared stuff first, and THEN it further dispatches via this method at some “later” time to the individual “custom” portions to finish up.  lots of ways you might go about it, but also many ways to avoid a big messy if statement.  (which i think was your original q?)

Hi @bwilhelm,

Personally, I would use one function to handle all power-ups unless doing so would result in like 50+ conditional checks and you’re going to be spawning dozens or hundreds of power-ups in one time-sensitive call (i.e. during gameplay). In that case, it might be better to separate them out by type and call the appropriate function when needed.

Hope this helps,

Brent

Thanks Brent. The idea was to have a table for each level that contained the XY coordinates, type, and respawn information for the powerups that would be loaded with each level. I would loop through the array/table and use the powerUp() function to create them all when the level is loading. 

Great, that should work fine. Doing this process as the level “loads” is the perfect time.

Great - thanks Brent!

in your second code sample you’d still need a similar if statement “somewhere” to properly dispatch to all those individual functions, so little to gain either way.  but you can also use table indexing to accomplish something like a “switch” statement (essentially a deep if-elseif-else).  for example, and only for ideas, not as finished code, just to show you might not need “if” at all::

local leveldef01 = { { ptype="attack", x=123, y=234, respawn=true }, { ptype="shield", x=234, y=345, respawn=false }, -- etc, one line for each powerup instance } local SpawnFunctions = { attack = function(x,y,respawn) print("I'm making a 'attack' thingie",x,y,respawn) end, shield = function(x,y,respawn) print("I'm making a 'shield' thingie",x,y,respawn) end, -- etc, one function for each unique ptype } local function levelLoader(leveldef) for k,v in ipairs(leveldef) do SpawnFunctions[v.ptype](v.x,v.y,v.respawn) end end levelLoader(leveldef01)

hth

Thanks Dave - that’s a great example of what I am trying to accomplish. In your example it would seem then that having individual functions for each powerup would be the way to go? 

you can likely use that (or similar) table-indexing technique “somewhere”, but you’ll have to figure out if/how it “fits” your problem.  say, if you have lots of common code across all types of power-ups, then maybe one shared function to set up all that shared stuff first, and THEN it further dispatches via this method at some “later” time to the individual “custom” portions to finish up.  lots of ways you might go about it, but also many ways to avoid a big messy if statement.  (which i think was your original q?)