Page 1 of 1

Why this unit never is able to move?

Posted: Fri Aug 30, 2019 11:23 am
by Kossatx
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

Re: Why this unit never is able to move?

Posted: Fri Aug 30, 2019 4:54 pm
by Robotron
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!

Re: Why this unit never is able to move?

Posted: Fri Aug 30, 2019 5:57 pm
by Kossatx
: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?

Re: Why this unit never is able to move?

Posted: Fri Aug 30, 2019 7:00 pm
by Robotron
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:

Re: Why this unit never is able to move?

Posted: Fri Aug 30, 2019 8:39 pm
by Kossatx
Great! :D