Battle Event in Cities

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

Battle Event in Cities

Post by Johnsnowblind » Thu Jul 25, 2019 10:41 pm

Hello everyone. Can anyone help me with creating a battle event related to a city? I want to create an event by mentioning that a particular city was first attacked by some enemy unit. Suppose, for example, that at the beginning of the war the city of Metz, in Germany, is attacked by the French.
I wrote the following code in the game_events.lua file:

--Metz Battle
function MetzBattle(attacker, defender)
if GetEvent("MetzBattle") == 0 then
if (attacker.prototype.name == "infantry" or attacker.prototype.name == "cavalry" or attacker.prototype.name == "garrison" or attacker.prototype.name == "armour") and
defender.hex.x == 89 and defender.hex.y == 29 then
SetEvent("MetzBattle", game.turn)
end
end
end

I also included this event in function TriggerCombatEvents(attacker, defender, hex)
But it did not work. The game crashed. :cry:
Where would I be going wrong?
Obs: I'm not a expert in .lua files, and I use version 1.5.1 of this game.
And sorry for my english. :oops:
Thanks!

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

Re: Battle Event in Cities

Post by Robotron » Fri Jul 26, 2019 8:43 am

According to the crash log, the AI attacks a defending unit without a hex (maybe because the unit is already destroyed), so you must include a check in your event to make sure that the defender's hex is not nil:

Code: Select all

and defender.hex ~= nil
So the whole event looks like:

Code: Select all

function MetzBattle(attacker, defender)
  if GetEvent("MetzBattle") == 0 then
	if (attacker.prototype.name == "infantry" 
		or attacker.prototype.name == "cavalry" 
		or attacker.prototype.name == "garrison" 
		or attacker.prototype.name == "armour") 
	
		and defender.hex ~= nil
		and defender.hex.x == 89 
		and defender.hex.y == 29 then
			SetEvent("MetzBattle", game.turn)
	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: Battle Event in Cities

Post by Johnsnowblind » Sat Jul 27, 2019 12:50 am

Yes, exactly ! Now it worked perfectly.
Thank you so much, Robotron! I have a few more ideas here, surely I will need some help. Or using a typical Brazilian expression: "Anything I give a scream (for help)." :lol:
Thank you again. :)

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

Re: Battle Event in Cities

Post by Robotron » Sat Jul 27, 2019 10:00 pm

Feel free to ask me anything about modding the game. But be careful: I might steal your ideas and put them into my PotzBlitz mod if they are good enough. ;)
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: Battle Event in Cities

Post by Johnsnowblind » Mon Jul 29, 2019 2:04 am

No problem for me. :lol:
The modifications I am implementing in the game are very simple compared to the modifications made in PotzBlitz. They may not be of interest to you, but if you like, feel free to use them. :D

Post Reply

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