Page 1 of 1

How can I set a spawn point?

PostPosted: Tue May 31, 2016 06:53
by burli
I want to set the spawn point if I rightclick on a node. How can I do this without the bed?

Re: How can I set a spawn point?

PostPosted: Tue May 31, 2016 07:58
by azekill_DIABLO
hijack it :)

Re: How can I set a spawn point?

PostPosted: Tue May 31, 2016 08:05
by burli
azekill_DIABLO wrote:hijack it :)

Well, you are right. I probably hijack the spawn.lua from the bed mod

Re: How can I set a spawn point?

PostPosted: Thu Jun 02, 2016 15:10
by octacian
Add the following code to the node:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
on_rightclick = function(name, pos)
local player = minetest.get_player_by_name(name) -- get player
-- make sure it is a valid player
if not player then
return
end
local pos = player:getpos() -- get player position
-- add 0.5 to x and z to make it cooler
pos.x = math.floor(0.5+pos.x)
pos.z = math.floor(0.5+pos.z)
minetest.setting_set("static_spawn", minetest.pos_to_string(pos)) -- save position as a setting
-- notify player in chat
minetest.chat_send_player(name, "Spawn point set at "..minetest.setting_get("static_spawn"));
-- print to log
minetest.log("action", "[ModName] "..name.."'s spawn point set at "..minetest.setting_get("static_spawn"))
end,


Didn't test it, but it should work. You can call the setting static_spawn whatever you want, as long as you change it in all three places. However, remember that this is creating a setting in the minetest.conf file, so it will apply to all worlds and sometimes in servers is somehow cleared. To fix this, you would have to save these settings within a separate file in the world folder, but I don't have any example code at the moment. You also need a function that calls on respawn such as:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_on_respawnplayer(function(player)
   if not player then
      return
   end
   if minetest.setting_get("static_genesis") == nil or minetest.setting_get("static_genesis") == "" then
      return
   end
   player:setpos(minetest.string_to_pos(minetest.setting_get("static_genesis")))
end)

Once again, that will call for every player which could be an issue. To do this on a player and world basis, I would look at the code for the home command (or beds) as you could probably to copy and paste mostly everything. Also check out my WIP mod Server_Tools which includes a command to set the spawn of the world (which I called genesis, or beginning, point instead). Be careful with what you take from it though, I haven't yet gotten around to fixing any of the problems mentioned, but will soon.