Page 1 of 1

Battle Event in Cities

Posted: Thu Jul 25, 2019 10:41 pm
by Johnsnowblind
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!

Re: Battle Event in Cities

Posted: Fri Jul 26, 2019 8:43 am
by Robotron
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

Re: Battle Event in Cities

Posted: Sat Jul 27, 2019 12:50 am
by Johnsnowblind
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. :)

Re: Battle Event in Cities

Posted: Sat Jul 27, 2019 10:00 pm
by Robotron
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. ;)

Re: Battle Event in Cities

Posted: Mon Jul 29, 2019 2:04 am
by Johnsnowblind
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