Any way to grant a free UnitPerk or UnitUpgrade?

Lord Shadow

Admiral
Joined
Oct 14, 2005
Messages
2,177
Basically, I'm trying to implement perks in Alien Strains, but I can't seem to get the aliens to choose the free upgrades which would grant their units the perks they need. So I was wondering whether there was some way for Lua to grant an upgrade or perk to a specific UnitType. It sounds so simple it should be possible somehow, but there's no indication of anything similar in the vanilla Lua files. Only the granting of PlayerPerks, which isn't what I'm looking for.

Any help would be greatly appreciated.
 
I've done just this in a recent mod. Feel free to browse my lua code for it.

http://steamcommunity.com/sharedfiles/filedetails/?id=338277930
Looks very promising! Now I've to figure out how to adapt it so that the LGF_AddUnitPerks function fires once at the beginning and gives all the alien unit types their corresponding upgrades and perks. Shouldn't be faction-restricted, in theory, since players could get alien units from alien skeletons, and those should have the same perks the actual alien aliens do.

Here's a very rudimentary adaptation of the LGF_AddUnitPerks function, at least. I suck at Lua (I figure pUnit isn't defined, for instance):
Code:
function LGF_AddUnitPerks(player)
	local pPlayer = Players[player]
	local iUpgrade = GameInfo.UnitUpgrades["UNITUPGRADE_TEST_BEETLE_MANTIS"].ID
	local iPerk = GameInfo.UnitPerks["UNITPERK_TEST_BEETLE"].ID

	for pUnit in GameInfo.Units() do
		if pUnit.Type == "UNIT_ALIEN_WOLF_BEETLE_MANTIS" then
			pPlayer:AssignUnitUpgrade(pUnit.ID, iUpgrade, iPerk);
		end
	end
end
 
Well, the aliens are their own player object in the Player[] array. I don't know if they can have upgrades assigned to them, but you can give it a shot. If you want it applied to all players, just iterate over the Player[] array with a for loop.

Also you don't need to loop over the units if you know what units you want upgrades for. For example:

Code:
function LGF_AddUnitPerks(player)
	local iUnit = GameInfo.Units[ "UNIT_ALIEN_WOLF_BEETLE_MANTIS"].ID
	local iUpgrade = GameInfo.UnitUpgrades["UNITUPGRADE_TEST_BEETLE_MANTIS"].ID
	local iPerk = GameInfo.UnitPerks["UNITPERK_TEST_BEETLE"].ID

	for pPlayer in Players[] do
		pPlayer:AssignUnitUpgrade(iUnit, iUpgrade, iPerk);
	end
end

will upgrade everyone's wolf beetles, if they can somehow get their hands on them. (note: double check loop construction since I don't iterate over the Player[] array very often].
 
Thank you for the example! I've concocted the following to encompass the three Wolf Beetle strains:
Code:
function LGF_AddUnitPerks(player)
	local iUnitWBB = GameInfo.Units[ "UNIT_ALIEN_WOLF_BEETLE_BASE"].ID
	local iUpgradeWBB = GameInfo.UnitUpgrades["UNITUPGRADE_TEST_BEETLE_BASE"].ID
	local iPerkWBB = GameInfo.UnitPerks["UNITPERK_TEST_BEETLE"].ID
	
	local iUnitWBM = GameInfo.Units[ "UNIT_ALIEN_WOLF_BEETLE_MANTIS"].ID
	local iUpgradeWBM = GameInfo.UnitUpgrades["UNITUPGRADE_TEST_BEETLE_MANTIS"].ID
	local iPerkWBM = GameInfo.UnitPerks["UNITPERK_TEST_BEETLE"].ID
	
	local iUnitWBC = GameInfo.Units[ "UNIT_ALIEN_WOLF_BEETLE_CHAMELEON"].ID
	local iUpgradeWBC = GameInfo.UnitUpgrades["UNITUPGRADE_TEST_BEETLE_CHAMELEON"].ID
	local iPerkWBC = GameInfo.UnitPerks["UNITPERK_TEST_BEETLE"].ID

	for pPlayer in Players[] do
		pPlayer:AssignUnitUpgrade(iUnitWBB, iUpgradeWBB, iPerkWBB);
		pPlayer:AssignUnitUpgrade(iUnitWBM, iUpgradeWBM, iPerkWBM);
		pPlayer:AssignUnitUpgrade(iUnitWBC, iUpgradeWBC, iPerkWBC);
	end
end
But I get an error, unexpected symbol near ']' on line 14, which is the one with Players[]. Also, shouldn't there be an if clause somewhere so that the script is fired only once? And shouldn't pPlayer be defined?

As I said, it should apply to all players since alien units can be obtained by human factions from the excavation of Alien Skeletons, as in vanilla.
 
I do not have much experience with lua, but with other programming languages.
And the way you were writing it doesnt make sense in the beginning of the loop:

Definition looks like:
"myVariable = <datatype or class>[]"
After that myVariable is an array of the given type

If you want to get a field out of it, it should look like this:
"value = player[0]"

If the loop is similar to the python one the second argument must be something on which you can iterate, like an array for instance.

so try this:
"for pPlayer in Players do:"

In python you needn't to assign a datatype explicitly. So the above code would work well in python. In java it would look this:
"for Iterator pPlayers = Players. Iterator(); pPlayers.hasNext(); pPlayers.next()){
...
}"
(Could be that they progressed bit and loop definitions are easier - but not quite sure)

Hope this helps
 
Could you not do this in a lot more simple fashion by just assigning specific perks to the units in the traits file using the <Trait_FreePromotions> Command? Or where you trying to get it so you had multiple variations of upgrade that the AI chooses from?

Edit: Looking at the files unless you want it choosing it seems pretty easy to assign the alien Leader a trait type and then give it the free promotion perks for units.
 
There's two significant problems with promotions at the moment:

- Due to a severely annoying bug in the game code, modding in new promotions glitches out the unit action UI.
- Not all perk functions can be assigned to promotions (and viceversa, unfortunately).
 
Yes the script should only fire once. I've gone a round about way of it by having it run only when the player founds their capital city (which, conveniently, they have to do on their first turn unlike civ 5).
 
Back
Top Bottom