Good day to all!
I decided to try making a combat script using the Manuals.
A couple of issues came up with it
1) The GetDefenderCreatures() function throws an error: "Wrong type of argument 1 when calling function GetUnits" regardless of where I call it in the script file. I tried a similar function for the attacker - GetAttackerCreatures(). It worked the first time, but the second time I got the same problem as with the Defender.
2) The DefenderHeroMove(heroName) and AttackerHeroMove(heroName) hooks do not trigger when heroes move - there's not even a console message indicating the script entered the function body, and it doesn't output any errors at all. Once, the AttackerHeroMove(heroName) hook actually got stuck in a loop. Only the Prepare() and Start() hooks work reliably.
Could you please advise on how to fix these issues?Script contents (for now just logs to see what works and what doesn't):
print("inside combat file");DEFENDER = GetDefenderHero();print(DEFENDER);ATTACKER = GetAttackerHero();print(ATTACKER);function Prepare()print("inside Prepare function");print("Prepare function has ended");end;function Start()print("inside Start function");ShowFlyingSign(GetMapDataPath().."CombatMessage.txt", DEFENDER, 200);defenderCreatures = GetDefenderCreatures();attackerCreatures = GetAttackerCreatures();print(1);for key, creature in defenderCreatures doprint("key=", key, ", creature=", creature);end;print(attackerCreatures);print("Start function has ended");end;function DefenderHeroMove(heroName)print("inside DefenderHeroMove function");ShowFlyingSign(GetMapDataPath().."CombatMessage.txt", DEFENDER, 200);print("DefenderHeroMove function has ended");end;function AttackerHeroMove(heroName)print("inside AttackerHeroMove function");ShowFlyingSign(GetMapDataPath().."CombatMessage.txt", DEFENDER, 200);print("AttackerHeroMove function has ended");end;
you probably have an issue with these two lines:
DEFENDER = GetDefenderHero();print(DEFENDER);ATTACKER = GetAttackerHero();print(ATTACKER);here's the thing: the game defines two constants, ATTACKER and DEFENDER, which respectively specify the attacking and defending sides of the combat. By using these lines, you are essentially overwriting these constants, assigning them the names of the hero units instead of the standard sides. The subsequent errors are obvious: GetAttackerCreatures() and GetDefenderCreatures() are just wrappers around a lower-level function - inside them, GetCreatures(ATTACKER) and GetCreatures(DEFENDER) are called, respectively. Because of the overwritten constants, these functions cannot work correctly, which causes the error. The rest of the script simply fails to execute due to these errors. Accordingly, the solution is simple: use different names for your hero unit constants.