More than 200 variables?

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

Moderators: Slitherine Core, The Lordz

Post Reply
Kossatx
Sergeant - 7.5 cm FK 16 nA
Sergeant - 7.5 cm FK 16 nA
Posts: 241
Joined: Wed Nov 27, 2013 3:27 pm

More than 200 variables?

Post by Kossatx »

Hi! I've included many scripts like this one in game:supply.lua > function SetUnitMovePenalty(unit):
local koenigsberg = game:GetHex(112, 17)
if koenigsberg.unit ~= nil
and unit.hex ~= nil
and unit.hex.x == 112
and unit.hex.y == 17
and unit.prototype.name == "garrison" then
unit.mp = 0
end
And now this error appears:
[15:34:35][1104][C]:-1(global dofile) std::exception: 'Lua Error:game/game_supply.lua:1958: function at line 94 has more than 200 local variables'
Any idea about how to include more variables without breaking the function? Is it possible to clone the same function with no more than 200 variables each one? I don't know how to do it :( Robotron, are you on holidays?
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: More than 200 variables?

Post by Robotron »

You cannot have more than 200 "local" variables, like for example "local koenigsberg", in the same function.

You could clone it under a different name, that's one way, but still quite terrible in my opinion.

I believe you want to set 0 movement to quite a lot of german units, right?

There are better and much easier ways to achieve this, for example:

Code: Select all

for unit in germany.units do
	if unit.type == Unit.LAND 
	 	and unit.hex ~= nil
	 	and unit.hex.originalFaction.id == 2
	 	and unit.hex.construction ~= nil then
	 	unit.mp = 0
	 end	
end	 		
This will set movement to 0 to:
ALL german units
that are land units
that are not in the production queue but already on the map
that are on a hex originally belonging to germany
that are on a hex with a construction (~= nil means "not nothing").


Constructions come in different types, for example:

Code: Select all

Construction.TYPE_CAPITAL
Construction.TYPE_CITY
Construction.TYPE_FORTRESS
There is also

Code: Select all

Construction.TYPE_PORT
which is the actual port hex on an ocean hex, NOT a city with a port!

So for example if you would want to set 0 movement to all german units in all cities and fortresses but not capitals you could use:

Code: Select all

for unit in germany.units do
	if unit.hex ~= nil
	 	and unit.type == Unit.LAND 
	 	and unit.hex.construction ~= nil
                and (unit.hex.construction.type == Construction.TYPE_CITY
	 	       or unit.hex.construction.type == Construction.TYPE_FORTRESS) then
                         unit.mp = 0
	 end	
end
If that's not want you need I need more details about what you are trying to achieve, but please be specific.
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Kossatx
Sergeant - 7.5 cm FK 16 nA
Sergeant - 7.5 cm FK 16 nA
Posts: 241
Joined: Wed Nov 27, 2013 3:27 pm

Re: More than 200 variables?

Post by Kossatx »

Thanks again Robotron, althought I want to immobilize more than units in capitals and fortresses, your script CAPITAL/FORTRESS will be very useful. If I want to use this script but not affecting Lisbon (capital), this script is written ok?
if unit.hex ~= nil
and unit.type == Unit.LAND
and unit.hex.construction ~= nil
and unit.faction.id ~= 16
and (unit.hex.construction.type == Construction.TYPE_CAPITAL
or unit.hex.construction.type == Construction.TYPE_FORTRESS) then
unit.mp = 0
end
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: More than 200 variables?

Post by Robotron »

This is wrong:

Code: Select all

and unit.faction.id ~= 16
It means do NOT include units from faction with ID of 16

This is correct:

Code: Select all

and unit.faction.id == 16
If you want to exclude the capital then you could also write:

Code: Select all

if unit.hex ~= nil
   and unit.type == Unit.LAND
   and unit.faction.id == 16
   and unit.hex.construction ~= nil
   and unit.hex.construction.type ~= Construction.TYPE_CAPITAL then
      unit.mp = 0
end 
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Kossatx
Sergeant - 7.5 cm FK 16 nA
Sergeant - 7.5 cm FK 16 nA
Posts: 241
Joined: Wed Nov 27, 2013 3:27 pm

Re: More than 200 variables?

Post by Kossatx »

I don't understand why "and unit.faction.id ~= 16" is wrong. I don't want the script affects units in Lisbon, so isn't correct?
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: More than 200 variables?

Post by Robotron »

Portugal has faction.id == 16 so all units from Portugal have unit.faction.id == 16.
"unit.faction.id ~= 16" would exclude all portuguese units.

Remember ~= means "is not"

You want to include all units from Portugal but DO NOT WANT the unit on the capital.
That's why you need to use == in our example for the units and ~= for the construction type so that only the unit on the capital is excluded/not affected.
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Kossatx
Sergeant - 7.5 cm FK 16 nA
Sergeant - 7.5 cm FK 16 nA
Posts: 241
Joined: Wed Nov 27, 2013 3:27 pm

Re: More than 200 variables?

Post by Kossatx »

Ok Robotron, thanks for your help. You are a great teacher!
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: More than 200 variables?

Post by Robotron »

Thank you very much! :D

Why exactly do you want to keep the portuguese unit in Lisbon free to move while all other units are set to 0?
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Kossatx
Sergeant - 7.5 cm FK 16 nA
Sergeant - 7.5 cm FK 16 nA
Posts: 241
Joined: Wed Nov 27, 2013 3:27 pm

Re: More than 200 variables?

Post by Kossatx »

Robotron wrote: Fri Aug 14, 2020 10:39 pm Thank you very much! :D

Why exactly do you want to keep the portuguese unit in Lisbon free to move while all other units are set to 0?
Really I don't want to block all portuguese units, only most of the beginning order of battle to defend frontier with Spain, and I do the same with most nations. For example, while Romania stays neutral, bulgarian units in Nis and Sjopje are blocked. The problem with Lisbon is it's the only portuguese port, so if the unit in Lisbon is blocked Portugal couldn't embark troops to western front :wink:
Post Reply

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