serialize node position

Gronx
Member
 
Posts: 10
Joined: Fri Oct 11, 2013 14:09

serialize node position

by Gronx » Thu Oct 31, 2013 09:22

I'm trying to store the position of a node so that it can be destroyed when a "parent" node is destroyed. The metadata appears to come back as null but I have no idea why.

Here's the code

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("schematics:p1", {
    description = "Schematic point 1",
    tiles = {"p1_top.png", "p1_bottom.png", "p1_left.png", "p1_right.png", "p1_front.png", "p1_back.png", },
    paramtype2 = "facedir",
    groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
})

minetest.register_node("schematics:p2", {
    description = "Schematic point 2",
    tiles = {"p2_top.png", "p2_bottom.png", "p2_left.png", "p2_right.png", "p2_front.png", "p2_back.png", },
    paramtype2 = "facedir",
    groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
})

minetest.register_node("schematics:writer", {
    description = "Schematic writer",
    tiles = {"save_block.png"},
    paramtype2 = "facedir",
    groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
    on_construct = function(pos)
        --local n = minetest.get_node(pos)
        local meta = minetest.get_meta(pos)
        meta:set_string("formspec", "field[text;;${text}]")
        meta:set_string("infotext", """")
    end,
    on_receive_fields = function(pos, formname, fields, sender)
        --print("Sign at "..minetest.pos_to_string(pos).." got "..dump(fields))
        local meta = minetest.get_meta(pos)
        fields.text = fields.text or ""
        print((sender:get_player_name() or "").." wrote ""..fields.text.."" to sign at "..minetest.pos_to_string(pos))
        meta:set_string("text", fields.text)
        meta:set_string("infotext", '"'..fields.text..'"')
    end
})

local on_place_writer = function(pos, node, placer)
    if node.name == "schematics:writer"then
        local lookDir = placer:get_look_dir()
        local pos1, pos2
        if math.abs(lookDir.x) < math.abs(lookDir.z) then
            pos1 = { x = pos.x+1, y = pos.y, z = pos.z }
            pos2 = { x = pos.x-1, y = pos.y, z = pos.z }
        else
            pos1 = { x = pos.x, y = pos.y, z = pos.z+1 }
            pos2 = { x = pos.x, y = pos.y, z = pos.z-1 }
        end
        minetest.env:set_node(pos1, {name = "schematics:p1"})
        minetest.env:set_node(pos2, {name = "schematics:p2"})
        local meta = minetest.get_meta(pos)
        meta:set_string("pos1", minetest.serialize(pos1))
        meta:set_string("pos2", minetest.serialize(pos2))
   end
end

local on_place_point = function(pos, node, placer)
    if node.name == "schematics:p1" or node.name == "schematics:p1" then
        local lookDir = placer:get_look_dir()
        local pos1, pos2
        if math.abs(lookDir.x) < math.abs(lookDir.z) then
            pos1 = { x = pos.x+1, y = pos.y, z = pos.z }
            pos2 = { x = pos.x-1, y = pos.y, z = pos.z }
        else
            pos1 = { x = pos.x, y = pos.y, z = pos.z+1 }
            pos2 = { x = pos.x, y = pos.y, z = pos.z-1 }
        end
   end
end

local on_dig_writer = function(pos, node, placer)
    if node.name == "schematics:writer"then
        local meta = minetest.get_meta(pos)
        local pos1, pos2
        pos1 = minetest.deserialize(meta:get_string("pos1"))
        pos2 = minetest.deserialize(meta:get_string("pos2"))
        minetest.remove_node(pos1)
        minetest.remove_node(pos2)
   end
end

minetest.register_on_placenode(on_place_writer)
minetest.register_on_dignode(on_dig_writer)
minetest.register_on_placenode(on_place_point)






Here's the error code.

ServerError: LuaError: error: ...poon/minetest-master/bin/../mods/schematics/init.lua:77: bad argument #1 to 'remove_node' (table expected, got nil)



This is the line it's having difficulty with.

pos1 = minetest.deserialize(meta:get_string("pos1"))
 

User avatar
minermoder27
Member
 
Posts: 127
Joined: Wed Nov 20, 2013 23:24
GitHub: ZNixian
In-game: minermoder27

by minermoder27 » Mon Dec 09, 2013 08:49

Why not use minetest.pos_to_string() and minetest.string_to_pos()?
My best mods:
Buildtest
 


Return to WIP Mods

Who is online

Users browsing this forum: Bing [Bot] and 11 guests

cron