SLIC-Reference : CreateUnit
CTP2 Bureau
The Modding
Knowledgebase
WePlayCiv: CtP2 Downloads
WePlayCiv: Call To Power forum
Apolyton Call To Power forums
Civilization Fanatics: Call To Power

SLIC-Reference : : CreateUnit  

Arguments: owner, type, nearlocation, distance[, saveunit] Type: void

Create a unit of the given type belonging to owner approximately distance tiles from nearlocation

If a unit variable is passed in as saveunit, it will contain the newly created unit after this function returns. Example:
//I want to put (cheat) a defender in my city as soon as I build it

HandleEvent(CreateCity) post
{

    CreateUnit(player[0], UnitDB(UNIT_HOPLITE), city[0].location, 0);

}

tested   working  

Source:

SFN_ERROR Slic_CreateUnit::Call(SlicArgList *args)
{
	if (args->Count() < 4 || args->Count() > 5) {
		return SFN_ERROR_NUM_ARGS;
	}
	sint32 owner;
	if(!args->GetPlayer(0, owner)) {
		return SFN_ERROR_TYPE_ARGS;
	}

	sint32 type;
	if(!args->GetInt(1, type)) {
		return SFN_ERROR_TYPE_ARGS;
	}

	MapPoint pos;
	BOOL res = args->GetPos(2, pos);
	if(!res)
		return SFN_ERROR_TYPE_ARGS;

	sint32 distance;
	if(!args->GetInt(3, distance)) {
		return SFN_ERROR_TYPE_ARGS;
	}

	const UnitRecord *rec = g_theUnitDB->Get(type, g_player[owner]->GetGovernmentType());
	// Check if the function was called with a valid unit type.
	if(!rec) return SFN_ERROR_UNKNOWN_UNIT_TYPE;

	sint32 x, y;
	BOOL found = FALSE;
	DynamicArray legalPoints;
	for(x = 0; x < g_theWorld->GetXWidth(); x++) {
		for(y = 0; y < g_theWorld->GetYHeight(); y++) {
			MapPoint chk(x, y);
			sint32 dist = pos.NormalizedDistance(chk);
			Cell *cell = g_theWorld->GetCell(chk);
			if(dist == distance && 
			   g_theWorld->CanEnter(chk, rec->GetMovementType()) && 
			   (!cell->UnitArmy() ||
				(cell->UnitArmy()->GetOwner() == owner &&
				 cell->UnitArmy()->Num() < k_MAX_ARMY_SIZE)) &&
			   (cell->GetCity().m_id == (0) ||
				cell->GetCity().GetOwner() == owner)) {
				legalPoints.Insert(chk);
				found = TRUE;
			}
		}
	}

	if(found) {
		MapPoint upos;
		upos = legalPoints[g_rand->Next(legalPoints.Num())];
		Unit unit;
		if(g_player[owner]) {
			unit = g_player[owner]->CreateUnit(type, upos,
													Unit(),
													FALSE,
													CAUSE_NEW_ARMY_SCENARIO);
		}
		if(unit.m_id == 0) {
			m_result.m_int = 0;
		} else {
			m_result.m_int = 1;
			if (args->Count() > 4) {
				if(args->m_argValue[4].m_type == SA_TYPE_INT_VAR) {
					SlicSymbolData *sym = args->m_argValue[4].m_symbol;
					sym->SetUnit(unit);
					
				}
			}
		}
	} else {
		m_result.m_int = 0;
	}
	return SFN_ERROR_OK;
}

Information last updated by BureauBert on 23.11.2010

User Comments:

Using this form you may add your own observations concerning the above item, e.g. additional hints for usage, limitations, oddities etc. Please note this is not the place for asking questions or carrying out discussions about modding - please refer to the Apolyton CTP2 Modification Forum on that purpose.

Subject / Title*:
Text:*
(A maximum of characters left)
Your name:*
Your website (URL):

*) Required fields

No user comments yet.

  Administration
This site is currently maintained by: BureauBert (Webmaster)  Maquiladora (Manager)  Legal Notice: Legal Notice Statistics: 69667 unique users since 02.05.2004 (» Site Statistics) Hosted by: WebhostOne
Share this page:  VK Facebook Twitter LiveJournal OK MR Google+ LinkedIn tumblr Pinterest Blogger Digg Evernote