[LUA] Random Number

You can get a random number from the Map.Rand(maximum) function. Since I assume you're working on your spawning three units when a building is constructed:

Code:
local random = Map.Rand(3) + 1

The local variable 'random' now contains a random number where 0 < random <= 3. Then you can loop that many times and spawn the unit within the loop. (May need to call JumpToNearestValidPlot or whatever that function's called to move them to reasonable nearby locations if they all spawn in the same place.)
 
So, where exactly do I put the...

Code:
local random = Map.Rand(3) + 1

...in here:
Code:
[...]	local pCity = pPlayer:GetCapitalCity();
for pCity in pPlayer:Cities() do

	for building in GameInfo.Buildings() do
	strName = GameInfo.Buildings[building.ID].Description
	strCityName = pCity:GetName()
	if pCity:IsHasBuilding(building.ID) then
		if load( pPlayer, building.ID .. "and" .. iPlayer) ~= true then
			strBuildingClass = GameInfo.Buildings[building.ID].BuildingClass
			if GameInfo.BuildingClasses[strBuildingClass].MaxPlayerInstances > (-1)then
				save( pPlayer, building.ID .. "and" .. iPlayer, true );
			else if GameInfo.BuildingClasses[strBuildingClass].MaxGlobalInstances > (-1) then
				save( pPlayer, building.ID .. "and" .. iPlayer, true );
				iBuildingCost = GameInfo.Buildings[building.ID].Cost
					local iUnitID = GameInfo.Units.UNIT_WORKER.ID
					pPlayer:InitUnit (iUnitID, pCity:GetX(), pCity:GetY() );
					else
					end
					end
				end
				end
		end
	end
end
 
You've got 3 different posts asking the same question. Admittedly, none were answered. In case you haven't gotten the answer yet:

First, you've got some bad indenting causing you to put too many "end"s in there.

So, starting from line 12:
Code:
			else if GameInfo.BuildingClasses[strBuildingClass].MaxGlobalInstances > (-1) then
				save( pPlayer, building.ID .. "and" .. iPlayer, true )
				iBuildingCost = GameInfo.Buildings[building.ID].Cost
				local iUnitID = GameInfo.Units.UNIT_WORKER.ID
				local random = Map.Rand(3) + 1
				for i=1, random do
					unit = pPlayer:InitUnit (iUnitID, pCity:GetX(), pCity:GetY() )
					unit:JumpToNearestValidPlot()
				end
			end
		end
	end
end
 
I'm sorry for the extra posts. After I posted the third I realize it wasn't necessary. Since then I refrained from posting such requests and I'm trying them myself.

Really appreciate the code, will test it as soon as possible. Thank you.
About the indenting: I have more code before, that's the reason for the '[...]'; I'll try to be more clear in any eventual question regarding codes.
 
Back
Top Bottom