Page 1 of 2
Turn-based 'checkpoint' system
Posted: Sun Jun 04, 2023 6:31 pm
by ChristianC
Hi, I'm trying to code in a system in which the player has to capture a particular set of victory hexes by a certain turn in order to avoid defeat and continue the game - something like a turn-based 'checkpoint' system. I used Chat GPT to develop lua code for it and plugged in the particular hexes, it seems close but not working (rextester does not flag any of the code).
Code: Select all
function CheckVictory()
local currentPlayer = GetCurrentPlayer() -- Get the current player
if currentPlayer == 0 then -- Only check victory for allied player
local turn = GetCurrentTurn() -- Get the current turn number
if turn > 5 then -- Check if it's after turn 5
local hexesToCapture = { -- Specify the victory hexes to capture
{ x = 50, y = 32 },
{ x = 50, y = 24 },
{ x = 59, y = 12 }
}
local capturedHexes = 0
for i, hex in ipairs(hexesToCapture) do
if IsHexCaptured(hex.x, hex.y, currentPlayer) then
capturedHexes = capturedHexes + 1
end
end
if capturedHexes ~= #hexesToCapture then
SetPlayerLoss(currentPlayer) -- Set the player as defeated if not all victory hexes are captured
end
else
SetPlayerLoss(currentPlayer) -- Set the player as defeated if it's after turn 5
end
end
end
function IsHexCaptured(x, y, player)
local hex = GetHex(x, y) -- Get the hex at coordinates (x, y)
if hex then
local occupier = GetHexOccupier(hex)
if occupier and occupier:GetPlayer() == player then
return true -- Hex is captured by the player
end
end
return false -- Hex is not captured by the player
end
When I put the function "CheckVictory" as the "Condition" in the editor I get error: "44: attempt to call a nil value (global 'GetCurrentPlayer')"
When I put function "IsHexCaptured" as the "Condition" I get error: "74: attempt to call a nil value (global 'GetHex')"
In the editor I have the "Actions" set to "StartTurnAction"
Any ideas how to get this working?
Re: Turn-based 'checkpoint' system
Posted: Sun Jun 04, 2023 7:59 pm
by Grondel
ChristianC wrote: ↑Sun Jun 04, 2023 6:31 pm
Any ideas how to get this working?
a lot of the calls done there do not exist like that/are done diffrently in PC2. thats why u get "nilvalue" errors.
sers,
Thomas
Re: Turn-based 'checkpoint' system
Posted: Sun Jun 04, 2023 9:27 pm
by Grondel
ChristianC wrote: ↑Sun Jun 04, 2023 6:31 pm
Any ideas how to get this working?
had to get my brain of a diff problem and did some cleaning of ur chatgpd-script.
This should get u a bit further. In the editor folder of the game is a properties.txt that helps with the available calls.
sers,
Thomas
function CheckVictory()
local currentPlayer = world:GetPlayer(0) -- Get the current player
if world.round > 5 then -- Check if it's after turn 5
local hexesToCapture = {{50,32},{50,24},{59,12}}
local capturedHexes = 0
for i,_hex in ipairs(hexesToCapture) do
if IsHexCaptured(i,currentPlayer) then
capturedHexes = capturedHexes + 1
end
end
if capturedHexes ~= #hexesToCapture then
SetPlayerLoss(currentPlayer) -- Set the player as defeated if not all victory hexes are captured
end
else
SetPlayerLoss(currentPlayer) -- Set the player as defeated if it's after turn 5
end
end
function IsHexCaptured(hex,player)
local hex = world:GetHex(hex) -- Get the hex at coordinates (x, y)
occupier = hex.owner
if occupier == player then
return true break end-- Hex is captured by the player
return false -- Hex is not captured by the player
end
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 05, 2023 7:36 pm
by ChristianC
Grondel wrote: ↑Sun Jun 04, 2023 9:27 pm
Hi! I replaced the original code with yours, but the scenario would not start. I put the code into rextester and it flagged "27: 'end' expected (to close 'if' at line 26) near 'break'"
I'm unsure why this error appears as I can see "end" is included after "break" on line 27...
I tried removing "break" and the code then runs without issue according to rextester; but when I put that code into the .lua file, the scenario does not launch as previously.
I've taken a look through the properties.txt, but haven't developed new ideas from it (don't understand most of what is there).
I tried using different "Actions" in the editor, and also tried swapping the condition with the function again in the editor with the new code, but no luck.
Any more ideas? Thanks for your help as always.
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 05, 2023 7:39 pm
by Grondel
ChristianC wrote: ↑Mon Jun 05, 2023 7:36 pm
Hi! I replaced the original code with yours, but the scenario would not start. I put the code into rextester and it flagged "27: 'end' expected (to close 'if' at line 26) near 'break'"
probs need an additional end there.
sers,
Thomas
Re: Turn-based 'checkpoint' system
Posted: Wed Jun 07, 2023 12:13 am
by ChristianC
Grondel wrote: ↑Mon Jun 05, 2023 7:39 pm
probs need an additional end there.
sers,
Thomas
I tried these ones (additional "end") but the scenario continues to not open as before, and rextester reports the same error for them all (also the same error as previously): "lua:27: 'end' expected (to close 'if' at line 26) near 'break'" - line 26 here is the "if occupier == player then" line in the IsHexCaptured function.
Code: Select all
function IsHexCaptured(hex,player)
local hex = world:GetHex(hex) -- Get the hex at coordinates (x, y)
occupier = hex.owner
if occupier == player then
return true break end -- Hex is captured by the player
return false -- Hex is not captured by the player
end
end
Code: Select all
function IsHexCaptured(hex,player)
local hex = world:GetHex(hex) -- Get the hex at coordinates (x, y)
occupier = hex.owner
if occupier == player then
return true break end end -- Hex is captured by the player
return false -- Hex is not captured by the player
end
Code: Select all
if occupier == player then
return true break -- Hex is captured by the player
return false -- Hex is not captured by the player
end
end
But it seems to me that in the original code you provided the function and if statement are tied off properly with the "end" statements. No? (Notepad++ displays the brackets around them) Also, the CheckVictory function preceding it seems tied off..
Re: Turn-based 'checkpoint' system
Posted: Wed Jun 07, 2023 12:26 am
by Grondel
ChristianC wrote: ↑Wed Jun 07, 2023 12:13 am
Grondel wrote: ↑Mon Jun 05, 2023 7:39 pm
probs need an additional end there.
sers,
Thomas
I tried these ones (additional "end") but the scenario continues to not open as before, and rextester reports the same error for them all (also the same error as previously): "lua:27: 'end' expected (to close 'if' at line 26) near 'break'" - line 26 here is the "if occupier == player then" line in the IsHexCaptured function.
Code: Select all
function IsHexCaptured(hex,player)
local hex = world:GetHex(hex) -- Get the hex at coordinates (x, y)
occupier = hex.owner
if occupier == player then
return true break end -- Hex is captured by the player
return false -- Hex is not captured by the player
end
end
Code: Select all
function IsHexCaptured(hex,player)
local hex = world:GetHex(hex) -- Get the hex at coordinates (x, y)
occupier = hex.owner
if occupier == player then
return true break end end -- Hex is captured by the player
return false -- Hex is not captured by the player
end
Code: Select all
if occupier == player then
return true break -- Hex is captured by the player
return false -- Hex is not captured by the player
end
end
But it seems to me that in the original code you provided the function and if statement are tied off properly with the "end" statements. No? (Notepad++ displays the brackets around them) Also, the CheckVictory function preceding it seems tied off..
the missconception here is that a return is needed in both cases, which is not, therefor break is not needed here as well.
Code: Select all
function IsHexCaptured(hex,player)
local hex = world:GetHex(hex) -- Get the hex at coordinates (x, y)
occupier = hex.owner
if occupier == player then return true end -- Hex is captured by the player
end
if u really want it u could try this:
Code: Select all
function IsHexCaptured(hex,player)
local hex = world:GetHex(hex) -- Get the hex at coordinates (x, y)
occupier = hex.owner
if occupier == player then return true else return false end -- Hex is captured by the player
end
either in the base game or a scenario that came with the base game, i recall a function that counts victory hexes. that one might help u get along, but i can´t recall which it was.
sers,
Thomas
Re: Turn-based 'checkpoint' system
Posted: Wed Jun 07, 2023 1:18 am
by ChristianC
Grondel wrote: ↑Wed Jun 07, 2023 12:26 am
It's interesting, your codes cleared the error in rextester but the scenario still crashes.
So the "CheckVictory" function and "IsHexCaptured" functions you don't believe are sufficient on their own to achieve the desired result? I should incorporate the victory hex counting function you speak of along with these? or replace one of them?
I'm also thinking that when the scenario was not working in such a way last time (specifically, the game opening straight to main menu from the editor while I was working on the spawning units midgame trigger) when in the editor the wrong Condition/Function was set. When it was just the lua code that was wrong in that situation, the scenario opened but the red "script errors" appeared in game. For the current one, I switched the condition/function back and forth to test it, but it behaves the same way (going straight to main menu). Not sure if you gain any thoughts from that..
I will search for this function that counts victory hexes. And likely pop back in once I find it )
Re: Turn-based 'checkpoint' system
Posted: Wed Jun 07, 2023 2:02 am
by Grondel
ChristianC wrote: ↑Wed Jun 07, 2023 1:18 am
Not sure if you gain any thoughts from that..
This always happens when something in the LUA-script causes a syntax error.
It´s like a ( too many or missing, a missing end, could even be a simpel spacebar in the wrong place.
Compiling the script usually finds those issues.
sers,
Thomas
Re: Turn-based 'checkpoint' system
Posted: Wed Jun 07, 2023 2:05 am
by Grondel
Grondel wrote: ↑Sun Jun 04, 2023 9:27 pm
for i,_hex in ipairs(hexesToCapture) do
try this:
for _, i in ipairs(hexesToCapture) do
sers,
thomas
Re: Turn-based 'checkpoint' system
Posted: Sun Jun 18, 2023 7:00 pm
by ChristianC
Grondel wrote: ↑Wed Jun 07, 2023 2:05 am
Hi, I switched some things up and I believe I located the function you mentioned earlier - "CountCapturedVHs" in the 6th Army Escape scenario .lua - I do believe I got a useful line from it which I placed in the IsHexCaptured function I have listed below. The scenario launches now and no error is rextester, but it does not work.
This is where I'm at currently.
Code: Select all
function isturn5()
return world.round == 5
end
The game should only check on turn 5. I have "isturn5" in the trigger condition in the editor. No issues here it seems ^
Code: Select all
function CheckVictory()
local player = world:GetPlayer(0) -- on turn 5 the player to check is player "0" (Allies)
if player == 0 then
local hexesToCapture = { -- Specify the victory hexes to capture
{ x = 50, y = 32 },
{ x = 50, y = 24 },
{ x = 59, y = 12 }
}
local capturedHexes = 0
for _, i in ipairs(hexesToCapture) do
if IsHexCaptured(x, y, player) then
capturedHexes = capturedHexes + 1
end
end
if capturedHexes < 3 then
Defeat(0) --
end
end
end
This one should should check if player "0" (Allies) owns the three specified victory hexes.
A few questions regarding this function (entered as the "Function" within the Trigger Parameters on the editor)..
For the line "local player = world:GetPlayer(0)" - inputting "0" instead of "player," as in the SpawnWave function you helped me with in the other thread, got rid of the in-game error "null value" for this line, not sure why considering the SpawnWave function works just fine without the integer value, but I guess this is okay here?
For the segment "if capturedHexes < 3 then
Defeat(0)" - I got "Defeat" from the properties.txt file. It is written as "+ void Defeat(int)". So if player 0's captured hexes are less than 3, he loses.. Am I using this right? (I have not come across another piece of the properties.txt that seems more logical to enact a defeat than "Defeat")
Also, I input "for _, i in ipairs(hexesToCapture) do" to replace "for i,_hex in ipairs(hexesToCapture) do" as you mentioned earlier.
Code: Select all
function IsHexCaptured(x, y, player)
local hex = GetHex(x, y) -- Get the hex at coordinates (x, y)
if hex.owner == 0 then
return true -- Hex is captured by the player
end
end
This function contributes to the "CheckVictory" function above. It checks in particular if the certain hex, as specified in the "CheckVictory" function, is captured by player "0" (Allies). "hex.owner" - took this one from the "CountCapturedVHs" function. Seems pretty straight forward..
Any more ideas?
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 2:52 am
by ChristianC
I'm thinking there may be an easier way to do this - although it doesn't work, maybe it's close? - got the general outline from the multiplayer scenario lua's.
Code: Select all
CheckPoint1Vhs = {{50,32},{50,24},{59,12}} -- the victory hexes to capture by turn 5 to pass the 'checkpoint'
function isturn5() -- this is entered as the Condition in the editor to check on turn 5
return world.round == 5
end
function Player0VH() -- checks if player "0" (Allies) controls the specified hexes in "CheckPoint1Vhs"
local p0captured = 0
for _,vh in ipairs(CheckPoint1Vhs) do
local hex = world:GetHex(vh)
if hex.owner == 0 then
p0captured = p0captured + 1
end
end
return p0captured -- returns number of "CheckPoint1Vhs" player 0 captured
end
function Player0CustomFail(action) -- I intend for this to check if player "0" captured the three hexes or not and then returns a defeat to player "0" if not.
return action:GetActionClass() == "TurnsLimitAction" and Player0VH() < 3 -- I don't understand this "getactionclass" and "turnslimitaction" really, but just following the outline which I understand makes it work in the MP scenarios
end
Any idea what is wrong?
Btw, in the editor I have the "Actions" set to "EndTurnAction", "GetActionClass" is listed under that. (but why when I open an MP scenario with a similar lua in the editor there are no triggers listed for any of this?)
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 6:51 am
by Grondel
function CheckVictory()
local player = world:GetPlayer(0)
if player == 0 then <-- this is nonesense since player is always 0, u just grabed him one line above
local hexesToCapture = { -- Specify the victory hexes to capture
{ x = 50, y = 32 },
{ x = 50, y = 24 },
{ x = 59, y = 12 }
}
local capturedHexes = 0
for _,i in ipairs(hexesToCapture) do
if IsHexCaptured(x, y, player) then<---hexes are not adressed like that and has nothing to do with the hexes u want checked
capturedHexes = capturedHexes + 1
end
end
if capturedHexes < 3 then
Defeat(0) --
end
end
end
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 6:58 am
by Grondel
ChristianC wrote: ↑Mon Jun 19, 2023 2:52 am
Code: Select all
CheckPoint1Vhs = {{50,32},{50,24},{59,12}} -- the victory hexes to capture by turn 5 to pass the 'checkpoint'
function isturn5() -- this is entered as the Condition in the editor to check on turn 5
return world.round == 5
end
function Player0VH() -- checks if player "0" (Allies) controls the specified hexes in "CheckPoint1Vhs"
local p0captured = 0
for _,vh in ipairs(CheckPoint1Vhs) do
local hex = world:GetHex(vh)
if hex.owner == 0 then
p0captured = p0captured + 1
end
end
return p0captured -- returns number of "CheckPoint1Vhs" player 0 captured
end
function Player0CustomFail(action) -- I intend for this to check if player "0" captured the three hexes or not and then returns a defeat to player "0" if not.
return action:GetActionClass() == "TurnsLimitAction" and Player0VH() < 3 -- I don't understand this "getactionclass" and "turnslimitaction" really, but just following the outline which I understand makes it work in the MP scenarios
end
Any idea what is wrong?
this looks better in general. but has still some flaws.
return p0captured -- returns number of "CheckPoint1Vhs" player 0 captured
where do u return that to? what function is using "player0VH()" to check something? none i asume so no reason to "return" something to nirvana.
function Player0CustomFail(action)
this is halve the functions needed to create a custom objective in the editor, not sure why u want it here.
What u can do to make this work is a custom objective that triggers the loss on turn x if hexes are not owned.
sers,
thomas
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 5:59 pm
by ChristianC
Grondel wrote: ↑Mon Jun 19, 2023 6:58 am
return p0captured -- returns number of "CheckPoint1Vhs" player 0 captured
where do u return that to? what function is using "player0VH()" to check something? none i asume so no reason to "return" something to nirvana.
function Player0CustomFail(action) uses it
Code: Select all
function Player0CustomFail(action)
return action:GetActionClass() == "TurnsLimitAction" and Player0VH() < 3
end
Grondel wrote: ↑Mon Jun 19, 2023 6:58 am
function Player0CustomFail(action)
this is halve the functions needed to create a custom objective in the editor, not sure why u want it here.
What u can do to make this work is a custom objective that triggers the loss on turn x if hexes are not owned.
Do you mind to elaborate on this a bit more?
Why can the "custom objective" not be wrapped up in two of the functions already present?
function Player0VH() - returns the number of specified victory hexes (as given in CheckPoint1Vhs) that player 0 captured.
function Player0CustomFail(action) - check if the number returned by Player0VH() is less than 3, and if so assign defeat to player 0
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 6:05 pm
by ChristianC
ChristianC wrote: ↑Mon Jun 19, 2023 5:59 pm
Grondel wrote: ↑Mon Jun 19, 2023 6:58 am
what function is using "player0VH()" to check something? none i asume so no reason to "return" something to nirvana.
function Player0CustomFail(action) uses it
Code: Select all
function Player0CustomFail(action)
return action:GetActionClass() == "TurnsLimitAction" and Player0VH() < 3
end
Grondel wrote: ↑Mon Jun 19, 2023 6:58 am
function Player0CustomFail(action)
this is halve the functions needed to create a custom objective in the editor, not sure why u want it here.
What u can do to make this work is a custom objective that triggers the loss on turn x if hexes are not owned.
Do you mind to elaborate on this a bit more?
Why can the "custom objective" not be wrapped up in two of the functions already present?
function Player0VH() - returns the number of specified victory hexes (as given in CheckPoint1Vhs) that player 0 captured.
function Player0CustomFail(action) - check if the number returned by Player0VH() is less than 3, and if so assign defeat to player 0
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 6:06 pm
by ChristianC
Grondel wrote: ↑Mon Jun 19, 2023 6:58 am
what function is using "player0VH()" to check something? none i asume so no reason to "return" something to nirvana.
function Player0CustomFail(action) uses it
Code: Select all
function Player0CustomFail(action)
return action:GetActionClass() == "TurnsLimitAction" and Player0VH() < 3
end
Grondel wrote: ↑Mon Jun 19, 2023 6:58 am
function Player0CustomFail(action)
this is halve the functions needed to create a custom objective in the editor, not sure why u want it here.
What u can do to make this work is a custom objective that triggers the loss on turn x if hexes are not owned.
Do you mind to elaborate on this a bit more?
Why can the "custom objective" not be wrapped up in two of the functions already present?
function Player0VH() - returns the number of specified victory hexes (as given in CheckPoint1Vhs) that player 0 captured.
function Player0CustomFail(action) - check if the number returned by Player0VH() is less than 3, and if so assign defeat to player 0
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 6:24 pm
by Grondel
ChristianC wrote: ↑Mon Jun 19, 2023 6:06 pm
function Player0CustomFail(action) - check if the number returned by Player0VH() is less than 3, and if so assign defeat to player 0
u cannot "assign defeat" to a player. u need to take the detour via objectives.

in there u enter a success and a fail function.
Code: Select all
function Player1CustomSuccess(action)
return vh_counter > 3
end
function Player1CustomFail(action)
return action:GetActionClass() == "TurnsLimitAction" and vh_counter < 3
end
Those will be checked constantly and trigger as soon as the prerequisites are met.
Now u create a function to check them on turn 5 and following.
Code: Select all
vh_counter = 0
CheckPoint1Vhs = {{50,32},{50,24},{59,12}}
function Player0VH()
if world.round < 5 then return end
for _,vh in ipairs(CheckPoint1Vhs) do
local hex = world:GetHex(vh)
if hex.owner == 0 then
vh_counter = vh_counter + 1
end
end
end
make a trigger like this:
sers,
thomas
Re: Turn-based 'checkpoint' system
Posted: Mon Jun 19, 2023 10:44 pm
by ChristianC
Grondel wrote: ↑Mon Jun 19, 2023 6:24 pm
Thanks, I entered everything in (bottom of the post) but still not working - no in-game error messages and fine according to rextester.
Wondering about this code..
Code: Select all
function Player0CustomFail(action)
return action:GetActionClass() == "TurnsLimitAction" and vh_counter < 3
end
The "action:GetActionClass() == "whatever action is input" does not have to match the "Actions" as given in the editor? For example you show to check "NewRoundAction" in the "Actions" portion of the editor, but I see the "TurnsLimitAction" ("TurnsLimitAction", what is this referring to btw?) is entered into the code instead..
I tried messing around with this, entering "TurnsLimitAction" into the editor instead; and entering "NewRoundAction" into the "action:GetActionClass()" instead, but no success..
All the current things I have entered:
Code: Select all
vh_counter = 0
CheckPoint1Vhs = {{50,32},{50,24},{59,12}}
function Player0VH()
if world.round < 5 then return end
for _,vh in ipairs(CheckPoint1Vhs) do
local hex = world:GetHex(vh)
if hex.owner == 0 then
vh_counter = vh_counter + 1
end
end
end
function Player0CustomSuccess(action) -- this one doesn't need
return vh_counter > 3
end
function Player0CustomFail(action)
return action:GetActionClass() == "TurnsLimitAction" and vh_counter < 3
end

Re: Turn-based 'checkpoint' system
Posted: Tue Jun 20, 2023 5:14 am
by Grondel
ChristianC wrote: ↑Mon Jun 19, 2023 10:44 pm
Thanks, I entered everything in (bottom of the post) but still not working - no in-game error messages and fine according to rextester.
no u didn´t, count needs to be 0.
ChristianC wrote: ↑Mon Jun 19, 2023 10:44 pm
Wondering about this code..
Code: Select all
function Player0CustomFail(action)
return action:GetActionClass() == "TurnsLimitAction" and vh_counter < 3
end
this is the fail condition. this actionclass has nothing to do with the action u set in the editor.
this function checks at the end of the scenario"turns limit action" if vh are less than 3 and causes a loss. u need to set this to whatever u want the fail condition to be.
if i remember correct u want the player to loose if he hasn´t captured them on turn 5. fail condition needs to be then:
Code: Select all
function Player0CustomFail(action)
return world.round > 4 and vh_counter < 3
end
with this player will loose if at the start of turn 5 he has less than 3 of the vh.
sers,
thomas