I was looking through my threads trying to find something else. I forgot about this one and I have a solution so I may as well mention it while I'm here...
Code:
if I_EventCounter test_free_slots == 1
console_command create_unit Helms-Deep test_unit 1 0 0
campaign_wait 0.1
if I_NumberUnitsInSettlement Helms-Deep test_unit == 0
set_event_counter test_free_slots_ok 0
end_if
if I_NumberUnitsInSettlement Helms-Deep test_unit == 1
destroy_units milan test_unit
campaign_wait 0.1
set_event_counter test_free_slots_ok 1
end_if
end_if
if I_EventCounter test_free_slots == 2
console_command create_unit Helms-Deep test_unit 2 0 0
campaign_wait 0.1
if I_NumberUnitsInSettlement Helms-Deep test_unit == 0
set_event_counter test_free_slots_ok 0
end_if
if I_NumberUnitsInSettlement Helms-Deep test_unit == 1
destroy_units milan test_unit
campaign_wait 0.1
set_event_counter test_free_slots_ok 0
end_if
if I_NumberUnitsInSettlement Helms-Deep test_unit == 2
destroy_units milan test_unit
campaign_wait 0.1
set_event_counter test_free_slots_ok 1
end_if
end_if
This will test whether the "Helms-Deep" settlement has either one or two slots free: test_free_slots indicates whether to test for 1 or 2.
The first section spawns 1 unit of test_unit into the settlement, tests that there is indeed 1 unit in there and then removes it. If the "has 1 unit" test failed then there must have been no room for it: a full garrison already.
The second test is the same, spawning and testing for 2 units, but also handles the case where there was room for only 1 (removes that 1 unit).
Obviously the unit used for the testing should not be one that can be in the campaign for this faction, because destroy_units will wipe them all out. And it can't have spaces in the Type name as I_NumberUnitsInSettlement doesn't work with those.
I don't know if the campaign_waits are necessary. They are left over from something I was trying out when it wasn't working. The very next thing I was doing after this was spawning units into the settlement - that wasn't working and I thought that maybe the engine saw these test units as still there due to destroy_units not completed yet. So the waits gave it some breathing space between the destroys and the next spawns.
test_free_slots_ok is set to be 1 for yes and 0 for no which can then be used as a condition for whatever.
e.g.
Code:
if I_EventCounter test_free_slots == 2
and I_EventCounter test_free_slots_ok == 1
;there are two slots free: do what you have to do
end_if
Why bother? If you only need to spawn some units into there and don't really care whether there was room for them (and therefore were indeed spawned) then this script is unnecessary.
But if you need to know before you spawn anything 'real' into a settlement then this is one way to do it. And also for testing before trying to move a general into a settlement.
~~~~~~~~~~~~~~
I also found that create_unit can cause problems. If it is used on a settlement that is already full then subsequent create_unit calls on that same settlement can fail, even if, later, the settlement is no longer fully garrisoned. As though it has left the "garrison full" test logic in a broken state for the rest of the campaign.
However, "console_command create_unit" did not suffer from this problem. So I would suggest always using this one for spawning units into settlements.