Attach an object/node/model to a player

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

Attach an object/node/model to a player

by Nekrofage » Sun Dec 25, 2016 16:45

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
Location: United Kingdom
GitHub: stujones11

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

by stu » Mon Dec 26, 2016 13:15

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: 745
Joined: Fri May 29, 2015 21:14
Location: Unknown
GitHub: AiTechEye

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

by AiTechEye » Tue Dec 27, 2016 21:38

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 » Fri Feb 03, 2017 21:34

Hi all !! :D

Thank you for your Lua code examples !
 

sirrobzeroone
Member
 
Posts: 123
Joined: Mon Jul 16, 2018 07:56

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

by sirrobzeroone » Tue Sep 10, 2019 11:34

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)
 

sirrobzeroone
Member
 
Posts: 123
Joined: Mon Jul 16, 2018 07:56

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

by sirrobzeroone » Thu Sep 12, 2019 08:28

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: 1588
Joined: Mon Jul 11, 2016 21:08
GitHub: tacotexmex
In-game: texmex

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

by texmex » Fri Sep 13, 2019 10:04

Sounds like a case that would be useful for a lot of people. There need to be a wiki page
covering this imo.
 

User avatar
FreeGamers
Member
 
Posts: 282
Joined: Sat May 25, 2019 00:15
Location: United States

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

by FreeGamers » Wed Oct 30, 2019 09:20

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
"i showed u my source code, pls respond"
 

sirrobzeroone
Member
 
Posts: 123
Joined: Mon Jul 16, 2018 07:56

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

by sirrobzeroone » Fri Nov 01, 2019 01:03

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


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 3 guests