[solved] Can you detect node and get its location

Post Reply
User avatar
Semmett9
Member
Posts: 157
Joined: Sun Oct 07, 2012 13:48
Location: Gallifrey
Contact:

[solved] Can you detect node and get its location

by Semmett9 » Post

I am making a mod which detects the node above it and if its air it replaces it with a node.

so lets say I have a node (default:dirt) at 0,0,0
when i hit this done (with on_punch) it checks the node at 0,1,0
After this it checks what the node at 0,1,0 and if its air it places a node there (default:dirt)

What i have tried'

Code: Select all

pos.y = pos.y + 1;
if minetest.find_node_near(
{x = pos.x, y = pos.y + height, z = pos.z }, 1, {"default:dirt"}) then
The code above works but you cannot put two nodes together since since it looks for the dirt in X, Y and Z

So if i place a node at 0,0,0 then hit it it will place a node at 0,1,0
However if i place a node at 0,0,1 it will detect the node at 0,0,0 and 0,1,0 and not place a node.


I have also the code shown below but it just crashes the game

Code: Select all

 if minetest.find_node_in_area(
{x = pos.x, y = pos.y, z = pos.z },
{x = pos.x, y = pos.y, z = pos.z }, 
{"scaffolding:scaffolding"}) then
If anyone knows how to solve the problem feel free to reply.

many thanks
Last edited by Semmett9 on Mon Oct 17, 2016 19:53, edited 1 time in total.

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Can you detect node and get its location

by rubenwardy » Post

Code: Select all

local target_pos = { x = pos.x, y = pos.y, z = pos.z }
target_pos.y = target_pos.y + 1
local node = minetest.get_node(target_pos)
will get the node above pos. you can then do:

Code: Select all

if node.name == "air" then
    minetest.set_node(target_pos, { name = "default:dirt" })
end
to replace the node above with dirt if it is air
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
Semmett9
Member
Posts: 157
Joined: Sun Oct 07, 2012 13:48
Location: Gallifrey
Contact:

Re: Can you detect node and get its location

by Semmett9 » Post

rubenwardy wrote:
Spoiler

Code: Select all

local target_pos = { x = pos.x, y = pos.y, z = pos.z }
target_pos.y = target_pos.y + 1
local node = minetest.get_node(target_pos)
will get the node above pos. you can then do:

Code: Select all

if node.name == "air" then
    minetest.set_node(target_pos, { name = "default:dirt" })
end
to replace the node above with dirt if it is air
Thanks for the help

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests