Attach an object/node/model to a player

Post Reply
Nekrofage
Member
Posts: 50
Joined: Mon Oct 17, 2016 21:12
GitHub: Nekrofage

Attach an object/node/model to a player

by Nekrofage » Post

Hi !! :D

I search an example of Lua code to attach an object/node/model to a player.
Do you have links or codes about that ?

Thank in advance!

User avatar
stu
Member
Posts: 923
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11
Location: United Kingdom

Re: Attach an object/node/model to a player

by stu » Post

My wield3d mod has an example of how to do this, if you will excuse the blatant self-promotion. Be warned, however, that object attachment in minetest is rather buggy.

User avatar
AiTechEye
Member
Posts: 819
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: ¯\_(ツ)_/¯

Re: Attach an object/node/model to a player

by AiTechEye » Post

here are another example:

-- attach object
obj_to_attach:set_attach(on_obj, "", {x=0,y=0,z=0}, {x=0,y=0,z=0})

-- check if attached and detach it
if obj:get_attach() then
obj:set_detach()
end

Nekrofage
Member
Posts: 50
Joined: Mon Oct 17, 2016 21:12
GitHub: Nekrofage

Re: Attach an object/node/model to a player

by Nekrofage » Post

Hi all !! :D

Thank you for your Lua code examples !

User avatar
sirrobzeroone
Member
Posts: 190
Joined: Mon Jul 16, 2018 07:56

Re: Attach an object/node/model to a player

by sirrobzeroone » Post

Sorry a bit of Thread-o-mancy but my question relates to this as well.

I'm trying to attach a node to a player, I checked out stu's code but I'm not getting it and I end up with nothing, on the plus side my code doesn't crash/error. Here's what I've cribbed/borrowed out of stu's code. However it dosen;t work. Basically all I want to do is attach a basic 1x1 node to the players head but at the moment I'd settle for anything visibly attached to the player...help much appreciated:

Code: Select all

--register something to attach
minetest.register_node("testmod:hand", {
	description =  "Thing",
	
	tiles = {"top.png^[colorize:#fe8a18:220",
			 "side.png^[colorize:#fe8a18:220",
			},
})


--Create a wield entity 
local wield_entity = {
	physical = false,
	collisionbox = {-0.125,-0.125,-0.125, 0.125,0.125,0.125},
	visual = "wielditem",
	textures = {"testmod:hand"},
	visual_size = {x=5, y=5},
	is_visible = true,
	static_save = false,
}

minetest.register_entity("testmod:wield_entity", wield_entity)



minetest.register_on_joinplayer(function(player)

--basicallly Stu's code...well it all is...
local location = {
	"Right Arm",          -- default bone
	{x=0, y=5.5, z=3},    -- default position
	{x=-90, y=225, z=90}, -- default rotation
}
	local name = player:get_player_name()
	local pos = player:get_pos()
	pos.y = pos.y + 0.5
	local object = minetest.add_entity(pos, "testmod:wield_entity", name)

object:set_attach(player, location[1], location[2], location[3])
end)

User avatar
sirrobzeroone
Member
Posts: 190
Joined: Mon Jul 16, 2018 07:56

Re: Attach an object/node/model to a player

by sirrobzeroone » Post

Stumbled on something by paramat, slightly modded it and now a very basic example of attach entity to player on load. Cube can be viewed hovering above players head in 3rd person. Ill leave it here might be useful to someone else - place into init.lua of an empty/test mod.

Code: Select all

minetest.register_entity("modname:testentity", {
	        initial_properties = {
		physical = false,
		collide_with_objects = false,
               collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
		selectionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
		visual = "cube",
		textures = {
					"nil.png",
					"nil.png",
					"nil.png",
					"nil.png",
					"nil.png",
					"nil.png",
					},
		pointable = true,
	},
})

minetest.after(5, function()
	local pos = minetest.get_player_by_name("singleplayer"):get_pos()
	local ent1 = minetest.add_entity(pos, "modname:testentity")
	local player = minetest.get_player_by_name("singleplayer")
        ent1:set_attach(player, "", {x = 0, y = 30, z = 0}, {x = 0, y = 0, z = 0})
	
end)
edit: hmm dosen't quiet work as I hoped end up with an entity at my players feet as well as one above his head, if I figure it out i'll update.

User avatar
texmex
Member
Posts: 1752
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: tacotexmex

Re: Attach an object/node/model to a player

by texmex » Post

Sounds like a case that would be useful for a lot of people. There need to be a wiki page
covering this imo.
Mods | Support Mesehub: bc1qluuests9rxmlnvpjrhsnyjg9ucwy6z3r0y3srw

User avatar
FreeGamers
Member
Posts: 627
Joined: Sat May 25, 2019 00:15
GitHub: is proprietary I use NotABug
Location: United States
Contact:

Re: Attach an object/node/model to a player

by FreeGamers » Post

I was working on a project today and observed similar issues. The entity gets attached below the player at his/her feet.

I'm working on a facehugger (from Alien/Aliens) mob and it doesn't seem to work. Here is the relevant excerpt. I've tried a few variations of this.

Code: Select all

   -- do_custom = function(self)
		-- local pos = self.object:getpos()
		-- local objs = minetest.get_objects_inside_radius(pos, 2)
		-- for _, obj in pairs(objs) do
			-- if obj:is_player() and obj:get_attach() == nil then
					-- obj:set_attach(self.object, "", {x=0, y=8, z=0}, {x=0, y=0, z=0})
					-- self.object:set_animation({x=46, y=46}, 20, 0)
				-- end
			-- end
   -- end,
Looks like the origianal modder gave up on it and uncommented it out too :) from https://github.com/D00Med/mobs_scifi/bl ... ua#L74-L83

User avatar
sirrobzeroone
Member
Posts: 190
Joined: Mon Jul 16, 2018 07:56

Re: Attach an object/node/model to a player

by sirrobzeroone » Post

Freegamers could you make the face hugger a "helmet" bit like 3d_armour does?

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests