Jump to content

Spawn item at specific location


LoveBot

Recommended Posts

Posted (edited)

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...