How multiple endings logic work?

(I dont know which tag i should use so im using scripting support)

I wanted to know the logic behind the Multiple Endings games.

What comes first in my mind that is a bunch of if elses that makes the code, for example:

if EnemyIsDead then
--Ending1
else
--Ending2
end

But i still think thats not the most effective way to do it, or it is?

1 Like

Hey, i think it would depend based on what the objective is, like for example kill / dying to an enemy then it would probably use the code u posted above

2 Likes

basically you’d have a series of objectives in a default path, and when the player does something that should change the path/ending he’s in, he’d switch to the objectives and an ending specific to that path, for example:
pseudocode

while true do
   -- if wanted, make the player go through something first here
    if didSomething (like enemyIsDead for example) then
        sigmaPath = true
        if sigmaPath then
            -- objectives

            -- a different ending
            break
    else
    -- default path and objectives
    -- normal ending
   break
end

and you can add more pathes when needed.

if(EnemyIsDead)then
    local ending=math.random(1, 5)
    if ending==1 then
    elseif ending==2 then
    elseif ending==3 then
    elseif ending==4 then
    elseif ending==5 then
    end
end
local ending=0
function ends()
	if(ending==1)then return end
	if(ending==2)then return end
	if(ending==3)then return end
	if(ending==4)then return end
	if(ending==5)then end
end

if(EnemyIsDead)then ending=math.random(1,5)end ends()

Sweet pseudocode btw… 100% how that is done.
There are many ways to do this; it all comes down to a random pick or something you can reference to make that choice.

I assume you want different endings based on what the player has or hasn’t done before the ending.

When you want an ending based on when a specific NPC is killed then you can for example have some variable like NPCkilled = true set when you kill the NPC.

You can also have other variable based on if the player has found or done other things like
HiddenDoorFound = true
AppleFound = true
etc

When it’s time to show the ending you can make the necessary checks

if NPCkilled then
 print ("Show NPC killed ending")
end

if HiddenDoorFound then
print ("Show Hidden Door Ending")
end

this code could potentially lead to both endings showing up at once… talk about a catastrophe.

1 Like

Obviously.
This will assume that one can not find the hidden door once the NPC is killed and vice verse. This is a guide and not complete code since I don’t know the structure of the game. Feel free to add more in-depth code examples. We are here to help after all.

1 Like

I’d make a module script containing all the important things like if a character has died or if something’s been unlocked. Then, after you get to the part of the game where you want this to happen if this is true, check if player has unlocked it and boom, ending 1 or whatever.

Example:
In a Module Script

local module = {}
module.CharacterDied = false
module.SecretUnlock = false
return module

|
Somewhere else in a game

local ModuleContainingStuff = require(--The module that has the variables)

local part = workspace.Part
part.Touched:Connect(function()
	ModuleContainingStuff.SecretUnlock = true
end)

|
Somewhere else in the game, again

local ModuleContainingStuff = require(--The module that has the variables)

if ModuleContainingStuff.SecretUnlock == true then
	--Ending 1
	if ModuleContainingStuff.CharacterDied == true then
		--Ending 2
	end
end

And boom, now you got yourself a ending system

1 Like