1. We have added a Gift Upgrades feature that allows you to gift an account upgrade to another member, just in time for the holiday season. You can see the gift option when going to the Account Upgrades screen, or on any user profile screen.
    Dismiss Notice

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

Discussion in 'Civ6 - Creation & Customization' started by jwsimms42, Nov 7, 2016.

  1. jwsimms42

    jwsimms42 Chieftain

    Joined:
    Nov 7, 2016
    Messages:
    3
    Gender:
    Male
    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!
     
  2. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    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.
     
  3. jwsimms42

    jwsimms42 Chieftain

    Joined:
    Nov 7, 2016
    Messages:
    3
    Gender:
    Male
    That would be PERFECT for my purposes!
     
  4. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    Let me test it one more time (might take a few, keep getting 2k terms pop-up issue) then I'll post for you.
     
  5. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    Okay, here you go:

    Edit:was old file
     
    Last edited: Nov 7, 2016
  6. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    Since I have my game modded for multiple units/tile just had scout spawn in same tile as settler.
    This shouldn't cause a problem, but if it does you can try changing:
    Code:
    pUnit = playerUnits:Create(iScout, unit:GetX(), unit:GetY())
    to:
    Code:
    local pUnit = playerUnits:Create(iScout, unit:GetX(), unit:GetY())
                                pUnit:JumpToNearestValidPlot();
     
  7. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    Just realized I uploaded a previous copy. It will work, but it will spawn each turn you have a settler. I re-uploaded the new one, which also has the edit I posted above.
    Sorry about that.
     

    Attached Files:

  8. jwsimms42

    jwsimms42 Chieftain

    Joined:
    Nov 7, 2016
    Messages:
    3
    Gender:
    Male
    Thanks! Can't wait to try it out!
     
  9. Mynex

    Mynex Warlord

    Joined:
    Sep 30, 2010
    Messages:
    287
    @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)
    
     
  10. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    I'll look. byw, that was just a script I was messing around with, the JumpToNearestValidPlot was used in civ v, I was just seeing if it still worked, don't think it does.
     
  11. Mynex

    Mynex Warlord

    Joined:
    Sep 30, 2010
    Messages:
    287
    Gotcha... I did a couple minor mods for 5, but gave up pretty quick when the limitations were realized so quickly.... I'm seriously hoping we have as much (or more!) abilities than IV.
     
  12. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    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: Nov 7, 2016
  13. qqqbbb

    qqqbbb Prince

    Joined:
    Sep 25, 2010
    Messages:
    489
    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: Nov 8, 2016
  14. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    The code we were talking about above was for spawning more than one unit, and on first turn before a capital is created. The reason I have the addition of looking for players settler is to create a spawn point on the map.
     
  15. Mynex

    Mynex Warlord

    Joined:
    Sep 30, 2010
    Messages:
    287
    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
     
  16. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    in V, you could just add a ",number" in the argument but don't think that works now.
    I'm not sure how to define in the command yet, but for now you could just do it like so:
    Code:
    for i = 1, 2  do
    
    -- unit creation code--
    
    end
    and replace the 2 with how many you want
     
  17. Mynex

    Mynex Warlord

    Joined:
    Sep 30, 2010
    Messages:
    287
    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*
     
  18. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    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)
     
  19. gyogen2

    gyogen2 Emperor

    Joined:
    May 8, 2014
    Messages:
    1,201
    Also, I did notice when it's set to multiple spawn, when game starts the first turn the button has next turn, but once you move a unit it go back to normal.
     
  20. Mynex

    Mynex Warlord

    Joined:
    Sep 30, 2010
    Messages:
    287
    *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.
    :)
     

Share This Page