How can I add a starting unit in eras.xml so that only human players get it, not AI?

jwsimms42

Chieftain
Joined
Nov 7, 2016
Messages
3
I would like to add, say, a scout unit to human controlled civs, but not AI. I know this can be accomplished in eras.xml in reverse, i.e. adding a unit to AI civs, but not human. Can this be done?

Thanks!
 
I don't know of any way to that with xml right now. I do have a lua script that will spawn a scout for human player on first turn(i was messing around with create function).
The only problem right now is gamescripts will only load in new game, but that would be fine for this purpose.

That would be PERFECT for my purposes!
 
@gyogen2 - I have been so long away from LUA and wasn't great to begin with... so I'd appreciate your help with your code here...

I tried to add the ability to spawn extra scouts and settlers... no dice, so I tried just adding a single settler unit and scout... I'd like to be able to adjust the number of spawns for the units... but my code-fu fails.

Here's what I have for a single scout & settler, can you point out where I failed please?

Code:
local iScout = GameInfo.Units["UNIT_SCOUT"].Index
local iSettler = GameInfo.Units["UNIT_SETTLER"].Index


local function OnPlayerTurnActivated( player, bIsFirstTime )
local pUnit;
print("running");
   if (bIsFirstTime and player == Game.GetLocalPlayer()) then
   local currentTurn = Game.GetCurrentGameTurn();

       if 1 == currentTurn then   
       local pPlayer = Players[player]

           if pPlayer:IsHuman() then
               local adjacentPlot;
               local playerUnits = pPlayer:GetUnits()

                   for i, unit in playerUnits:Members() do
                       local unitTypeName = UnitManager.GetTypeName(unit)
                       print("unitTypeName", unitTypeName)   ;

                       if "LOC_UNIT_SETTLER_NAME" == unitTypeName then
                           local pUnit = playerUnits:Create(iScout, unit:GetX(), unit:GetY())
                           pUnit:JumpToNearestValidPlot();
                       end
                   end
           end
           
           if pPlayer:IsHuman() then
               local adjacentPlot;
               local playerUnits = pPlayer:GetUnits()

                   for i, unit in playerUnits:Members() do
                       local unitTypeName = UnitManager.GetTypeName(unit)
                       print("unitTypeName", unitTypeName)   ;

                       if "LOC_UNIT_SETTLER_NAME" == unitTypeName then
                           local pUnit = playerUnits:Create(iSettler, unit:GetX(), unit:GetY())
                           pUnit:JumpToNearestValidPlot();
                       end
                   end
           end

       end
   end
end
           
Events.PlayerTurnActivated.Add(OnPlayerTurnActivated)
 
Yeah, jumpto doesn't work so anything after wouldn't.

I hadn't finished that code either, I just posted because it would do what was requested.

I think is more what your looking for (though someone else could probably do it prettier)
Code:
local iScout = GameInfo.Units["UNIT_SCOUT"].Index
local iSettler = GameInfo.Units["UNIT_SETTLER"].Index


local function OnPlayerTurnActivated( player, bIsFirstTime )
local pUnit;
print("running");
    if (bIsFirstTime and player == Game.GetLocalPlayer()) then
    local currentTurn = Game.GetCurrentGameTurn();
    local SpawnTurn = 0;
    local playerUnits;  
    local adjacentPlot;
    local unitPlot;
        if 1 == currentTurn then  
        local pPlayer = Players[player]
            if pPlayer:IsHuman() then
             playerUnits = pPlayer:GetUnits()
               
                for i, unit in playerUnits:Members() do
                    local unitTypeName = UnitManager.GetTypeName(unit)

                    if "LOC_UNIT_SETTLER_NAME" == unitTypeName then
                        SpawnTurn = 1;
                        unitPlot = Map.GetPlot(unit:GetX(), unit:GetY());
                       
                    end
                end
            end      
           
            if SpawnTurn == 1 then
           
                pUnit = playerUnits:Create(iScout, unitPlot:GetX(), unitPlot:GetY())  
                for direction = 1, DirectionTypes.NUM_DIRECTION_TYPES - 1, 1 do
                    adjacentPlot = Map.GetAdjacentPlot(pUnit:GetX(), pUnit:GetY(), direction);
                    if (adjacentPlot ~= nil) and not (adjacentPlot:IsWater() or adjacentPlot:IsImpassable()) then
                        break      
                    end
                end
                pUnit2 = playerUnits:Create(iSettler, adjacentPlot:GetX(), adjacentPlot:GetY())  
            end  
        end
    end
end
 
Last edited:
@gyogen2
Here's what I have for a single scout & settler, can you point out where I failed please?
Open Lua.log or launch firetuner. It will tell you the line with error.
Here is simple code to spawn unit:
Code:
local function OnPlayerTurnActivated( iPlayer, bIsFirstTime )
       local pPlayer = Players[iPlayer]
       if pPlayer:IsHuman() and Game.GetCurrentGameTurn() == 1 then
             local pCapitalCity = pPlayer:GetCities():GetCapitalCity()
             local pPlayerUnits = pPlayer:GetUnits()
             local pUnit = pPlayerUnits:Create(GameInfo.Units["UNIT_SCOUT"].Index, pCapitalCity:GetX(), CapitalCity:GetY())
      end
end
Events.PlayerTurnActivated.Add(OnPlayerTurnActivated)
 
Last edited:
Haven't looked at/tried it yet @gyogen2, been busy with that pesky Real Life thing... But at a quick glance, this pops a single settler and a single scout, if i'm reading it correctly, yes?

If that's the case, how/where would I specify a quantity of each unit to spawn? Say like "Spawn me 4 settlers and 10 scouts and 2 warriors" ?

Thanks for the help, it's much appreciated!!

Anyway, I'll check back in later
 
Something is wrong... or most likely me not understanding something.

So I copy/pasted your code from above (the 'This is more what you're looking for post' and it spawns a scout just fine, but not the Settler.

I thought that it spawned both in that example and if I wanted more of X unit, then I add in the for statement - which I haven't tried yet, since I'm having issues as is.

I remember now why I didn't get far with LUA... I hate coding much more than html/xml. *sigh*
 
This is new game w/ 2 set to spawn (another mod also added a scout, that's why 3)
upload_2016-11-8_21-23-10.png


Here's code with for statement:

Code:
local iScout = GameInfo.Units["UNIT_SCOUT"].Index
local iSettler = GameInfo.Units["UNIT_SETTLER"].Index


local function OnPlayerTurnActivated( player, bIsFirstTime )
local pUnit;

    if (bIsFirstTime and player == Game.GetLocalPlayer()) then
    local currentTurn = Game.GetCurrentGameTurn();
    local SpawnTurn = 0;
    local playerUnits;  
    local adjacentPlot;
    local unitPlot;
        if 1 == currentTurn then  
        local pPlayer = Players[player]
            if pPlayer:IsHuman() then
             playerUnits = pPlayer:GetUnits()
               
print("SpawnTurn", SpawnTurn)    ;              
                for i, unit in playerUnits:Members() do
                    local unitTypeName = UnitManager.GetTypeName(unit)
print("unitTypeName", unitTypeName)    ;
                    if "LOC_UNIT_SETTLER_NAME" == unitTypeName then
                        SpawnTurn = 1;
                        unitPlot = Map.GetPlot(unit:GetX(), unit:GetY());
                        print("SpawnTurn", SpawnTurn)    ;      
                    end
                end
            end      
           
            if SpawnTurn == 1 then
print("yeps", SpawnTurn)    ;  
            for i = 1, 2 do
                    pUnit = playerUnits:Create(iScout, unitPlot:GetX(), unitPlot:GetY())  
                    for direction = 1, DirectionTypes.NUM_DIRECTION_TYPES - 1, 1 do
                        adjacentPlot = Map.GetAdjacentPlot(pUnit:GetX(), pUnit:GetY(), direction);
                        if (adjacentPlot ~= nil) and not (adjacentPlot:IsWater() or adjacentPlot:IsImpassable()) then
                            break      
                        end
                    end
                    pUnit2 = playerUnits:Create(iSettler, adjacentPlot:GetX(), adjacentPlot:GetY())  
                end  
            end
        end
    end
end
           



Events.PlayerTurnActivated.Add(OnPlayerTurnActivated)
 
*facpalm/headdesk*

I'm an idjit. I copy/pasted your code above, but I missed the very last line... oh and then I pasted the code into a file in my 'downloaded' directory, not my working or the actual mods dir of the game... oh and then I forgot to adjut the modinfo with thr correct file name.

So once I figured out how I was being dumb and corrected it all... tada! it works as advertised.

*facpalm/headdesk*

So yea... gonna go poke at things now and try to resolve another one of my mods that's having issues now... I'll come poke you again for more help I'm sure I'll need. Even if it's along the lines of "Are you SURE you copied it to the right place this time" commentary.
:)
 
Top Bottom