Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is it a good practice to infinitely nest a function?

Asked by 6 years ago
Edited 6 years ago

I was thinking of how I was going to structure the flow of my game and I began to think how one state could move to another.

Of course, you could run a function, have it fully execute and complete, and then carry on, but what if the function can't efficiently reach it's end?

Say I had 3 functions:

function Menu()
--Allows user to engage in game. Calls Game() to start
end

function Game()
--User plays a game until the game until GameOver()
end

function GameOver()
--User ends game and goes back to Menu()
end

If you notice, each function calls each other. If this behavior were put in action, it would be an infinitely long function

Menu()
    Game()
        GameOver()
            Menu()
                Game()
                    GameOver()
                        Menu()
                            ....

So if you call Menu, you won't ever get past that function call

Menu() --function call, starts game

print("HI MOM") --this code is never reached

Is this a good practice to run such a system, or is there an alternative way to structure game flow?

2 answers

Log in to vote
0
Answered by 6 years ago

I guess it's alright in my book if the game doesn't have any other functions like that. Just call menu in a coroutine:

coroutine.wrap(Menu)()
0
Why is a coroutine required? randomsmileyface 375 — 6y
0
so the `print("HI MOM!")` would print. mightydifferent 85 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

It is acceptable to do this only if you use proper tail-calls -- see https://www.lua.org/pil/6.3.html for examples. If you fail to use proper tail-calls, you will eventually overflow the stack. (As mightydifferent says, you can get past your initial call to Menu using coroutines.)

The link provided mentions that doing this is much like using goto -- this statement is not in many langauges for a good reason: it can easily lead to bad code. Having gotos everywhere, if they aren't carefully organized, can make it incredibly difficult to follow the logic of a bit of code. If you have 10s of functions that all call each other for different reasons, you may have a fairly difficult time figuring out what's going on if something goes wrong.

Alternatively, much of Roblox is structured around using events. For instance, leaderboard code doesn't do much until a player enters the game, then it acts (gives the player information and sets up more event listeners so that it can act when the player dies, for instance).

0
If I were using Gotos like this, wouldn't there be a lot of garbage that can't be cleaned up? randomsmileyface 375 — 6y
0
(To be clear, lua doesn't have any 'goto' statements.) Garbage only occurs when you create new objects and then stop referencing them. Tail-calls don't create any garbage (though if you call a function that creates a new, temporary object, then of course that will have to be garbage collected). chess123mate 5873 — 6y

Answer this question