[Question] Entity attached to player

Post Reply
User avatar
Extex
Member
Posts: 246
Joined: Wed Mar 14, 2018 23:14
GitHub: Extex101
In-game: Extex

[Question] Entity attached to player

by Extex » Post

I need help with attaching an entity to a player

Have it functional for several different players, accessible from the player and the player from the entity

I've tried several times but I can't seem to add only one and still have it remain attached and remove it after leaving

Many thanks for your help!
Last edited by Extex on Thu Aug 22, 2019 01:05, edited 1 time in total.
Creator of jelys_pizzaria and motorbike, and player of persistent kingdoms. RIP

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: [Question] Entity attached to player

by runs » Post

See my mount code, it could help you a bit:

Code: Select all

petz.force_detach = function(player)
	local attached_to = player:get_attach()
	if not attached_to then
		return
	end
	local entity = attached_to:get_luaentity()
	if entity.driver and entity.driver == player then
		entity.driver = nil
	end
	player:set_detach()
	default.player_attached[player:get_player_name()] = false
	player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
	default.player_set_animation(player, "stand" , 30)
	player:set_properties({visual_size = {x = 1, y = 1}, pointable = true })	
end

-------------------------------------------------------------------------------
minetest.register_on_leaveplayer(function(player)
	petz.force_detach(player)
end)

minetest.register_on_shutdown(function()
	local players = minetest.get_connected_players()
	for i = 1, #players do
		petz.force_detach(players[i])
	end
end)

minetest.register_on_dieplayer(function(player)
	petz.force_detach(player)
	return true
end)

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

Re: [Question] Entity attached to player

by Krock » Post

@runs: They want to attach an entity to a player. Your code assumes the inverse.


Here's a 5.0+ approach:
parent = player
child = luaentity

Code: Select all

	... inside object definition
	on_detach(self, parent)
		print("got detached from player " .. parent:get_player_name())
		self.object:remove()
	end,
https://github.com/minetest/minetest/bl ... 3619-L3621
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
Extex
Member
Posts: 246
Joined: Wed Mar 14, 2018 23:14
GitHub: Extex101
In-game: Extex

Re: [Question] Entity attached to player

by Extex » Post

OK added.
But how would I attach it to the player (and keep it attached to the player) and be able to edit it in-game?
Creator of jelys_pizzaria and motorbike, and player of persistent kingdoms. RIP

User avatar
Extex
Member
Posts: 246
Joined: Wed Mar 14, 2018 23:14
GitHub: Extex101
In-game: Extex

Re: [Question] Entity attached to player

by Extex » Post

I need some way to access the entity from the player, and the player from the entity.
And keep it attached to the player
Creator of jelys_pizzaria and motorbike, and player of persistent kingdoms. RIP

User avatar
sparky
Member
Posts: 154
Joined: Sun Oct 05, 2014 00:54
GitHub: Elkien3
IRC: ircSparky
In-game: sparky
Location: USA
Contact:

Re: [Question] Entity attached to player

by sparky » Post

What do you mean by "access"?

You can save the entity id in a table, something like:

Code: Select all

entites[playername] = add_entity()
You can also have the entity (or even the player I think) have it's own variable
eg:

Code: Select all

self.owner = player
or

Code: Select all

entites[playername].owner = player
or you can get the object the entity is attached to with

Code: Select all

self:get_attach()
I'm pretty sure when you attach an object to a player the player can't see it, nor the collision box of it, unless they are in third person.
Also the Selection/collisionboxes of entities attached to entities are not functional further than 200 nodes from (0,0,0)

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 16 guests