Place a block with LUA

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

Place a block with LUA

by webdesigner97 » Fri Jan 04, 2013 10:34

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
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Fri Jan 04, 2013 12:28

 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Fri Jan 04, 2013 16:40

Well, i had some trouble the the add_node() function. But now I found an explanation at the Developer Wiki. Thanks Adam! :)
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Fri Jan 04, 2013 16:49

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?
 

User avatar
PilzAdam
Member
 
Posts: 4026
Joined: Fri Jul 20, 2012 16:19
GitHub: PilzAdam
IRC: PilzAdam

by PilzAdam » Fri Jan 04, 2013 16:52

 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Fri Jan 04, 2013 17:05

PilzAdam wrote:Your parameter for on_place are wrong: https://github.com/celeron55/minetest/blob/master/doc/lua_api.txt#L1391

Thanks :)
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Fri Jan 04, 2013 17:12

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
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Sat Jan 05, 2013 09:19

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
 

jin_xi
Member
 
Posts: 165
Joined: Mon Jul 02, 2012 18:19

by jin_xi » Sat Jan 05, 2013 09:55

use inventory:get_size("main"). note the :
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

by kaeza » Sat Jan 05, 2013 10:10

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.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Sat Jan 05, 2013 10:18

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
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Sat Jan 05, 2013 10:30

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?
 

User avatar
webdesigner97
Member
 
Posts: 1307
Joined: Mon Jul 30, 2012 19:16
GitHub: webD97
IRC: webdesigner97
In-game: webdesigner97

by webdesigner97 » Sat Jan 05, 2013 10:33

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
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 11 guests

cron