Page 1 of 1

Help with my mod

PostPosted: Sat Jul 21, 2012 04:54
by Menche
I'm making a mod that adds a node that can be pushed around, moving one block whenever it's punched. It seems to work, but a lot of the time the node moves two blocks over rather than one. I can't figure out why, can someone more experienced look at it? The block only moves if hit from one angle, I haven't done the others yet.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
minetest.register_node("pushblocks:pushstone", {
        description = "Test Pushstone",
        tiles = {"default_cobble.png"},
        is_ground_content = true,
        groups = {cracky=3},
})

minetest.register_on_punchnode(function(pos, node, puncher)
        if node.name == "pushblocks:pushstone" then
                if minetest.dir_to_facedir(puncher:get_look_dir()) == 0 then
                        pos.z = pos.z + 1
                        if minetest.env:get_node(pos).name == "air" then
                                minetest.env:add_node(pos,{name = "pushblocks:pushstone"})
                                pos.z = pos.z - 1
                                minetest.env:remove_node(pos)
                        end
                end
        end
end)


PostPosted: Sat Jul 21, 2012 05:40
by Jeija
The code is 100% working. The reason for that it moves 2 blocks sometimes is that you punch it twice (once before it moves, second time after it moved), although you don`t release the mouse button.
You could put some timer in or use on_dignode instead. Feel free to ask if you need help in coding that.

PostPosted: Sat Jul 21, 2012 10:05
by mauvebic
you have this bit in twice:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
pos.z = pos.z - 1


right here:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
------------------->                        pos.z = pos.z + 1
                        if minetest.env:get_node(pos).name == "air" then
                                minetest.env:add_node(pos,{name = "pushblocks:pushstone"})
------------------->                                pos.z = pos.z - 1


which would result in a pos.z decreasing by two

PostPosted: Sun Jul 22, 2012 07:39
by Jeija
No mauvebic, thats fine. One time it is +1, the other time -1.
I tried the mod and it was possible to push the moving thing just 1 block over.

PostPosted: Sun Jul 22, 2012 09:01
by cactuz_pl
I think this is due to that the game interpret click mouse button like hold mouse button, and "minetest.register_on_punchnode" counts twice or even triple, if you try click mouse button very shortly, node will move only one block length.

Try add some "wait" command, if is there any.

Edit: Jeija sorry I did not notice your post, which is about the same.