Tools events in mods

Post Reply
vladts
Member
Posts: 23
Joined: Tue Jan 31, 2017 23:08

Tools events in mods

by vladts » Post

Hello!

I have some question about tools. I have tool which displays some HUD. When on_use is called, I can modify this HUDs. But I didn't find callback for switching to other tool. But i need to hide HUD when the tool is deselected. How can i do this?

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Tools events in mods

by sofar » Post

vladts wrote:Hello!

I have some question about tools. I have tool which displays some HUD. When on_use is called, I can modify this HUDs. But I didn't find callback for switching to other tool. But i need to hide HUD when the tool is deselected. How can i do this?
there is no such callback, because switching hand items is mostly done client-side. You need to periodically check what item a client has wielded by inspecting the player through the various inventory methods instead.

User avatar
sorcerykid
Member
Posts: 1847
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Tools events in mods

by sorcerykid » Post

sofar wrote: there is no such callback, because switching hand items is mostly done client-side. You need to periodically check what item a client has wielded by inspecting the player through the various inventory methods instead.
If wield item changes are handled entirely client-side, then how does the API function minetest.get_wielded_item( ) work? I took a quick glance at src/network/serverpackethandler.cpp, and the server is clearly notified of player item events:

Code: Select all

void Server::handleCommand_PlayerItem(NetworkPacket* pkt)
{
        if (pkt->getSize() < 2)
                return;

        Player *player = m_env->getPlayer(pkt->getPeerId());
        if (player == NULL) {
                errorstream << "Server::ProcessData(): Canceling: "
                                "No player for peer_id=" << pkt->getPeerId()
                                << " disconnecting peer!" << std::endl;
                m_con.DisconnectPeer(pkt->getPeerId());
                return;
        }

        PlayerSAO *playersao = player->getPlayerSAO();
        if (playersao == NULL) {
                errorstream << "Server::ProcessData(): Canceling: "
                                "No player object for peer_id=" << pkt->getPeerId()
                                << " disconnecting peer!" << std::endl;
                m_con.DisconnectPeer(pkt->getPeerId());
                return;
        }

        u16 item;

        *pkt >> item;
        playersao->setWieldIndex(item);
}
Unless I missed something, a callback should therefore be extremely easy to implement in the engine. And it would undoubtedly streamline a number of mods (like 3d_armor and wield3d) that need to constantly poll this information.

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Tools events in mods

by sofar » Post

sorcerykid wrote: Unless I missed something, a callback should therefore be extremely easy to implement in the engine. And it would undoubtedly streamline a number of mods (like 3d_armor and wield3d) that need to constantly poll this information.
totally agreed.

https://github.com/minetest/minetest/issues/1377

User avatar
sorcerykid
Member
Posts: 1847
Joined: Fri Aug 26, 2016 15:36
GitHub: sorcerykid
In-game: Nemo
Location: Illinois, USA

Re: Tools events in mods

by sorcerykid » Post

Ah thanks for that link :D After endless searching of Google, for some reason it never turned up that issue.

I'll give it a read, and see if maybe I could help or contribute in some way to get this rolling!

sofar
Developer
Posts: 2146
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: Tools events in mods

by sofar » Post

sorcerykid wrote:Ah thanks for that link :D After endless searching of Google, for some reason it never turned up that issue.

I'll give it a read, and see if maybe I could help or contribute in some way to get this rolling!
there's a link to a PR in that thread that's currently pending. I've expressed my support for it, it's fairly trivial, so, let's hope it gets merged.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests