[Done] Adding loot to Spawner

Place to get help with not working mods / modding interface.

[Done] Adding loot to Spawner

Postby TheSAguy » Sat Jan 27, 2018 6:58 pm

    Hi, I'd like to endure that a Spawner drops Alien artifacts when killed. What's wrong with my code below?

    Code: Select all
    for k, unitSpawner in pairs(data.raw["unit-spawner"]) do
       
       if unitSpawner.loot == nil then

          unitSpawner.loot = {}
          table.insert(unitSpawner.loot, {  item = "alien-artifact",  count_min = 5,  count_max = 10,  probability = 1 } )
             
       end
       
    end


    Thanks.,
    Last edited by TheSAguy on Tue Jul 17, 2018 1:02 am, edited 1 time in total.
    TheSAguy
    Smart Inserter
    Smart Inserter
     
    Posts: 1244
    Joined: Mon Jan 13, 2014 6:17 pm

    Re: Adding loot to Spawner

    Postby orzelek » Sun Jan 28, 2018 6:07 pm

    Move table.insert call outside of the condition.
    Currently if it's not nil because someone else added the loot your code will not add anything.

    It would be also nice to check if table doesn't contain the alien artifact already and edit the count then.
    orzelek
    Smart Inserter
    Smart Inserter
     
    Posts: 3236
    Joined: Fri Apr 03, 2015 10:20 am

    Re: Adding loot to Spawner

    Postby bobingabout » Sun Jan 28, 2018 6:53 pm

    this is a peice of code I used.
    Code: Select all
    if data.raw["unit-spawner"]["biter-spawner"] then
      if data.raw["unit-spawner"]["biter-spawner"].loot == nil then
        data.raw["unit-spawner"]["biter-spawner"].loot = {}
      end
      if data.raw.item["alien-artifact"] then
        table.insert(data.raw["unit-spawner"]["biter-spawner"].loot, {  item = "alien-artifact",  count_min = 5,  count_max = 15,  probability = 1 } )
      end
    end



    The first section checks if a loot table exists, and if not, adds a blank one.
    The second section checks to see if the item I want to add exists, and if so, adds it to the loot table.

    In my opinion, this is the best way to do it, but you don't have to check if the item exists if you know it does (EG, you added it yourself).
    This is part of my enemies mod to re-add alien artifacts as loot to the spawner if the mod settings says to re-add them to the game.
    No matter what you do, you can't please everyone.
    User avatar
    bobingabout
    Smart Inserter
    Smart Inserter
     
    Posts: 5727
    Joined: Fri May 09, 2014 1:01 pm
    Location: England

    Re: Adding loot to Spawner

    Postby TheSAguy » Sun Jan 28, 2018 9:20 pm

    Thanks, so this is my code now:

    Code: Select all
    ---- Adding Alien Artifacts to Spawners:
    for k, unitSpawner in pairs(data.raw["unit-spawner"]) do
       
       if unitSpawner.loot == nil then

          unitSpawner.loot = {}
                   
       end
       
       if data.raw.item["alien-artifact"] then
          if unitSpawner.loot.item == "alien-artifact" then
             break
          
          else
             table.insert(unitSpawner.loot, {  item = "alien-artifact",  count_min = 5,  count_max = 10,  probability = 1 } )
          end
       end
       
    end



    I also have one for units:
    Code: Select all

    ---- Adding Small Alien Artifacts to Units:
    for k, units in pairs(data.raw["unit"]) do
       
       if units.loot == nil then

          units.loot = {}
                   
       end
       
       if data.raw.item["small-alien-artifact"] then
          if units.loot.item == "small-alien-artifact" then
             break
          
          else
             table.insert(units.loot, {  item = "small-alien-artifact",  count_min = 1,  count_max = 2,  probability = 1 } )
          end
       end
       
    end


    Bob, your code only adds to the Vanilla spawners, but Rampant Mod just added a lot of new units and spawners and trying to add to those. (Rampant is not incompatible with NE Enemies.)
    Trying to make it compatible with NE Buildings by adding in the Alien Artifact drops.
    TheSAguy
    Smart Inserter
    Smart Inserter
     
    Posts: 1244
    Joined: Mon Jan 13, 2014 6:17 pm

    Re: Adding loot to Spawners or Units

    Postby TheSAguy » Mon Jul 16, 2018 7:41 pm

    Hi,

    I'm having a hard time figuring the code out.
    I'm trying to add loot to units ONLY if they don't currently have "small-alien-artifacts" as loot.

    My code below is not working, it keeps adding the new loot even though the unit already had "small-alien-artifacts" as loot:

    Code: Select all
    for k, units in pairs(data.raw["unit"]) do
       

       if data.raw.item["small-alien-artifact"] then
          if units.loot == nil then
             units.loot = {}
          end
          
          if units.loot.item ~= "small-alien-artifact" then
             table.insert(units.loot, {item = "small-alien-artifact",  count_min = 10,  count_max = 20,  probability = 1 } )
          end
       end
       
    end



    Result:
    Code: Select all
    data.raw.unit["small-biter"].loot[1].item = "small-alien-artifact"
    data.raw.unit["small-biter"].loot[1].probability = 1
    data.raw.unit["small-biter"].loot[1].count_min = 1
    data.raw.unit["small-biter"].loot[1].count_max = 1
    data.raw.unit["small-biter"].loot[2].item = "small-alien-artifact"
    data.raw.unit["small-biter"].loot[2].count_min = 10
    data.raw.unit["small-biter"].loot[2].count_max = 20
    data.raw.unit["small-biter"].loot[2].probability = 1



    The new lot is being added even though it already had "small-alien-artifact" as loot.
    What am I doing wrong here?

    Originally I had:
    Code: Select all
    for k, units in pairs(data.raw["unit"]) do
       
       if units.loot == nil then

          units.loot = {}
                   
       end
       
       if data.raw.item["small-alien-artifact"] then
          if units.loot.item == "small-alien-artifact" or units.loot.item == "alien-artifact" then
             break
          
          else
             table.insert(units.loot, {  item = "small-alien-artifact",  count_min = 1,  count_max = 2,  probability = 1 } )
          end
       end
       
    end


    But this has the same result.
    Thanks.
    TheSAguy
    Smart Inserter
    Smart Inserter
     
    Posts: 1244
    Joined: Mon Jan 13, 2014 6:17 pm

    Re: Adding loot to Spawner

    Postby eradicator » Mon Jul 16, 2018 8:07 pm

    @bob: your code also has the drawback of not working with items that are not type='item', i.e. ammo, etcpp.
    @OP: If you need to check if the item exists at all you'll have to add that yourself, otherwise try this:

    Code: Select all
    local function add_loot(spawner,item)
      if not spawner.loot then spawner.loot = {} end
      local has = false
      for _,v in pairs(spawner.loot) do
        if v.item == item.item then has = true break end
        end
      if not has then
        table.insert(spawner.loot,item)
        end
      end

    add_loot(
      data.raw["unit-spawner"]['SPAWNERNAME'],
      {item = "alien-artifact",count_min = 5,count_max = 10,probability = 1}
      )
    User avatar
    eradicator
    Smart Inserter
    Smart Inserter
     
    Posts: 1913
    Joined: Tue Jul 12, 2016 9:03 am
    Location: Mod support languages: JA/DE/EN

    Re: [Done] Adding loot to Spawner

    Postby TheSAguy » Tue Jul 17, 2018 1:03 am

    Thanks, this made things very easy!
    TheSAguy
    Smart Inserter
    Smart Inserter
     
    Posts: 1244
    Joined: Mon Jan 13, 2014 6:17 pm

    Re: Adding loot to Spawner

    Postby bobingabout » Wed Jul 18, 2018 9:06 am

    eradicator wrote:@bob: your code also has the drawback of not working with items that are not type='item', i.e. ammo,

    Recipe ingredients (and results) tables use what I call simple item. so, ammo, tool etc all come under the "item" type, and since type defaults to item, you only need to specify it if you are using a fluid.

    AFAIK, the same is true for the loot table (correct me if I'm wrong) and since you can't have a fluid drop on the ground, you should never need to specify the item's type, since it should always be the default type, item.

    Also, my example was just what I did, looking for something that was specifically an item (not a tool, etc), I didn't say you shouldn't customise it to be fit for your purpose.
    No matter what you do, you can't please everyone.
    User avatar
    bobingabout
    Smart Inserter
    Smart Inserter
     
    Posts: 5727
    Joined: Fri May 09, 2014 1:01 pm
    Location: England

    Re: [Done] Adding loot to Spawner

    Postby eradicator » Wed Jul 18, 2018 11:59 am

    @bob:
    I meant this line:
    Code: Select all
    if data.raw.item["alien-artifact"] then

    Which wouldn't work in those cases. And i only mentiond it because not everyone who reads this will have enough experience in the api to instantly recognize this limitation. Not saying you're doing it wrong ;).
    User avatar
    eradicator
    Smart Inserter
    Smart Inserter
     
    Posts: 1913
    Joined: Tue Jul 12, 2016 9:03 am
    Location: Mod support languages: JA/DE/EN

    Re: [Done] Adding loot to Spawner

    Postby bobingabout » Thu Jul 19, 2018 8:15 am

    eradicator wrote:@bob:
    I meant this line:
    Code: Select all
    if data.raw.item["alien-artifact"] then

    Which wouldn't work in those cases. And i only mentiond it because not everyone who reads this will have enough experience in the api to instantly recognize this limitation. Not saying you're doing it wrong ;).

    Right, well, alien-artifact is an item, so... I'll admit it does trip me up when I use some of the non-item items. Like how most of my modules components are actually tools, so that they can be used as research materials.
    No matter what you do, you can't please everyone.
    User avatar
    bobingabout
    Smart Inserter
    Smart Inserter
     
    Posts: 5727
    Joined: Fri May 09, 2014 1:01 pm
    Location: England

    Re: [Done] Adding loot to Spawner

    Postby eradicator » Thu Jul 19, 2018 11:30 am

    bobingabout wrote:
    eradicator wrote:@bob:
    I meant this line:
    Code: Select all
    if data.raw.item["alien-artifact"] then

    Which wouldn't work in those cases. And i only mentiond it because not everyone who reads this will have enough experience in the api to instantly recognize this limitation. Not saying you're doing it wrong ;).

    Right, well, alien-artifact is an item, so... I'll admit it does trip me up when I use some of the non-item items. Like how most of my modules components are actually tools, so that they can be used as research materials.

    I knew i posted a solution to that once (which was easier to find than i feared :p). I didn't remember that you were in that thread too though :P.
    User avatar
    eradicator
    Smart Inserter
    Smart Inserter
     
    Posts: 1913
    Joined: Tue Jul 12, 2016 9:03 am
    Location: Mod support languages: JA/DE/EN


    Return to Modding help

    Who is online

    Users browsing this forum: No registered users and 6 guests