What is happening in screwdriver/init.lua?

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

What is happening in screwdriver/init.lua?

by sorcerykid » Post

I'm trying my hardest to override the default screwdriver behavior on a node with an on_rotate( ) callback, yet I want the screwdriver to actually rotate the node under certain conditions. No matter what I try, I can't get it to work.

Code: Select all

on_rotate = function(pos, node, player, mode, new_param2)
If defined, called when the node is rotated using the screwdriver (from minetest_game / screwdriver mod). Screwdriver only rotates facedir nodes.

Returns false to avoid screwdriver actually perform the rotation (if rotation is rejected/disallowed or if on_rotate performs the rotation itself). Returns true to let screwdriver perform the rotation.
  • pos — Position of the node that is being rotated.
  • node — Node definition of the node that is being rotated.
  • player — Reference to the Player who is using the screwdriver.
  • mode — Screwdriver mode (1=Rotate face, 2=Rotate axis)
  • new_param2 — New node param2 proposed by the screwdriver mod.
According to the documentation, if on_rotate( ) returns true, the screwdriver performs the usual action. But I've attempted returning both true and false, and the screwdriver still doesn't do anything. In fact, I've even deleted everything from the callback function, returning immediately with true and no luck.

Briefly perusing the code, it appears (and correct me if I'm wrong) that the screwdriver definitely short circuits on false. But something very strange seems to happen otherwise: the variable should_rotate is set to false. I'm frankly not sure what purpose this variable serves since the screwdriver routine short circuits on false, so why is should_rotate even set to false when the callback returns true?

https://github.com/minetest/minetest_ga ... t.lua#L106

Code: Select all

	if ndef.on_rotate then
		-- Copy pos and node because callback can modify it
		local result = ndef.on_rotate(vector.new(pos),
				{name = node.name, param1 = node.param1, param2 = node.param2},
				user, mode, new_param2)
		if result == false then -- Disallow rotation
			return itemstack
		elseif result == true then
			should_rotate = false
		end
	end
Clearly, I'm doing something wrong, or else I just don't understand screwdrivers. Could anyone please offer a hint?

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

Re: What is happening in screwdriver/init.lua?

by sofar » Post

The wiki contains incorrect information.

The official screwdriver API is part of minetest_game's game_api.txt:

https://github.com/minetest/minetest_ga ... i.txt#L497
* return value: false to disallow rotation, nil to keep default behaviour, true to allow
it but to indicate that changed[sic] have already been made (so the screwdriver will wear out)

parasite
Member
Posts: 186
Joined: Sat May 06, 2017 17:45
GitHub: Parasitoid

Re: What is happening in screwdriver/init.lua?

by parasite » Post

I have noticed that screwdriver doesn`t work in some of servers right now and also some of nodes rotated earlier now are not rotated. For example the chair from homedecor or chainsaw which were rotated to be like flipped over now looks normal.

But I have a question. I would like to ask if I am correctly understund it. According to API comment "To use it, add the `on_screwdriver` function to the node definition.". Does that "on_screwdriver / on_rotate" things is needed in any node definition to use screwdriver ingame with that node? Or it is not needed, and then screwdriver used with such node will act somehow default?

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

Re: What is happening in screwdriver/init.lua?

by sofar » Post

parasite wrote:Does that "on_screwdriver / on_rotate" things is needed in any node definition to use screwdriver ingame with that node? Or it is not needed, and then screwdriver used with such node will act somehow default?
The screwdriver can, and will, rotate a whole bunch of nodes automatically, based on the knowledge that it has. It's not perfect at doing this, but, in most regular cases, you don't need to even add this callback.

parasite
Member
Posts: 186
Joined: Sat May 06, 2017 17:45
GitHub: Parasitoid

Re: What is happening in screwdriver/init.lua?

by parasite » Post

sofar wrote:The screwdriver can, and will, rotate a whole bunch of nodes automatically, based on the knowledge that it has. It's not perfect at doing this, but, in most regular cases, you don't need to even add this callback.
So screwdriver works without that function callback and that is just an option which can be used if needed to modify default screwdriver settings. Thanks.

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

Re: What is happening in screwdriver/init.lua?

by sorcerykid » Post

sofar wrote:The wiki contains incorrect information.
Ahh, this makes more sense. I returned nil, and now all is working as expected.

Thanks for the link to the game API, as I didn't even know it existed. I've always been referencing the lua_api.txt. I appreciate the tips!

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 10 guests