Skip to content

Posts from Scripts for beginners.

4.5 (2 ratings)
Auto-translated
Hey, can you guys suggest a script that, when you kill an enemy and enter a zone, a hostile hero appears?

Added 1 minute ago
Hey, can you guys suggest a script that, when you kill an enemy and enter a zone, a hostile hero appears?
Auto-translated
Could you please tell me how to check if a creature belongs to a specific castle faction or a player's faction? And after that, how to apply a certain buff to that creature?
User Avatar
Auto-translated
I_Am_Walking, as far as I know, there is no function for directly determining a faction. For heroes, you can do this indirectly by checking for faction-specific skills.
In reply to Jack_of_shadows
Auto-translated
So, if you need to check, for example, the faction of a town or a player, is it impossible to do this directly? Can it only be done indirectly, through creatures?
User Avatar
Auto-translated
I think that if it's a standard map without any custom elements, you can simply use a script to check all the heroes of all players in one day. If there's necromancy, it means it's a Necromancer; if there's summoning, it means it's a Demon, and so on, and store the results in a global table (key - player number, value - faction). Then you can use it for either heroes or castles.
In reply to Jack_of_shadows
Auto-translated
it's certainly worth a try, but someone who isn't very familiar with programming will have to spend a long time studying the manuals.
User Avatar
Auto-translated
Why not? I would recommend reading any introductory article on Lua from the internet, and even better, save it and refer to it periodically.
User Avatar
Auto-translated
I_Am_Walking,
you can determine the faction of a castle using the GetTownRace function, and set the courage and mood using SetMonsterCourageAndMood.
Карты для Героев Меча и Магии 5
Одиночные: Завеса срывается, Посол, Последний рывок, Эхо Пустоты
Кампания: Империя Единорога

Существа NCF
Орден Порядка: 1
Нейтралы: 1 2 3 4
Auto-translated
Guys, help me out. For some reason, after the question (QuestionBox) and the "OK" answer, the battle doesn't start (StartCombat). If I copy StartCombat into the function with the question, then everything works fine. I made a link to another function in the question.
In reply to sergejez
User Avatar
Auto-translated
Hello everyone, could you please advise me on something? Is it possible to configure a function to execute not based on a specific trigger from the list of triggers, but based on something else, such as the construction of a building or the appearance of a specific artifact or creature with a particular hero or player? It seems that the command that starts a new thread does something similar (although I don't really know what a thread is). `startThread(fProc, vParam1, vParam2, ...)` Theoretically, if I understand correctly, it should constantly check for a specific condition, but I can't figure out how to use it. Specifically, I need to replace one creature with another in a hero's army when a specific building is constructed in a town. Currently, I have something like this: ``` function replacecreature (); if GetHeroCreatures( "Sarge", CREATURE_ARCHER ) >= 1 and GetTownBuildingLevel( "cheast_town", TOWN_BUILDING_DWELLING_2 ) = 2; then AddHeroCreatures( "Sarge", CREATURE_LONGBOWMAN, 1 ); RemoveHeroCreatures( "Sarge", CREATURE_ARCHER, 9999 ); else nil; end; end; startThread( "replacecreature", TOWN_BUILDING_DWELLING_2 ); ``` What's wrong with this, and is it even possible to do what I'm describing? If not, is there a similar alternative? Thank you very much in advance for any advice.
User Avatar
Auto-translated
Don't create a trigger. But you can implement a check in an infinite loop. Your code looks fine, but you need to wrap your `if` statement in the following structure: ``` while 1 do if... sleep(4); end; ``` Don't forget about `sleep`, otherwise everything will freeze...
 

К А
Стикеры GBF в Telegram
In reply to Ment
User Avatar
Auto-translated
I fixed it like this, but it still doesn't work
I must be doing something wrong; if anyone knows exactly where the error is, I would be very grateful if you could let me know



P.S. while I'm at it, I accidentally leaked the mod I'm trying to put together))
User Avatar
Auto-translated
function replacecreature() while 1 do if GetHeroCreatures("Sarge", CREATURE_ARCHER) >= 1 and GetTownBuildingLevel("cheast_town", TOWN_BUILDING_DWELLING_2) == 2 then RemoveHeroCreatures("Sarge", CREATURE_ARCHER, 9999); AddHeroCreatures("Sarge", CREATURE_LONGBOWMAN, 1); end; sleep(4); end; end; startThread(replacecreature); Is this how it should be?
for one, I accidentally revealed the mod I'm trying to put together
You've piqued my interest... --- However, the code I wrote has a weak point. I suspect you don't actually have 9999 archers. It will throw an error. You need to check the exact number of archers and subtract that exact number! If the goal is to have no more of them in the army. But for now, check it without subtracting the archers.
 

К А
Стикеры GBF в Telegram
In reply to Ment
User Avatar
Auto-translated
Ment
function replacecreature ()
while 1 do
if GetHeroCreatures( "Sarge", CREATURE_ARCHER ) >= 1 and GetTownBuildingLevel( "cheast_town", TOWN_BUILDING_DWELLING_2 ) = 2 then
RemoveHeroCreatures( "Sarge", CREATURE_ARCHER, 9999 );
AddHeroCreatures( "Sarge", CREATURE_LONGBOWMAN, 1 );
end;
end;
end;

startThread( "replacecreature" );


I checked, it still doesn't work.
(It doesn't even start, whether the building is built at the beginning of the map or during the game).

I read somewhere that you should write not
startThread( "replacecreature")
but
startThread( replacecreature)
I tried this option too, but it didn't give any results.

In Novik's Manual, there is also this snippet about startThread:
while(1) do
local heroes = GetPlayerHeroes(PLAYER_1)
for i, h in heroes do
if....
but I have no idea how to apply it and what it means because I'm a newbie

Statistics

Welcome our newest member: Emil