Page 1 of 1

Place a block with LUA

PostPosted: Fri Jan 04, 2013 10:34
by webdesigner97
Hello,
I'd like to place a block with the API. I read, that i need something like set_node(). But what do the params of set_node look like? The block is a nodebox, so it needs to "facedir".
Here's all code i currently have:

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
(..)
on_place = function ()
   
    end

PostPosted: Fri Jan 04, 2013 12:28
by PilzAdam

PostPosted: Fri Jan 04, 2013 16:40
by webdesigner97
Well, i had some trouble the the add_node() function. But now I found an explanation at the Developer Wiki. Thanks Adam! :)

PostPosted: Fri Jan 04, 2013 16:49
by webdesigner97
Well, but i crashes:

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
on_place = function (pos)
        pos.y = pos.y+1;
        minetest.env:add_node(pos, {name="streets:pole"})
    end


Message:

ServerError: LuaError: error: ...t-0.4.4-win32\bin\..\mods\minetest\streets/nodes.lua:231: attempt to perform arithmetic on field 'y' (a nil value)

Why is "pos.y" a nil value?

PostPosted: Fri Jan 04, 2013 16:52
by PilzAdam

PostPosted: Fri Jan 04, 2013 17:05
by webdesigner97
PilzAdam wrote:Your parameter for on_place are wrong: https://github.com/celeron55/minetest/blob/master/doc/lua_api.txt#L1391

Thanks :)

PostPosted: Fri Jan 04, 2013 17:12
by webdesigner97
Finally it works:

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
on_place = function (itemstack, placer, pointed_thing)
        if pointed_thing.type == "node" then
            local pos = pointed_thing.above
            minetest.env:add_node(pos, {name="streets:pole"})
            pos.y = pos.y+1
            minetest.env:add_node(pos, {name="streets:pole"})
        end
    end

PostPosted: Sat Jan 05, 2013 09:19
by webdesigner97
How can i access the inventory of the placer? The dev wiki says
get_size("listname")


My 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
local inventory =  placer:get_inventory()
print(inventory.get_size("main"))


Error:

bad argument #1 to "get_size" userdata expected, got string

o.O

PostPosted: Sat Jan 05, 2013 09:55
by jin_xi
use inventory:get_size("main"). note the :

PostPosted: Sat Jan 05, 2013 10:10
by kaeza
From the Lua Manual (section 2.5.8 Function Calls):
A call v:name(args) is syntactic sugar for v.name(v,args), except that v is evaluated only once.

PostPosted: Sat Jan 05, 2013 10:18
by webdesigner97
jin_xi wrote:use inventory:get_size("main"). note the :

Thanks, now it works..... The worst errors are only the little ones you can't see :D

PostPosted: Sat Jan 05, 2013 10:30
by webdesigner97
But why does

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
function (itemstack, placer, pointed_thing)
     (...)
     itemstack:take_item(1)
end

not work?

PostPosted: Sat Jan 05, 2013 10:33
by webdesigner97
webdesigner97 wrote:But why does

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
function (itemstack, placer, pointed_thing)
     (...)
     itemstack:take_item(1)
end

not work?

Found out:

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
function (itemstack, placer, pointed_thing)
     (...)
     itemstack:take_item(1)
     return itemstack
end