Withdrawal Event

A forum to discuss custom scenarios, campaigns and modding in general.

Moderators: Slitherine Core, The Lordz

Post Reply
Johnsnowblind
Private First Class - Wehrmacht Inf
Private First Class - Wehrmacht Inf
Posts: 7
Joined: Thu Jul 25, 2019 10:05 pm

Withdrawal Event

Post by Johnsnowblind » Tue Jul 30, 2019 4:31 pm

Hello everyone. I can't create an event I call the "Withdrawal Event." For example, let's say the city in question is Sarajevo. Suppose it has already been attacked by Serbian troops (as in the "Battle Event in Cities" post in this forum). After a few turns, let's say all Serbian troops withdraw at least two hexes from the city.
I created the following event:

--Sarajevo Withdrawal
function SarajevoWithdrawal ()
local austria = game: GetFactionById (3)
local sarajevo = game: GetHex (106, 40)
   if sarajevo.alliance.id == 2 and austria.alliance.id == 2 then
     if GetEvent ("SarajevoBattle")> 0 then
       if GetEvent ("SarajevoWithdrawal") == 0 then
local hexes = game.map:GetHexesInRange(sarajevo, 2)
for _, hex in pairs (hexes) of
            if hex.unit ~ = nil and hex.unit.alliance.id == 2 then
             SetEvent ("SarajevoWithdrawal", game.turn)
break
            end
         end
        end
     end
   end
end

The "SarajevoBattle" event works very well. But as much as Serbian troops are more than two hexes apart, the "SarajevoWithdrawal" event does not appear during the game. What would be wrong? :roll:

Robotron
Lieutenant Colonel - Panther D
Lieutenant Colonel - Panther D
Posts: 1247
Joined: Tue Nov 23, 2010 3:35 pm

Re: Withdrawal Event

Post by Robotron » Wed Jul 31, 2019 7:39 am

1. What you want to do:

trigger the event if no Serbian troops are within 2 hexes of Belgrade

2. What the event does:

trigger the event as soon as one or more CP unit is within 2 hexes of Belgrade

3. how the check part of the event can be improved:

Code: Select all

local unitcheck = 0
local hexes = game.map:GetHexesInRange(sarajevo, 2)
for _, hex in pairs (hexes) of
            if hex.unit ~= nil and hex.unit.faction.id == 7 then
      		unitcheck = 1	
		break
            end
end
if unitcheck == 0 then
	SetEvent ("SarajevoWithdrawal", game.turn)	
end
4. so the whole event looks like:

Code: Select all

function SarajevoWithdrawal ()
local austria = game: GetFactionById (3)
local sarajevo = game: GetHex (106, 40)
   if sarajevo.alliance.id == 2 and austria.alliance.id == 2 then
     if GetEvent ("SarajevoBattle")> 0 then
       if GetEvent ("SarajevoWithdrawal") == 0 then
		local unitcheck = 0
		local hexes = game.map:GetHexesInRange(sarajevo, 2)
		for _, hex in pairs (hexes) of
            		if hex.unit ~= nil and hex.unit.faction.id == 7 then
      				unitcheck = 1	
					break
            		end
		end
		if unitcheck == 0 then
			SetEvent ("SarajevoWithdrawal", game.turn)	
		end
        end
     end
end
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Johnsnowblind
Private First Class - Wehrmacht Inf
Private First Class - Wehrmacht Inf
Posts: 7
Joined: Thu Jul 25, 2019 10:05 pm

Re: Withdrawal Event

Post by Johnsnowblind » Thu Aug 01, 2019 8:55 pm

Thanks for your help, Robotron. But by adding this event to the "events.lua" file, unfortunately the event does not happen. :cry:

Robotron
Lieutenant Colonel - Panther D
Lieutenant Colonel - Panther D
Posts: 1247
Joined: Tue Nov 23, 2010 3:35 pm

Re: Withdrawal Event

Post by Robotron » Fri Aug 02, 2019 9:13 am

Of course you inserted the function call into TriggerTurnevents(), right?
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Johnsnowblind
Private First Class - Wehrmacht Inf
Private First Class - Wehrmacht Inf
Posts: 7
Joined: Thu Jul 25, 2019 10:05 pm

Re: Withdrawal Event

Post by Johnsnowblind » Fri Aug 02, 2019 5:30 pm

Yes. I also tested for "TriggerTurnAfterEvents()". But now I have identified the possible error I was commenting on: when I put the command "GetHexesInRange (sarajevo, 2)", I expected Serbian troops to be at least two hexes from the city. In this case, the event did not happen. I changed it to (sarajevo, 1), and then when the troops were more than two hexes apart, the event would happen.

Robotron
Lieutenant Colonel - Panther D
Lieutenant Colonel - Panther D
Posts: 1247
Joined: Tue Nov 23, 2010 3:35 pm

Re: Withdrawal Event

Post by Robotron » Fri Aug 02, 2019 9:27 pm

To be honest, I'm confused why that worked for you.

Be aware that as long as you are using the ancient V1.5.1 I might run into trouble trying to find out why certain things are not working right because of stuff having changed between versions.

Please update your game (if possible) to the last official version 1.6.6 as this is the last official stable release and the version I'll be referring to when trying to find bugs.
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Johnsnowblind
Private First Class - Wehrmacht Inf
Private First Class - Wehrmacht Inf
Posts: 7
Joined: Thu Jul 25, 2019 10:05 pm

Re: Withdrawal Event

Post by Johnsnowblind » Sat Aug 03, 2019 5:52 pm

Thanks a lot, Robotron. I will do what you said.
By the way, what situations do you indicate for using this "check" command that you suggested to use in this post? In the version I use of the game, I had not seen it in any event.

Robotron
Lieutenant Colonel - Panther D
Lieutenant Colonel - Panther D
Posts: 1247
Joined: Tue Nov 23, 2010 3:35 pm

Re: Withdrawal Event

Post by Robotron » Sat Aug 03, 2019 8:53 pm

If you mean "unitcheck" that's not a command but a local variable which in our case will be either 0 or 1 and is used to decide whether the desired conditions have been met.

Local variables are variables that are only operating within the bounds of a specific function in contrast to global variables which will be checked and used by ALL functions.

This is useful so you can be sure there is no "intereference" from other functions that use variables of the same name and is generally regarded good scripting practice.

In my mod I use several hundred local variables with generic names like "test", "check" or "value" and because they are all within their own functions (for example: "local test = 0") there is no danger of accidently reading or writing the wrong values into the wrong variable just because it has the same name.

Else you would be forced to book-keep a huge list of variables each with their own specific names to make sure nothing goes wrong which is a nightmare.

In our case the condition to trigger the event was when no more Serb units were left within 2 hexes of Belgrade, if I understood you right.

If there were Serbs in vicinity the value would be set to 1, preventing the event to trigger.

If there were no Serbs in vicinity the value would be set to 0, triggering the event.
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Post Reply

Return to “Commander the Great War : Mods & Scenario Design”