Random number of units

bane_

Howardianism High-Priest
Joined
Nov 27, 2013
Messages
1,559
Ok, I have a code for when I build a Wonder a Worker appears at the Capital.
What changes do I have to make in order for the Unit be a random unit from a small group and a random number of 1-3?

Also, what should I change the 'pPlayer:GetCapitalCity' to in order to make the random unit(s) appear at the city of the Wonder instead of the Capital?

Here is the important part (I got it working from another thread):
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
 
Actually, you shouldn't need the "pPlayer:GetCapitalCity" code, since your loop should be checking each of your cities for the corresponding wonder. In my Human Village code, I commented it out (by putting a -- before it). The InitUnit code should spawn the Worker in the city the building/Wonder was constructed in... I wonder if the GetCapitalCity code is confusing it?

Try commenting out that first line (or just delete it) and see if it helps.
 
No no, the code works fine.
I want to change it so it actually spawns a random number of units (1-3). The way it is now, it spawns 1 Worker.

To spicy if further, I want to make it choose from a list (also at random), but I'm not sure if that's feasible.

The first part I imagine to be made in some sort of loop, like it rolls a random number than verifies it with elses until it reaches the right number, or something like that, but I don't know how to actually code it. :(
The second part, the part where it chooses the unit at random from a pre-determined list, instead of the Worker, I have no idea if it is even possible.
 
Back
Top Bottom