LUA - Generating Random Unit

PatRoy

Chieftain
Joined
Jul 21, 2014
Messages
30
I am trying to add in a Lua script where a player selects a unit "Draft A Unit" for production, and it produces one of three units at random. It is my first time trying to add in any Lua and have been using some references on these boards (http://forums.civfanatics.com/showthread.php?t=526843). I have the code below, while changing a couple of keywords. How do I have this code work for all Civilizations and not just a specified one? Would I just delete the "if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_CANADIENS"] then" and have it go into the next if statement "local unit = Players[playerID]:GetUnitByID(unitID)"?



Spoiler :
function Unit_Draft(playerID, unitID, hexVec, unitType, cultureType, civID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)
local player = Players[playerID]
local unit = player:GetUnitByID(unitID)
if(player == nil or
unit == nil or
unit:IsDead()) then
return
end

--------------
-- Choose Random Drafted Unit
--------------
if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_CANADIENS"] then
local unit = Players[playerID]:GetUnitByID(unitID)
if unit:GetUnitType() == GameInfoTypes["UNIT_DRAFT_A_UNIT"] then
local spawn = { "UNIT_SNIPER", "UNIT_PLAYMAKER", "UNIT_GRINDER" }
local unitRnd = player:InitUnit(GameInfo.Units[spawn[math.random(#spawn)]].ID, unit:GetX(), unit:GetY())
unitRnd:Convert(unit)
end
end
end
Events.SerialEventUnitCreated.Add(Unit_Draft)
 
I would probably change it to this, but then again I don't know a ton about lua...
Code:
function Unit_Draft(playerID, unitID, hexVec, unitType, cultureType, civID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)
	local player = Players[playerID]
	local unit = player:GetUnitByID(unitID)
	if(player == nil or
	unit == nil or
	unit:IsDead()) then
		return
	end


--------------
-- Choose Random Drafted Unit
--------------
	if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_CANADIENS"] then
		local unit = Players[playerID]:GetUnitByID(unitID)
		if unit:GetUnitType() == GameInfoTypes["UNIT_DRAFT_A_UNIT"] then
			[B][COLOR="Blue"]local spawn = {}
				spawn[1] = GameInfoTypes.UNIT_SNIPER
				spawn[2] = GameInfoTypes.UNIT_PLAYMAKER
				spawn[3] = GameInfoTypes.UNIT_GRINDER
			local unitRnd = player:InitUnit(spawn[math.random(1,3)], unit:GetX(), unit:GetY())[/COLOR][/B]
			unitRnd:Convert(unit)
		end
	end
end
Events.SerialEventUnitCreated.Add(Unit_Draft)
BTW wrapping your code in
Code:
 blocks rather than [SPOILER] blocks will make the indents show up; it's generally much more readable that way.
 
I'm assuming you grabbed this code from the thread Civitar (I think) started called something like "Help A Inexperienced Necromancer..." ? And that you took the code shown farthest down within that thread (ie, less likely to have had goofs that were shown and corrected later on in that same thread) ? If so, then you should have otherwise solid code to start with as a template except that it limits the random unit thing to a single civilization.*

To answer on-point to your question, see the addition of the "--" (the blue stuff) in two lines ? These two characters turn everything that follows on those lines into comments. I've shown the rest of those lines in red. The red part is what would be turned into a comment within lua. In order to allow the lua function to work for all civilizations you would indeed need to get rid of the line you were thinking of, but you also need to get rid of it's associated "end" line.

Code:
function Unit_Draft(playerID, unitID, hexVec, unitType, cultureType, civID, primaryColor, secondaryColor, unitFlagIndex, fogState, selected, military, notInvisible)
	local player = Players[playerID]
	local unit = player:GetUnitByID(unitID)
	if(player == nil or
		unit == nil or
		unit:IsDead()) then
		return
	end

	--------------
	-- Choose Random Drafted Unit
	--------------
	[COLOR="Blue"]--[/COLOR][COLOR="Red"]if player:GetCivilizationType() == GameInfoTypes["CIVILIZATION_CANADIENS"] then[/COLOR]
		local unit = Players[playerID]:GetUnitByID(unitID)
		if unit:GetUnitType() == GameInfoTypes["UNIT_DRAFT_A_UNIT"] then
			local spawn = { "UNIT_SNIPER", "UNIT_PLAYMAKER", "UNIT_GRINDER" }
			local unitRnd = player:InitUnit(GameInfo.Units[spawn[math.random(#spawn)]].ID, unit:GetX(), unit:GetY())
			unitRnd:Convert(unit)
		end
	[COLOR="blue"]--[/COLOR][COLOR="red"]end[/COLOR]
end
Events.SerialEventUnitCreated.Add(Unit_Draft)

I myself wouldn't make AW's suggested changes for two reasons:
1) I can't see any need to
2) You can expand to allow more than three random units merely by adding another unit to the list you have within the line
Code:
local spawn = { "UNIT_SNIPER", "UNIT_PLAYMAKER", "UNIT_GRINDER" }

* I added this paragragh because I haven't actually tested this code in-game, so can't say one way or the other if there is some bug in there waiting to...errr...feature itself.
 
Top Bottom