Jump to content

Spawn item at specific location


LoveBot

Recommended Posts

Hello everyone.

 

I'm trying to spawn an item at a very specific location in the world (x: 5548, y: 12469, z: 0) when the game starts, but strangely, I'm unable to.

I saw a lot of posts in this forum, and only found two "solutions". This one:

Which makes use of getOrCreateGridSquare() which sounds great but doesn't work.

With this solution, I tried a lot of hooks from this list:

https://pzwiki.net/wiki/Category:Lua_Events

But the problem is, it only works for grid squares that are near the player (I'm assuming the system only loads the grid squares as needed, which makes sense. But then again, getOrCreateGridSquare() should work anyways, because I'm forcing the creation of the grid square, right?).

 

I also tried this solution:

Which seems to check coordinates every time a grid square is spawned, which is honestly crazy and very bad for performance.

 

How do you usually do something like this? Shouldn't it be very simple? What am I getting wrong?

 

Sorry for not posting my code. That is because I tried a lot of different solutions. But in case you really need something, here's a simplified version of one fo my attempts:

 

CVM.spawn = function()
  local x, y, z = 5548, 12469, 0;
  local sq = getCell():getOrCreateGridSquare(x, y, z);
  sq:AddWorldInventoryItem("Base.Axe", 0, 0, 0);
end


Events.OnCGlobalObjectSystemInit.Add(CVM.spawn)

 

Thank you for your help.

Edited by LoveBot
Link to comment
Share on other sites

There's probably a better way, but the way I coded this in Hydrocraft was using Events.EveryHours.Add(function), and checking the square was loaded (not nil) before calling AddWorldInventoryItem.

 

You'd also need (I assume) some mechanism for only doing this once.  In the code I wrote I didn't need that as I wanted an item to keep respawning, all I added was a check to see if the item was there already so it didn't spawn more than one at the same.

 

 

I've seen mods using the bad for performance option seemingly without issue, the Insurgent mod does that I believe.

 

Link to comment
Share on other sites

Thanks for the reply.

 

The problem with Events.EveryHours, I assume, is that if the player goes to that location after the tile loads but before the hour "ticks", the item will not be there. But I can test for that.

 

I'll check the mods you suggested. But aren't there any mods that simply spawn items on a place at the beginning of the game?

I tried to find a mod that does this and use it as example, but I could find none.

Link to comment
Share on other sites

1 hour ago, LoveBot said:

Thanks for the reply.

 

The problem with Events.EveryHours, I assume, is that if the player goes to that location after the tile loads but before the hour "ticks", the item will not be there. But I can test for that.

 

I'll check the mods you suggested. But aren't there any mods that simply spawn items on a place at the beginning of the game?

I tried to find a mod that does this and use it as example, but I could find none.

 

Correct, but in my case that wasn't an issue as the spawn location was deep inside the military base, so it would be there by the time the player arrived (probably).

 

I don't know of any, but there might be some that use a technique I don't know about.

Link to comment
Share on other sites

Thanks.

 

I think I'll go with  Events.EveryMinute for now and check if the tile exists.

I'll add a flag to make sure it only spawns once.

 

That's what I'll probably do until I find a better option.

 

Thanks for your feedback, mate. Your suggestion led me in a good direction. At least now I have an almost perfect solution :-)

 

I'll post my solution here later as reference for anyone who finds the same problem.

 

If anyone else has any better ideas, please let me know.

 

Link to comment
Share on other sites

  • 6 months later...

Hello,

I've got a working code for spawning items into the game world. However, the items spawn every time I load up the game again. Would anyone know how to make the specific Item only spawn once into the world, and then never again. Here is the code I'm using..

 

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

Events.LoadGridsquare.Add(SpawnInventoryItem)

 

Thanks,

-Raven

 

Edit* I've gotten my answer to this question. 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...