Why this unit never is able to move?

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

Moderators: Slitherine Core, The Lordz

Post Reply
Kossatx
Administrative Corporal - SdKfz 251/1
Administrative Corporal - SdKfz  251/1
Posts: 144
Joined: Wed Nov 27, 2013 3:27 pm

Why this unit never is able to move?

Post by Kossatx » Fri Aug 30, 2019 11:23 am

Hi, I have done this script in order to immobilize the ottoman unit in Basra if any other non CP unit is in a range of 2 hex from Basra. But somethig is wrong, because the ottoman unit never is able to move :cry: Any idea about it?

Code: Select all

local basra = game:GetHex(157, 68)
local hexes = game.map:GetHexesInRange(basra, 2)
for _, hex in pairs(hexes) do
if hex.unit == nil
	and unit.faction ~= 2
	and basra.unit ~= nil
	and unit.hex ~= nil
	and unit.hex.x == 157
	and unit.hex.y == 68
	and unit.type == Unit.LAND
	and unit.faction ~= 2 then
		unit.mp = 0
end
end

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

Re: Why this unit never is able to move?

Post by Robotron » Fri Aug 30, 2019 4:54 pm

Oh dear...your script is severely messed up. :mrgreen:

Important: you cannot just set a unit.mp to 0 via events, this will not work but must be done in:

function SetUnitMovePenalty(unit)

which is in:

game_supply.lua

For the effect you described I would try add this to the end of function SetUnitMovePenalty(unit)

Code: Select all

local basra = game:GetHex(157, 68)
local hexes = game.map:GetHexesInRange(basra, 2)

for _, hex in pairs(hexes) do
if hex.unit ~= nil
	and hex.unit.alliance.id == 1 then
		if basra.unit ~= nil	 
			and and basra.unit.type == Unit.LAND
			and basra.unit.alliance.id = 2 then
				basra.unit.mp = 0
				break
		end
end	
end

By the way: the method GetHexesInRange(target, radius) always checks the center hex (in this case basra) too!
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Kossatx
Administrative Corporal - SdKfz 251/1
Administrative Corporal - SdKfz  251/1
Posts: 144
Joined: Wed Nov 27, 2013 3:27 pm

Re: Why this unit never is able to move?

Post by Kossatx » Fri Aug 30, 2019 5:57 pm

:lol: Thanks a lot Robotron. I already wrote the script in the right function and file, but it was wrong written as you say :mrgreen: Other question, what's the "break" command meaning?

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

Re: Why this unit never is able to move?

Post by Robotron » Fri Aug 30, 2019 7:00 pm

break is used to exit a loop before the loop is finished.

in our case we have a "for" loop and the break is not really needed but I hoped you would ask so I could show you this link here and you learn something new

https://www.tutorialspoint.com/lua/lua_ ... tement.htm

:wink:
Slitherine's Commander the Great War on steroids: POTZBLITZ mod!
http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610

Kossatx
Administrative Corporal - SdKfz 251/1
Administrative Corporal - SdKfz  251/1
Posts: 144
Joined: Wed Nov 27, 2013 3:27 pm

Re: Why this unit never is able to move?

Post by Kossatx » Fri Aug 30, 2019 8:39 pm

Great! :D

Post Reply

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