Page 1 of 1

(Request)disappear NPC and others appear after finishing a level

Posted: Sun Feb 06, 2022 3:33 pm
by Shodax
Sorry I don't know much about lua. But I need one to know how to do something with saveData.
In one of my levels there is an invasion of enemies to the city but after defeating the boss they no longer appear, nor does the boss when the level is passed and the level is re-entered.
It occurred to me that the script would disappear the layer that belongs to the invaders and that it apprehends the layer where the villagers apprehend
When I finish the mission or how else can I achieve this?
first of all, Thanks

Re: (Request)disappear NPC and others appear after finishing a level

Posted: Sun Feb 06, 2022 6:57 pm
by deice
when the boss is killed, trigger an event. then try using the following code:

Code: Select all

local BOSS_EVENT = "boss_killed" --name of event triggered when the boss dies
local ENEMY_LAYER = "enemies" --name of the layer with enemies on it

function onEvent(eventName)
	if(eventName == BOSS_EVENT) then
		SaveData[Level.filename()] = SaveData[Level.filename()] or {}
		SaveData[Level.filename()].missionClear = true
	end
end

function onStart()
	if(SaveData[Level.filename()] and SaveData[Level.filename()].missionClear == true) then
		Layer.get(ENEMY_LAYER):hide(true)
	end
end
this should also be portable across levels should you ever need another level with the same mechanic.
(if it doesn't work, let me know cause i'm typing this off the top of my head haha~)

Re: (Request)disappear NPC and others appear after finishing a level

Posted: Mon Feb 07, 2022 1:17 pm
by Shodax
deice wrote:
Sun Feb 06, 2022 6:57 pm
when the boss is killed, trigger an event. then try using the following code:

Code: Select all

local BOSS_EVENT = "boss_killed" --name of event triggered when the boss dies
local ENEMY_LAYER = "enemies" --name of the layer with enemies on it

function onEvent(eventName)
	if(eventName == BOSS_EVENT) then
		SaveData[Level.filename()] = SaveData[Level.filename()] or {}
		SaveData[Level.filename()].missionClear = true
	end
end

function onStart()
	if(SaveData[Level.filename()] and SaveData[Level.filename()].missionClear == true) then
		Layer.get(ENEMY_LAYER):hide(true)
	end
end
this should also be portable across levels should you ever need another level with the same mechanic.
(if it doesn't work, let me know cause i'm typing this off the top of my head haha~)


Thank you.
I work wonders, I'll give you credit in my episode.
I made some modifications to show the villagers and remove other things. I leave it in case someone serves it, too

Code: Select all


local BOSS_EVENT = "final" --name of event triggered when the boss dies
local ENEMY_LAYER = "invasores" --name of the layer with enemies on it
local ALLY_LAYER = "toads"
local BLOCK_LAYER = "bloque1"
local PLANTA1_LAYER = "planta1"
local PLANTA2_LAYER = "planta2"
local PLANTA3_LAYER = "planta3"
local JEFE_LAYER = "bowser"

function onEvent(eventName)
	if(eventName == BOSS_EVENT) then
		SaveData[Level.filename()] = SaveData[Level.filename()] or {}
		SaveData[Level.filename()].missionClear = true
	end
end

function onStart()
	if(SaveData[Level.filename()] and SaveData[Level.filename()].missionClear == true) then
		Layer.get(ENEMY_LAYER):hide(true)
		Layer.get(BLOCK_LAYER):hide(true)
		Layer.get(PLANTA1_LAYER):hide(true)
		Layer.get(PLANTA2_LAYER):hide(true)
		Layer.get(PLANTA3_LAYER):hide(true)
		Layer.get(JEFE_LAYER):hide(true)
		Layer.get(ALLY_LAYER):show(true)
	end
end