Workaround for when entities refuse to spawn

Post Reply
User avatar
Wuzzy
Member
Posts: 4786
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy
Contact:

Workaround for when entities refuse to spawn

by Wuzzy » Post

Spawning an entity with minetest.add_entity seems like a game of chance. You never know whether the entity will actually spawn. Actually, it does always spawn, but there's a chance it is immediately removed afterwards. So the return value of minetest.add_entity is useless. This is one of the most frustrating bugs when you have to work with entities in a game and you absolutely must ensure the spawning of an entity. See https://github.com/minetest/minetest/issues/4759

Entity spawning is only reliable when it's close enough to players, not on an arbitrary map position.

But I think I have finally discovered a very simple workaround!
Right before you call minetest.add_entity, you call minetest.load_area in a small area that includes the entity position before. Like so:

Code: Select all

local pos = vector.new(0,0,0)
local offset = vector.new(2,2,2)
minetest.load_area(vector.subtract(pos, offset), vector.add(pos, offset))
minetest.add_entity(pos, "example:my_entity")
I'm not 100% sure if this is 100% reliable, but it allowed me to fix some bugs with parachute spawning in Repixture.

User avatar
FreeLikeGNU
Member
Posts: 280
Joined: Tue Oct 28, 2014 02:50
GitHub: FreeLikeGNU
IRC: freelikegnu
In-game: FreeLikeGNU

Re: Workaround for when entities refuse to spawn

by FreeLikeGNU » Post

I had to solve a similar issue with my witches. A witch is supposed to spawn in a cottage, but the cottages are often far from a player as they are generated on a map gen dungeon event.

To work around this I aliased the air node as witch_spawn and after the cottage is constructed the node is placed inside. I set the witch mob to always spawn from that node (which will happen as the player nears the cottage).

Once the witch spawns, it has a function to search for that node and remove it (so that no more witches spawn there). It's pretty reliable unless a player is flying with fast and gets too close for a spawn to occur.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests