Jump to content

Spawn item on the ground


username2764

Recommended Posts

Thank you for your advice.I wrote a few lines of code but they didn't work at all.Could you tell me what the problem is?

local function spawnSpoon(square)
square:AddWorldInventoryItem("Base.Spoon", 171, 204, 0);
end
Events.OnNewGame.Add(spawnSpoon)

Link to comment
Share on other sites

local function SpawnInventoryItem(sq)
  local x, y, z = sq:getX(), sq:getY(), sq:getZ()
  if x == 6142 and y == 5399 and z == 0 then
    sq:AddWorldInventoryItem("Base.Axe", 0, 0, 0)
    Events.LoadGridsquare.Remove(SpawnInventoryItem) --once
  end
end

Events.LoadGridsquare.Add(SpawnInventoryItem)

 

Edited by ProjectSky
Link to comment
Share on other sites

The code seems to spawn an axe every time the map is loaded.As you might suspect,I don't want too many axes.I can manually disable a mod to prevent spawning axes but it's not a sophisticated method.You've already helped me a lot but please let me know if you have any good ideas.

Link to comment
Share on other sites

FWIW: that code will be running potentially thousands of if statement until the right square is loaded.  You could do something like:

 

local sq = getCell():getGridSquare(6142, 5399, 0)

if (sq ~= nil) then

  sq:AddWorldInventoryItem("Base.Axe", 0, 0, 0)

end

 

sq will be nil if isn't currently loaded, e.g. if it's not near the player.  The code will need to be put in an event of some sort, e.g. EveryHours.

Link to comment
Share on other sites

  • 1 year later...
On 3/7/2022 at 11:24 PM, ProjectSky said:
local function SpawnInventoryItem(sq)
  local x, y, z = sq:getX(), sq:getY(), sq:getZ()
  if x == 6142 and y == 5399 and z == 0 then
    sq:AddWorldInventoryItem("Base.Axe", 0, 0, 0)
    Events.LoadGridsquare.Remove(SpawnInventoryItem) --once
  end
end

Events.LoadGridsquare.Add(SpawnInventoryItem)

 

 

Hello ProjectSky. Your code works really well. I'm able to add my custom items into the world where I need them to be. Is it possible to only have this done once? I don't want the items to spawn every time I load up the game, until there is 1000 items. Username2764 mentioned that they fixed this issue, but didn't say how they did it. I'm new to coding, and I have no idea how to add a flag or whatever, that will make the item only spawn one time. Any help would be appreciated.

 

Edit* I've gotten my answer already. Thanks again, ProjectSky.

Edited by Signoftheraven
Question answered
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...