Skip to content

Posts from Scripts

4.8 (8 ratings)
In reply to Heroist
User Avatar
Auto-translated
SetHeroCombatScript(heroname, script)
heroname - the script name of the hero,
script - an xdb file specifying the lua file.
All that remains is to loop it with a condition or place it on a PLAYER_ADD_HERO_TRIGGER.
Карты для Героев Меча и Магии 5
Одиночные: Завеса срывается, Посол, Последний рывок, Эхо Пустоты
Кампания: Империя Единорога

Существа NCF
Орден Порядка: 1
Нейтралы: 1 2 3 4
User Avatar
Auto-translated
What about linking a battle script to a hero? The thing is, I would like to be able to hire such a hero, say, in a tavern, but in this case, he appears on the map without the linked script and loses his functions.
You can write your function in a general combat script (as a mod), with a check for the hero's name. I think that would work.
---
Ah, well, the RedHaven option is probably easier.
 

К А
Стикеры GBF в Telegram
In reply to Heroist
Auto-translated
Heroist

What about linking a battle script to a hero? The thing is, I would like to be able to hire such a hero, say, in a tavern, but in this case, he appears on the map without the linked script and loses his functions.
Is there a function that, if such a hero appears on the map, automatically links the battle script to him? Or is there another way?
SetHeroCombatScript(heroname, script)
is not the best option. The script will only work if the hero is under AI control, and it will also be reset if the hero is lost.

I already described it in the neighboring topic:
it is done by adding a few lines of script to the 'combat-common.lua' file - this file is launched by the game when any battle starts.
If needed, I can explain it in more detail. But as always, the best option is to study other people's maps that use battle scripts.
In reply to JonnyP
User Avatar
Auto-translated
JonnyP
- this isn't the best option. The script will only work if the hero is controlled by AI, and it will also reset if that hero is lost.
Yes, it will reset, but this can be fixed with a loop or a trigger, and the function works fine with player-controlled heroes as well.
Карты для Героев Меча и Магии 5
Одиночные: Завеса срывается, Посол, Последний рывок, Эхо Пустоты
Кампания: Империя Единорога

Существа NCF
Орден Порядка: 1
Нейтралы: 1 2 3 4
In reply to RedHeavenHero
Auto-translated
RedHeavenHero
Yes, it will be reset, but this can be fixed using a loop or a trigger, and the function works normally with the player's hero as well.
Is there an error in the manual, or is Google translating incorrectly?
This function allows you to specify which scenario will be executed when fighting this hero. The script only works if the hero is under the control of the AI players, and no other scenarios are set in the location where the battle takes place (town, garrison, etc.). The scenario is automatically reset when the player loses this hero in some way.
In reply to JonnyP
User Avatar
Auto-translated
The manual contains a lot of information, but not all of it is true. Although, perhaps the patches fixed this issue, while the manual remained as it was.
Карты для Героев Меча и Магии 5
Одиночные: Завеса срывается, Посол, Последний рывок, Эхо Пустоты
Кампания: Империя Единорога

Существа NCF
Орден Порядка: 1
Нейтралы: 1 2 3 4
User Avatar
Auto-translated
Thanks for the answers, and I found a very interesting old map called "Refugee" and played it a bit =)
---

a) SetHeroCombatScript(heroname, script) – does this mean it applies a combat trigger to your hero for the rest of their life? Because I have a hero with a combat trigger as the main one, so even if he dies, it will still be loooooose, so if I apply it once at the beginning of the game, that's what I need.
b) Regarding 'combat-common.lua', I still couldn't find any explanations in that thread. What exactly is considered "a few lines"? I would like more details, if possible.

Whatever
User Avatar
Auto-translated
SetHeroCombatScript('Alaric', CombatScript.lua) --- doesn't work. It gives an error: value was nil. In the manual, it says to write .lua, but I tried .xdb as well, and it still doesn't work.

Whatever
In reply to Heroist
User Avatar
Auto-translated
Therefore, the file path should be enclosed in quotes and include a forward slash.
'/CombatScript.xdb#xpointer(/Script)'
Карты для Героев Меча и Магии 5
Одиночные: Завеса срывается, Посол, Последний рывок, Эхо Пустоты
Кампания: Империя Единорога

Существа NCF
Орден Порядка: 1
Нейтралы: 1 2 3 4
User Avatar
Auto-translated
SetHeroCombatScript('Alaric', 'CombatScript.lua') --- the script doesn't exist, or something like that. The same applies to the .xdb extension.
Maybe you need to specify a longer path?

Whatever
In reply to Heroist
Auto-translated
Heroist

Regarding 'combat-common.lua', I couldn't find any explanations in that thread. What exactly is considered "a few lines"? I would like more details, if possible.
1. In the main file 'MapScript.lua', we define the strings. (let's introduce the variable 'vasya')
vasya = "Scripted hero name";
SetGameVar("vasya", vasya);
2. Create a new lua file in the map archive in the 'scripts' folder. Let it be 'battle.lua'
3. Copy the 'combat-common.lua' file into the map archive and add a few lines.
sleep(1);
vasya = GetGameVar("vasya");
if GetHeroName(GetAttackerHero()) == vasya then
	doFile("/scripts/battle.lua");
	print("Vasya has entered the battle!");
	else if GetHeroName(GetDefenderHero()) == vasya then
		doFile("/scripts/battle.lua");
		print("Vasya has entered the battle!");
	end;
end;
4. Open the created file 'battle.lua' and write the following:
vasya = GetGameVar("vasya");
if GetHeroName(GetAttackerHero()) == vasya then
	human = ATTACKER;
	comp = DEFENDER;
	human_hero = GetAttackerHero();
	print("player - attacker");
	else human = DEFENDER;
	comp = ATTACKER;
	human_hero = GetDefenderHero();
	print("player - defender");
end;

The variables 'human', 'comp', 'human_hero', 'comp_hero' are introduced for convenience - they will be needed in almost all functions of the combat script, regardless of whether the player is attacking or defending. Also, for convenience, print statements are used to output information to the console.
All that remains is to add additional conditions and their consequences in the Prepare() or Start() functions - this is where the map author's creativity comes into play :)

Ps: From point 3, it is clear that the combat script will not run if the hero 'vasya' is not involved in it.

Pss: This approach is more versatile than SetHeroCombatScript, in which the loss of a hero can also be considered as him fleeing the battlefield and then being ransomed in the tavern.
In reply to JonnyP
User Avatar
Auto-translated
Heroist, I wrote the correct file path in the message above. JonnyP, fleeing is also a defeat in the game => no subsequent redemption.
Карты для Героев Меча и Магии 5
Одиночные: Завеса срывается, Посол, Последний рывок, Эхо Пустоты
Кампания: Империя Единорога

Существа NCF
Орден Порядка: 1
Нейтралы: 1 2 3 4
In reply to RedHeavenHero
Auto-translated
RedHeavenHero

JonnyP, fleeing is also a defeat in the game => no subsequent ransom.
Well, why not. You kidnapped a hero, fled, bought more troops, and attacked again. It's not necessary to make the quest 'the hero must survive'.
User Avatar
Auto-translated
RedHeavenHero - SetHeroCombatScript('Alaric', '/CombatScript.xdb#xpointer(/Script)') --- the same error again.

JonnyP - I'll try to do it on a different map now.

Added 2 hours 1 minute ago
It's a pity... why doesn't it work...

Whatever

Statistics

Welcome our newest member: Emil