Spawning Units...

Joined
Sep 28, 2008
Messages
402
Location
United States
So I want to spawn a unit at a certain time on a certain plot.

This is the code I have:

Code:
function SpawnUnits(playerID)
	Dprint ("-------------------------------------")
	Dprint ("Spawning units!")
	--local player = Players[playerID]
	local SpawnPlot = GetPlot(80,40) -- Plot Spawned On
	local SpawnYear = Game.GetGameTurnYear()
	local WARRIOR =	GameInfo.Units.UNIT_WARRIOR.ID
	local BARBARIAN	= GameInfo.Civilizations.CIVILIZATION_BARBARIAN.ID
	local iBarbarian = GetPlayerIDFromCivID(BARBARIAN, true)
	if (SpawnYear==-1500) then --and not Players[iBarbarian]:IsHuman() then  -- Spawn Unit on this year (and not human)
			Dprint ("Attempting to Spawn Barbarian Warrior!")
			iBarbarian:InitUnit(WARRIOR, SpawnPlot.X, SpawnPlot.Y)
	end
end

Ok so it is picking up the correct year (I know this is correct) but it has an error indexing iBarbarian afterward which I guess I just don't get.

So my main question is how do I get it too spawn a unit?

Then my second question is how do I get it too pick up if a player is human or not? (I have it 'commented out' but the current code right there doesn't work...


EDIT: nvm I figured it out I guess I posted this too soon:) but If anyone has a question on how to do this I can post what I did.
 
local iBarbarian = GetPlayerIDFromCivID(BARBARIAN, true)

On the assumption your using the same code I am from Gedemon, then this flag indicates a minor civ. Barbarian is a major civ as per your previous line indexing ID from Civilizations table as opposed to MinorCivilizations.

Try changing to false and try that.

EDIT: Second part of your post...player:IsHuman() should work based on what you have defined for player.
 
On the assumption your using the same code I am from Gedemon, then this flag indicates a minor civ. Barbarian is a major civ as per your previous line indexing ID from Civilizations table as opposed to MinorCivilizations.

Try changing to false and try that.

EDIT: Second part of your post...player:IsHuman() should work based on what you have defined for player.

Yep, I was having trouble with the idea that there are player ID's, I think im starting to learn this stuff. Thanks:)


So I have another question (for anyone to answer). So I'm spawning a settler but I want it to found by itself because the AI likes to move it and found a city in stupid places:). I'm not sure how the pushmission thing works but this function I made just wasn't working for me... (it of course spawns the unit but the unit doesn't found a city like I want it too).

Code:
function SpawnSettleTestChina()
	Dprint ("-------------------------------------")
	Dprint ("Spawning units!")
	--local player = Players[playerID]
	local SpawnYear = Game.GetGameTurnYear()
	local SETTLER =	GameInfo.Units.UNIT_UNMOVEABLE_SETTLER.ID
	local CHINA	= GameInfo.Civilizations.CIVILIZATION_CHINA.ID
	local playerID = GetPlayerIDFromCivID(CHINA, false, true)
	local player = Players[playerID]
	if (SpawnYear==-1500) and not player:IsHuman() then  -- Spawn Unit on this year (and not human)
			Dprint ("Attempting to Spawn Test Chinese Settler!")
			unit = player:InitUnit(SETTLER, 81, 40)
			local unitPlot = unit:GetPlot()
			unit:PopMission()
			unit:PushMission(MissionTypes.MISSION_FOUND, 81, 40, 0, 0, 1, MissionTypes.MISSION_FOUND, unitPlot, unit)
	end
end

So my question is what is wrong with this? My second question is would it be easier to create it's own function since I want all units of the unmovable_settler type to found a city the turn they spawn anyway. But I couldn't find a way to extract a unit type's ID and I'm not sure how I would set it up anyway.

But appreciate any insight people can give.
 
If you're spawning a settler to immediately found a city, why not just found the city directly?

pPlayer:Found(iPlotX, iPlotY)
 
Back
Top Bottom