/back Command

Post Reply
kosmonautik
Member
Posts: 30
Joined: Fri Jun 24, 2016 10:40
In-game: kosmonautik
Location: Czech Republic
Contact:

/back Command

by kosmonautik » Post

Hello.
While playing I died and couldn't get back to my death location - it was just too far away...
Then I remembered there was a /back command in Minecraft and tried it out -
It didn't work, as expected. So I spent a few hours this afternoon searching the developer wiki etc.

If anyone of you have an idea how to make it, definitely go for it and take this just as inspiration.

Thanks a lot

User avatar
Krock
Developer
Posts: 4649
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: /back Command

by Krock » Post

I think this mod offers something that can help you: viewtopic.php?t=15453
A mod based on your idea would need to register a "on_dieplayer" (or similar) callback. It can be used to get the position of the player given in its function parameter. Then it's just a few lines to add a new chat command to actually set the position of the player. Here's some copypasta material for registering a chat command: https://github.com/minetest/minetest/bl ... #L680-L698
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

kosmonautik
Member
Posts: 30
Joined: Fri Jun 24, 2016 10:40
In-game: kosmonautik
Location: Czech Republic
Contact:

Re: /back Command

by kosmonautik » Post

Well I made this and got stuck here:

Code: Select all

minetest.register_on_dieplayer(function(player)
    local pos = player:getpos()
end)

minetest.register_chatcommand("back", {
	params = "",
	description = "",
	func = function(name, param)
		local player = minetest.get_player_by_name(name)
		if not player then
			return false, "Player not found"
		end
        player:setpos(pos)
	end
})
When the code is like this, when I type /back it says something like "set_pos: expected table got nil"
I then tried:

Code: Select all

minetest.register_on_dieplayer(function(player)
    local pos = player:getpos()
end)

minetest.register_chatcommand("back", {
	params = "",
	description = "",
	func = function(name, param)
		local player = minetest.get_player_by_name(name)
		if not player then
			return false, "Player not found"
		end
        player:setpos({pos})
	end
})
This time: "set_pos: invalid x coordinate"

Kroukuk
Member
Posts: 53
Joined: Tue Feb 27, 2018 18:08
In-game: Kroukuk

Re: /back Command

by Kroukuk » Post

kosmonautik wrote:Well I made this and got stuck here:

Code: Select all

minetest.register_on_dieplayer(function(player)
    local pos = player:getpos()
end)

minetest.register_chatcommand("back", {
	params = "",
	description = "",
	func = function(name, param)
		local player = minetest.get_player_by_name(name)
		if not player then
			return false, "Player not found"
		end
        player:setpos(pos)
	end
})
When the code is like this, when I type /back it says something like "set_pos: expected table got nil"
You'll have to make pos a global variable so it's defined in all the functions and not only the first one. c:
Like so:

Code: Select all

local pos
minetest.register_on_dieplayer(function(player)
	pos = player:getpos(),
	minetest.chat_send_all("died")
end)

minetest.register_chatcommand("back", {
	params = "",
	description = "",
	func = function(name, param)
		local player = minetest.get_player_by_name(name)
		if not player then
			return false, "Player not found"
		end
		if not pos then
			return false, "No death position found"
		end
		player:setpos(pos)
	end
})
I think

User avatar
ExeterDad
Member
Posts: 1717
Joined: Sun Jun 01, 2014 20:00
In-game: ExeterDad
Location: New Hampshire U.S.A

Re: /back Command

by ExeterDad » Post

make pos local outside of the functions.
Your chat command can't see it as it's local to the register_on_dieplayer function.

Edit: ninja'd
I need to type faster.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests