Skip to content

Posts from Scripts for beginners. Auto-translated

4.5 (2 ratings)
In reply to Syryus
User Avatar
#242
Auto-translated
Syryus

Hello!

As a result, I got the following script:

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
for key, name in preserve_heroes do
if name == heroName then
local PreserveHero=1
end;
end;
if not PreserveHero then
         StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
     end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );

It intercepts all heroes without exception...
I even tried having Ilfina ("Itil") enter the region - a battle starts, and the console shows the message: Value was NIL when getting global with name 'PreserveHero'
Your error is related to the scope of the variable. If you declare a local variable inside a certain block, it will only be known within that block. In other words, in your example, the PreserveHero variable is only known in the block
if name == heroName then
local PreserveHero=1
end;

For the rest of the function, the variable is not defined, which causes the error. You can use a global variable, as suggested above, but this is generally bad practice, so you should simply change the scope of the local variable.

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
local PreserveHero
for key, name in preserve_heroes do
if name == heroName then
PreserveHero=1
end;
end;
if not PreserveHero then
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );


or, even simpler, use the built-in contains() function

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
if not contains(preserve_heroes, heroName) then
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );

 

Нет войне.
User Avatar
#243
Auto-translated
I'll warn Syrvyus that calling the function without any parameters causes the game to crash.
Gerter, will the game crash if the parameters turn out to be nil?
С уважением, }{0TT@6bI4
_________________
Группа картостроителей
Там ответы на вопросы, руководства, гайды и прочее
Discord-сервер "Герои 5: S.T.A.L.K.E.R"
Сервер по модификации "Герои 5: S.T.A.L.K.E.R"
_________________
In reply to }{0TT@6bI4
#244
Auto-translated
}{0TT@6bI4
Okay, try this:

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
PreserveHero=0
for key, name in preserve_heroes do
if name == heroName then local PreserveHero=1
end;
end;
if  PreserveHero==0 then         
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)    
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );
All heroes still enter the battle.
And there are no messages in the console.
In reply to }{0TT@6bI4
User Avatar
#245
Auto-translated
}{0TT@6bI4
I'll warn Syrvyus that calling `contatins` without parameters causes the game to crash.
Gerter, will it crash if the parameters turn out to be nil?
It will crash only if you pass nil instead of a table (you can actually fix this bug in the code). But this situation seems extremely unlikely in practice, because you need to specifically pass nil to the function for this to happen. That is, even if you write:
x = {}
x = nil
if contains(x, 1) then...
there will be no crash.
Нет войне.
User Avatar
#246
Auto-translated
Okay, thank you. If you simply make a typo in the array name, for example, `contains(perserve_heroes, "Itil")` instead of `contains(preserve_heroes, "Itil")`, will nothing happen? That's very good.
С уважением, }{0TT@6bI4
_________________
Группа картостроителей
Там ответы на вопросы, руководства, гайды и прочее
Discord-сервер "Герои 5: S.T.A.L.K.E.R"
Сервер по модификации "Герои 5: S.T.A.L.K.E.R"
_________________
In reply to }{0TT@6bI4
User Avatar
#247
Auto-translated
}{0TT@6bI4
Okay, thank you.
If you simply make a typo in the array name, for example, `contains(perserve_heroes, "Itil")` instead of `contains(preserve_heroes, "Itil")`, will nothing happen? That's very good.
It will output the default "value was nil with name..."
Нет войне.
In reply to Gerter
#248
Auto-translated
preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
local PreserveHero
for key, name in preserve_heroes do
if name == heroName then
PreserveHero=1
end;
end;
if not PreserveHero then
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );


In this form, all heroes enter the battle.

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
if not contains(preserve_heroes, heroName) then
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );

 

And this version works as intended: it lets Rangers pass through, and initiates a battle with the rest. Thank you!
Another related question: how can I add a condition so that the trigger resets after a specific player loses?
In reply to Gerter
User Avatar
#249
Auto-translated
Gerter
will output a default value of nil with name...
It's not a big deal, the main thing is that the game doesn't crash.
С уважением, }{0TT@6bI4
_________________
Группа картостроителей
Там ответы на вопросы, руководства, гайды и прочее
Discord-сервер "Герои 5: S.T.A.L.K.E.R"
Сервер по модификации "Герои 5: S.T.A.L.K.E.R"
_________________
User Avatar
#250
Auto-translated

Syryus
Another related question – how can I add a feature so that the trigger resets after a specific player loses?

after losing specifically in this battle? Here's how you can do it:
preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
if not contains(preserve_heroes, heroName) then
local owner = GetObjectOwner(heroName)
local fight_id = GetLastSavedCombatIndex()
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
while GetLastSavedCombatIndex() == fight_id do
sleep()
end
if not IsHeroAlive(heroName) and owner == player_number then
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", nil)
end
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );
Нет войне.
In reply to Gerter
#251
Auto-translated
Gerter


after losing specifically in this battle? something like this might work:

preserve_heroes={"Elleshar", "Linaas", "Gillion", "Diraya", "Itil", "Ossir", "Nadaur"}
function Def( heroName )
if not contains(preserve_heroes, heroName) then
local owner = GetObjectOwner(heroName)
local fight_id = GetLastSavedCombatIndex()
StartCombat(heroName, "Metlirn",7,44,24,146,80,148,16,147,48,50,16,48,48,44,24)
while GetLastSavedCombatIndex() == fight_id do
sleep()
end
if not IsHeroAlive(heroName) and owner == player_number then
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", nil)
end
end;
end;
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "def", "Def" );

 

I already know this option, thanks to various Manuals, but it doesn't match the intended purpose... I need the trigger to always activate as long as a specific player owns the location where the region is located. As soon as this player loses the battle, the trigger should be reset.
User Avatar
#252
Auto-translated
then everything is much simpler: ```lua function PlayerLostCheck() while GetPlayerState(player_number) ~= PLAYER_LOST do sleep() end Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'def', nil) end startThread(PlayerLostCheck)
Нет войне.
In reply to Syryus
User Avatar
#253
Auto-translated
Syryus

Another related question – how can I add a feature here so that the trigger resets after a specific player loses?

function PlayerLooseCheck(pl)
while 1 do
if GetPlayerState(pl)==PLAYER_LOST then
Trigger(...trigger reset)
end
sleep(10)
end
end;

startThread(PlayerLooseCheck, PLAYER_PLAYERNUMBER)


You can do it like this, for example. Alternatively, you can attach a trigger to the event of a player losing a hero, and use that to determine if the player has lost – this would put less load on the game, but I'm not sure if this method would work.

С уважением, }{0TT@6bI4
_________________
Группа картостроителей
Там ответы на вопросы, руководства, гайды и прочее
Discord-сервер "Герои 5: S.T.A.L.K.E.R"
Сервер по модификации "Герои 5: S.T.A.L.K.E.R"
_________________
User Avatar
#254
Auto-translated
biggrin While I was writing, Gerter already replied. The function names even matched... almost!
С уважением, }{0TT@6bI4
_________________
Группа картостроителей
Там ответы на вопросы, руководства, гайды и прочее
Discord-сервер "Герои 5: S.T.A.L.K.E.R"
Сервер по модификации "Герои 5: S.T.A.L.K.E.R"
_________________
In reply to }{0TT@6bI4
#255
Auto-translated
Colleagues, thank you very much! I will test the trigger reset options later and report back... Yesterday, I didn't get around to testing the trigger reset, but I did test the main script in different situations. It turned out that it doesn't work for heroes controlled by AI... Is that intentional? Or can something else be added to the script so that it also includes/excludes AI heroes in the battle based on the script's condition? Correction: After an AI hero enters a region, if there is an enemy hero within its movement range, it engages in battle with it. After the turn of this AI hero, if the army described in the script wins, the AI hero disappears...
User Avatar
#256
Auto-translated
Hello. The question is this: I need the hero to have a static army. That is, at a certain point, you are given, for example, 10 units of X and 5 units of Y. At a certain point, if there were losses, the army of that hero should be reset to the values of 10 units of X and 5 units of Y. Also, there should be the possibility to change the parameters, for example, a little later, you will be given 15 X, 9 Y, 3 Z. It is probably easier to create a function for "issuing an army" and place a trigger at certain points. But I will figure that out myself, but how to check the army, the hero, or whatever needs to be checked, and how to write a function so that it does not "add" units, but "changes" their values? Thank you in advance.
Автор карт:
Долина расхитителей
Выбор мира