Jump to content

How do I spawn in an item filled bag?


tinyrascal

Recommended Posts

I'm creating a story driven mod that will have a reward for the player at the end. I've created a custom bag that I was able to successfully spawn in when the player approaches the location of the loot.

 

However, I don't know how to fill the bag with loot. I've looked all over the internet for solutions but nothing seems to work with my current code.

 

Perhaps I am overthinking this because I feel this should be easy to accomplish. Any guidance is much appreciated.

 

See below my code to spawn the loot bag named NSBag_WeaponBag.

 

[spoiler][code]local function SpawnInventoryItem(sq)
  local sq = getCell():getGridSquare(10969, 9161, 0)
  local modData = ModData.getOrCreate("MyMod") -- tracks item so no respawn on load
  if modData.itemSpawned then  -- part of item tracker
  return 
  end
  if (sq ~= nil) then
    sq:AddWorldInventoryItem("Base.NSBag_WeaponBag", 0, 0, 0)
	
    Events.LoadGridsquare.Remove(SpawnInventoryItem) --once
	modData.itemSpawned = true -- part of item tracker
  end

end

Events.LoadGridsquare.Add(SpawnInventoryItem)[/code][/spoiler]

 

Link to comment
Share on other sites

This seemed to fix it.:-D

 

local function SpawnInventoryItem(sq)
    local sq = getCell():getGridSquare(10969, 9161, 0)
    local modData = ModData.getOrCreate("MyMod") -- tracks item so no respawn on load

    if modData.itemSpawned then -- part of item tracker
        return
    end

    if (sq ~= nil) then
        -- Create the item and add it to the world
        local item = sq:AddWorldInventoryItem("Base.NSBag_WeaponBag", 0, 0, 0)
        modData.itemSpawned = true -- part of item tracker
        
        -- Add loot inside the item
        local inventory = item:getInventory()
        inventory:AddItem("Base.Axe")
        inventory:AddItem("Base.Bandage")
        inventory:AddItem("Base.CannedBeans")
    end
end

Events.LoadGridsquare.Add(SpawnInventoryItem)

 

Link to comment
Share on other sites

On 9/28/2024 at 2:19 AM, tinyrascal said:

This seemed to fix it.:-D

 

local function SpawnInventoryItem(sq)
    local sq = getCell():getGridSquare(10969, 9161, 0)
    local modData = ModData.getOrCreate("MyMod") -- tracks item so no respawn on load

    if modData.itemSpawned then -- part of item tracker
        return
    end

    if (sq ~= nil) then
        -- Create the item and add it to the world
        local item = sq:AddWorldInventoryItem("Base.NSBag_WeaponBag", 0, 0, 0)
        modData.itemSpawned = true -- part of item tracker
        
        -- Add loot inside the item
        local inventory = item:getInventory()
        inventory:AddItem("Base.Axe")
        inventory:AddItem("Base.Bandage")
        inventory:AddItem("Base.CannedBeans")
    end
end

Events.LoadGridsquare.Add(SpawnInventoryItem)

 


Nice.
 

Not a criticism, but I would have expected the function to start something like:

 

if sq:getX() == 10969 and sq:getY() == 9161 and sq:getZ() == 0 then
--your code here
end


That way you don't need to get the square and check if it exists, just use the square provided by the function.

 

Also, in case you don't know, you can just do:  if sq then - as all non-nil values equate to true (with the exception of boolean false, obviously).
 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...