Skip to content

Scripts for beginners.

User Avatar
#287
Auto-translated
BlueHeavenHero
The scenario is already 47% complete; only some design and scripting need to be finished. And now I'm stuck. The plot involves the main hero transforming into a Necromancer. How can I do this in a way that keeps the survival objective relevant?

It's actually quite easy. Reserve a Necromancer hero, create a quest to save the main hero, but make it technically empty (OBJECTIVE_KIND_MANUAL) so you can control it through scripts. In the scripts, write something like this:

NowNecromancer = 0 --Condition that will be triggered when the hero becomes a Necromancer

function prim_save_gg( heroName ) --You can name this function differently; this is just for clarity.
if ( heroName == Duncan ) and NowNecromancer ~= 1 then --Duncan is used as an example. Insert the script name of your hero here.
SetObjectiveState("script name of the quest", OBJECTIVE_FAILED) --Fail the quest to save the main hero.
sleep(10) --Give the player time to see the quest failure and realize that they have lost the main hero.
Loose() --Fail the mission, as our hero died before becoming a Necromancer.
elseif ( heroName == Duncan ) and NowNecromancer == 1 then --We lose the hero, but because we now have the condition that we are a Necromancer, we don't lose.
print("Everything is okay, the condition is met. He died, and he died. It happens. Now I'm a Necromancer, maybe I'll go buy some skulls.")
end
if ( heroName == DuncanNecro ) and NowNecromancer == 1 then --Our hero is a Necromancer, and the condition that we made him a Necromancer has been met.
SetObjectiveState("script name of the quest", OBJECTIVE_FAILED) --Fail the quest to save the main hero.
sleep(10) --Give the player time to see the quest failure and realize that they have lost the main hero.
Loose() --Fail the mission, as our hero died after becoming a Necromancer.
end
end

function TransformToNecromancer() --Here, specify what will happen after the main hero becomes a Necromancer. Where it will be called from is up to you.
--Various things from the author; who knows what conditions he has?
NowNecromancer = 1 --Set the condition so that the game doesn't fail after the main hero is removed.
sleep(2) --Just in case, give the game time to process the information.
DeployReserveHero("DuncanNecro", x, y, GROUND/UNDERGROUND) --Place the new main hero so that the game doesn't fail if the player only has one hero and no castle with a tavern.
sleep(2) --Also, just in case, give the game time to process everything.
RemoveObject("Duncan") --Painlessly remove the main hero.
end

Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_1, "prim_save_gg") --Trigger to track hero losses by player PLAYER_1.

Give it a try!