Skip to content

Posts from Scripts

4.8 (8 ratings)
User Avatar
8 years ago
Auto-translated
then you can assign the OBJECT_TOUCH_TRIGGER to each dwelling and check who touched it. If it's your hero, display the appropriate message; if it's the AI, for example, use a script to add units from the dwelling to its army.
In reply to Gerter
User Avatar
8 years ago
Auto-translated
I have another question: does anyone know how to check if a specific spell has been learned? I couldn't find such a trigger in the documentation.
Example:
If a hero has learned a specific spell, then he gets +1 to spell power.
User Avatar
8 years ago
Auto-translated
LetoX, there isn't a trigger for that, but you can create a flow that constantly checks if the hero has a specific spell. See the examples with flows (the startThread function) a couple of pages back, but replace the condition with KnowHeroSpell(heroName, spell).
In reply to Jack_of_shadows
User Avatar
8 years ago
Auto-translated
Hey guys, I need some help. I wrote a combat script for a Necromancer to cast mass spells of darkness depending on the level. ``` function start() lev = GetGameVar("level") + 0; mana = GetUnitManaPoints(GetAttackerHero()) SetUnitManaPoints(GetAttackerHero(), 100); if lev < 10 then UnitCastGlobalSpell(GetAttackerHero(), 212) elseif lev < 20 then UnitCastGlobalSpell(GetAttackerHero(), 212) UnitCastGlobalSpell(GetAttackerHero(), 215) elseif lev < 30 then UnitCastGlobalSpell(GetAttackerHero(), 212) UnitCastGlobalSpell(GetAttackerHero(), 215) UnitCastGlobalSpell(GetAttackerHero(), 210) else UnitCastGlobalSpell(GetAttackerHero(), 212) UnitCastGlobalSpell(GetAttackerHero(), 215) UnitCastGlobalSpell(GetAttackerHero(), 210) UnitCastGlobalSpell(GetAttackerHero(), 213) end SetUnitManaPoints(GetAttackerHero(), mana); end ``` In battle, nothing happens (the variable is set, this was checked with another script).
User Avatar
8 years ago
Auto-translated
A simple solution would be to add a sleep function after SetUnitManaPoints(). And does the console display any errors? (I think it should display something like 'attacker-hero can't cast spell...')
In reply to Gerter
User Avatar
8 years ago
Auto-translated
The console is actually my best friend – it simply doesn't turn on. :D I think I wrote the correct line in profiles (Ctrl+C, Ctrl+V), and I also wrote "show_console" in the input field in my profile, but the console still doesn't turn on; it would certainly make things easier.
User Avatar
8 years ago
Auto-translated
LetoX, possible errors:
1. The start() function seems to need to start with a capital letter - Start(). Lua is case-sensitive.
2. Mana might not be given to the hero immediately; you can reliably wait using this construct:
SetUnitManaPoints(GetAttackerHero(), 100);
repeat sleep() until(GetUnitManaPoints(GetAttackerHero()) == 100);
3. Spell casting functions may fail in some cases, causing the script to crash at that point. There is a safer way to run them through a separate thread:
startThread(UnitCastGlobalSpell, GetAttackerHero(), 212);
And also, after the spell casting functions and before restoring the previous mana, it is better to put sleep(1), so that the mana has time to be deducted before it is restored.
In reply to Jack_of_shadows
User Avatar
8 years ago
Auto-translated
I don't know what the problem is anymore. I tried a simple script to summon creatures in a number equal to level * 2 – it seems to work, everything looks correct, but nothing happens.
function Start()
lev = GetGameVar("level") + 0;
mana = GetUnitManaPoints(GetAttackerHero())
SetUnitManaPoints(GetAttackerHero(), 100);
repeat sleep() until(GetUnitManaPoints(GetAttackerHero()) == 100);
if lev < 10 then
startThread(UnitCastGlobalSpell, GetAttackerHero(), 212);
elseif lev < 20 then
startThread(UnitCastGlobalSpell, GetAttackerHero(), 212);
startThread(UnitCastGlobalSpell, GetAttackerHero(), 215);
elseif lev < 30 then
startThread(UnitCastGlobalSpell, GetAttackerHero(), 212);
startThread(UnitCastGlobalSpell, GetAttackerHero(), 215);
startThread(UnitCastGlobalSpell, GetAttackerHero(), 210);
else
startThread(UnitCastGlobalSpell, GetAttackerHero(), 212);
startThread(UnitCastGlobalSpell, GetAttackerHero(), 215);
startThread(UnitCastGlobalSpell, GetAttackerHero(), 210);
startThread(UnitCastGlobalSpell, GetAttackerHero(), 213);
end
sleep(2);
SetUnitManaPoints(GetAttackerHero(), mana);
end
User Avatar
8 years ago
Auto-translated
LetoX, could it be that you made a mistake somewhere else in the combat script, and the entire interpreter crashes? If you have a working version with a different spell and a non-working current version, try gradually switching from one to the other and see at what stage it stops working. Here is a piece of my similar script that works normally: player_hero = '';

function Start()

if(IsHuman(ATTACKER)) then
player_hero = GetAttackerHero();
else
player_hero = GetDefenderHero();
end

combatSetPause(not nil);

local last_mana = GetUnitManaPoints(player_hero);
SetUnitManaPoints(player_hero, 100);
repeat sleep() until(GetUnitManaPoints(player_hero) == 100);
startThread(UnitCastGlobalSpell, player_hero, SPELL_MASS_STONESKIN);
sleep(1);
SetUnitManaPoints(player_hero, last_mana);
repeat sleep() until(GetUnitManaPoints(player_hero) == last_mana);

setATB(player_hero, 0.5);
sleep(1);
combatSetPause(nil);

end
User Avatar
8 years ago
Auto-translated
Here's the question. I want the hero Laszlo to move sequentially between three points. After the trigger activates, he should immediately go to the last point and rest there. What needs to be corrected? ``` function Patrul () MoveHero("Laszlo", 197, 184, GROUND) MoveHero("Laszlo", 159, 196, GROUND) MoveHero("Laszlo", 156, 158, GROUND) Trigger(OBJECT_CAPTURE_TRIGGER, "derevo1", nil) end; Trigger(OBJECT_CAPTURE_TRIGGER, "derevo1", "Patrul")
User Avatar
8 years ago
Auto-translated
OrnsteinDragonslayer, commands should not be executed all at once; instead, the units should first move to one point, wait for the hero to arrive (by checking their coordinates or placing a region with a trigger at that point), and only then be sent to the second point, and so on.
User Avatar
8 years ago
Auto-translated
The problem is as follows: I want the "uderzhatG" quest to activate after capturing the "gorodSvyat" town. Also, if this town belongs to Player 2, then the quest should fail, and the player should lose the mission. Additionally, the quest should be completed after the "Nemor" hero is defeated. However, after I add the functions below to my code, everything breaks, and the quest doesn't trigger (although the initial settings are applied). What's the problem? ``` function uderzhat () SetObjectiveState("uderzhatG", OBJECTIVE_ACTIVE); Trigger(OBJECT_CAPTURE_TRIGGER, "gorodSvyat", nil ); end; Trigger(OBJECT_CAPTURE_TRIGGER, "gorodSvyat", "uderzhat" ); function CheckSvyat () while (1) do sleep(2); if GetObjectOwner("gorodSvyat")==PLAYER_2 then sleep (2) SetObjectiveState("uderzhatG", OBJECTIVE_FAILED); sleep (15) Loose (); break end; end; end; startThread (CheckSvyat) function UderzhatComplete () SetObjectiveState("uderzhatG", OBJECTIVE_COMPLETED); Trigger (PLAYER_REMOVE_HERO_TRIGGER, "Nemor", nil) end Trigger (PLAYER_REMOVE_HERO_TRIGGER, "Nemor", "UderzhatComplete")
In reply to OrnsteinDragonslayer
8 years ago
Auto-translated
OrnsteinDragonslayer
The problem is this: I want the "uderzhatG" quest to activate after capturing the "gorodSvyat" town. Also, if this town belongs to Player 2, then the quest should fail, and the player should lose the mission. Also, after the hero "Nemor" is defeated, the quest should be completed. But after I add the functions below to my code, everything breaks, and it doesn't trigger (although the initial settings are applied). What's the problem?

function uderzhat ()
SetObjectiveState("uderzhatG", OBJECTIVE_ACTIVE);
Trigger(OBJECT_CAPTURE_TRIGGER, "gorodSvyat", nil );
end;

Trigger(OBJECT_CAPTURE_TRIGGER, "gorodSvyat", "uderzhat" );

function CheckSvyat ()
while (1) do
sleep(2);
if GetObjectOwner("gorodSvyat")==PLAYER_2 then
sleep (2)
SetObjectiveState("uderzhatG", OBJECTIVE_FAILED);
sleep (15)
Loose ();
break
end;
end;
end;

startThread (CheckSvyat)

function UderzhatComplete ()
SetObjectiveState("uderzhatG", OBJECTIVE_COMPLETED);
Trigger (PLAYER_REMOVE_HERO_TRIGGER, "Nemor", nil)
end

Trigger (PLAYER_REMOVE_HERO_TRIGGER, "Nemor", "UderzhatComplete")

I might be wrong, but perhaps you could try this trigger: Trigger(OBJECT_CAPTURE_TRIGGER, "gorodSvyat", "CheckSvyat" );
In reply to Kartoha
User Avatar
8 years ago
Auto-translated
Kartoha
I might be wrong, but it might be possible to place a trigger: Trigger(OBJECT_CAPTURE_TRIGGER, "gorodSvyat", "CheckSvyat" );

Instead of a start thread or where?
In reply to OrnsteinDragonslayer
8 years ago
Auto-translated
OrnsteinDragonslayer
Instead of a starting thread, or where?


After this line:

Trigger(OBJECT_CAPTURE_TRIGGER, "gorodSvyat", nil )

Does it work?

Statistics

Welcome our newest member: recijeb