Page 1 of 1

[Done] Adding loot to Spawner

Posted: Sat Jan 27, 2018 6:58 pm
by TheSAguy
    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.,

    Re: Adding loot to Spawner

    Posted: Sun Jan 28, 2018 6:07 pm
    by orzelek
    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.

    Re: Adding loot to Spawner

    Posted: Sun Jan 28, 2018 6:53 pm
    by bobingabout
    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.

    Re: Adding loot to Spawner

    Posted: Sun Jan 28, 2018 9:20 pm
    by TheSAguy
    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.

    Re: Adding loot to Spawners or Units

    Posted: Mon Jul 16, 2018 7:41 pm
    by TheSAguy
    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.

    Re: Adding loot to Spawner

    Posted: Mon Jul 16, 2018 8:07 pm
    by eradicator
    @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}
      )

    Re: [Done] Adding loot to Spawner

    Posted: Tue Jul 17, 2018 1:03 am
    by TheSAguy
    Thanks, this made things very easy!

    Re: Adding loot to Spawner

    Posted: Wed Jul 18, 2018 9:06 am
    by bobingabout
    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.

    Re: [Done] Adding loot to Spawner

    Posted: Wed Jul 18, 2018 11:59 am
    by eradicator
    @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 ;).

    Re: [Done] Adding loot to Spawner

    Posted: Thu Jul 19, 2018 8:15 am
    by bobingabout
    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.

    Re: [Done] Adding loot to Spawner

    Posted: Thu Jul 19, 2018 11:30 am
    by eradicator
    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.