Re: Post your modding questions here
Posted: Tue Feb 14, 2017 04:02
You can't. Combine the code into one function.GreenDimond wrote:I seem to be having trouble with registering 2 'on_punch' to the same node.
The official Minetest discussion board
https://forum.minetest.net/
You can't. Combine the code into one function.GreenDimond wrote:I seem to be having trouble with registering 2 'on_punch' to the same node.
I have realized this, but now I am having trouble with that aswell. I will post here if I cant fix.sofar wrote:You can't. Combine the code into one function.GreenDimond wrote:I seem to be having trouble with registering 2 'on_punch' to the same node.
Code: Select all
get_item = function ()
-- function(pos, node, player, pointed_thing)
local inv = player:get_inventory()
inv:add_item("main", "modname:nodedame")
end,
replace_model = function ()
-- function(pos, node, puncher, pointed_thing)
minetest.set_node(pos, {name = "modname:othernodename", param2 = node.param2})
end,
do_punch = function do_punch()
get_item()
replace_model()
end,
on_punch = do_punch
Code: Select all
get_item = function (player)
-- function(pos, node, player, pointed_thing)
local inv = player:get_inventory()
inv:add_item("main", "modname:nodename")
end,
replace_model = function (pos, node)
-- function(pos, node, puncher, pointed_thing)
minetest.set_node(pos, {name = "modname:othernodename", param2 = node.param2})
end,
on_punch = function (pos, node, player, pointed_thing)
node.get_item(player)
node.replace_model(pos, node)
end
})
Code: Select all
2017-02-13 21:02:12: ERROR[Main]: ServerError: Lua: Runtime error from mod 'modname' in callback node_on_punch(): /home/alex/.minetest/mods/modname/nodes.lua:68: attempt to call field 'get_item' (a nil value)
2017-02-13 21:02:12: ERROR[Main]: stack traceback:
2017-02-13 21:02:12: ERROR[Main]: /home/alex/.minetest/mods/modname/nodes.lua:68: in function </home/alex/.minetest/mods/modname/nodes.lua:67>
Code: Select all
local target_yaw = yaw+math.pi+math.pi/2
local entity_yaw = entity.object:getyaw()
if target_yaw <= 6.2 and target_yaw >= 0.2 then --this is the only method I have to stop the problem
entity.object:setyaw(entity_yaw+(target_yaw-entity_yaw)/4)
else
entity.object:setyaw(target_yaw)
end
Code: Select all
-- before the node definition, make these as local functions
local function get_item(player)
local inv = player:get_inventory()
inv:add_item("main", "modname:nodename")
end
local function replace_model(pos, node)
-- function(pos, node, puncher, pointed_thing)
minetest.set_node(pos, {name = "modname:othernodename", param2 = node.param2})
end
minetest.register_node("name",{
-- ...
on_punch = function (pos, node, player, pointed_thing)
get_item(player)
replace_model(pos, node)
end
-- ...
})
I have a function advtrains.minAngleDiffRad in advtrains/helpers.lua. maybe this is helpful.D00Med wrote:I have a maths related question.
I can make an entity rotate so that it matches the player's yaw. Now I'm trying to make the entity 'catch-up' to the player's yaw. There's a problem with this though. When the player's yaw crosses 0/2pi the difference between the player's yaw and the entity's yaw makes the entity spin around in the opposite direction(the long way around).
At the moment the rotation of the entity is divided into four steps. The higher the number of steps it takes to catch up, the more noticeable the problem is.
^ yaw is the player's yawCode: Select all
local target_yaw = yaw+math.pi+math.pi/2 local entity_yaw = entity.object:getyaw() if target_yaw <= 6.2 and target_yaw >= 0.2 then --this is the only method I have to stop the problem entity.object:setyaw(entity_yaw+(target_yaw-entity_yaw)/4) else entity.object:setyaw(target_yaw) end
How can I do this so that the shortest difference is used?
@GreenDimond:
Have you tried just using get_item and replace_model rather than node.get_item and node.replace_model?
Code: Select all
get_item = function(pos, node, player)
local inv = player:get_inventory()
inv:add_item("main", "modname:nodename")
end,
replace_model = function(pos, node)
minetest.set_node(pos, {name = "modname:othernodename", param2 = node.param2})
end,
on_punch = function(pos, node, player, pointed_thing)
node.get_item(player)
node.replace_model(pos, node)
end
})
Code: Select all
2017-02-14 08:16:04: ERROR[Main]: ServerError: Lua: Runtime error from mod 'modname' in callback node_on_punch(): /home/alex/.minetest/mods/modname/nodes.lua:66: attempt to call field 'get_item' (a nil value)
2017-02-14 08:16:04: ERROR[Main]: stack traceback:
2017-02-14 08:16:04: ERROR[Main]: /home/alex/.minetest/mods/modname/nodes.lua:66: in function </home/alex/.minetest/mods/modname/nodes.lua:65>
Code: Select all
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local pos = pointed_thing.under
local pname = user:get_player_name()
if minetest.is_protected(pos, pname) then
minetest.record_protection_violation(pos, pname)
return
end
local node = minetest.get_node(pos).name
if node == "modename:node1" then
-- minetest.swap_node(pos, {name = "modename:node2"})
minetest.set_node(pos, {name = "modename:node2", param2 = node.param2})
return itemstack
end
end,
No not exactly. The .name went elsewhere. IRC not today.GreenDimond wrote:Is that not the code I currently have?
We could talk about this in IRC if you like (#minetest).
I see...trying this.orwell wrote:No not exactly. The .name went elsewhere. IRC not today.GreenDimond wrote:Is that not the code I currently have?
We could talk about this in IRC if you like (#minetest).
Code: Select all
function() replace_donemodel(pos, node)
minetest.set_node(pos, {name = "modname:nodename", param2 = node.param2})
end,
on_place = {
minetest.after(2.0, replace_donemodel)}
GreenDimond wrote:This code lets the game start and actually does something when I punch the node. My game crashes.I think I am about 90% of the way there, but am now moving as fast as a brick. So wat do now?Code: Select all
get_item = function (player) -- function(pos, node, player, pointed_thing) local inv = player:get_inventory() inv:add_item("main", "modname:nodename") end, replace_model = function (pos, node) -- function(pos, node, puncher, pointed_thing) minetest.set_node(pos, {name = "modname:othernodename", param2 = node.param2}) end, on_punch = function (pos, node, player, pointed_thing) node.get_item(player) node.replace_model(pos, node) end })
error code:In case you are wondering, I use "modname" and "nodename" and stuff to keep the mod a secret :PCode: Select all
2017-02-13 21:02:12: ERROR[Main]: ServerError: Lua: Runtime error from mod 'modname' in callback node_on_punch(): /home/alex/.minetest/mods/modname/nodes.lua:68: attempt to call field 'get_item' (a nil value) 2017-02-13 21:02:12: ERROR[Main]: stack traceback: 2017-02-13 21:02:12: ERROR[Main]: /home/alex/.minetest/mods/modname/nodes.lua:68: in function </home/alex/.minetest/mods/modname/nodes.lua:67>
tell us the fix - this way everyone learns from the mistake!GreenDimond wrote:Functions hate me :(
I am trying to get a simple function to work, and it doesn't.This is inside a register_node.Code: Select all
function() replace_donemodel(pos, node) minetest.set_node(pos, {name = "modname:nodename", param2 = node.param2}) end, on_place = { minetest.after(2.0, replace_donemodel)}
I want the model to be replaced 2 seconds after it is placed (or set).
I have tried registering the function outside the register node aswell, but nothing works. What do?
EDIT: Fixed.
This was the fix:GreenDimond wrote:Functions hate me :(
I am trying to get a simple function to work, and it doesn't.This is inside a register_node.Code: Select all
function() replace_donemodel(pos, node) minetest.set_node(pos, {name = "modname:nodename", param2 = node.param2}) end, on_place = { minetest.after(2.0, replace_donemodel)}
I want the model to be replaced 2 seconds after it is placed (or set).
I have tried registering the function outside the register node aswell, but nothing works. What do?
EDIT: Fixed.
Code: Select all
-- in definition of node A
on_punch = function(pos, node, puncher, pointed_thing)
minetest.set_node(pos, { name = "nodeBName", param2 = node.param2 })
minetest.after(2, minetest.set_node, pos, { name = "nodeCName", param2 = node.param2 })
end,
Code: Select all
local function replace_donemodel(pos, node)
minetest.set_node(pos, {name = "waffles:wafflemaker_open_done", param2 = node.param2})
end
minetest.register_node("node:name", {
after_place_node = function(pos, placer, itemstack, pointed_thing)
local node = minetest.get_node(pos)
minetest.after(2.0, replace_donemodel, pos, node)
end
})
Thanks, this improves it a lot. The problem still occurs a little, but it's not as commonorwell wrote: I have a function advtrains.minAngleDiffRad in advtrains/helpers.lua. maybe this is helpful.
Returns how far to rotate from first angle in which direction to reach second angle
Link is in signature
There's no access to camera, but you can get the player's position and add the eye offset:Nyarg wrote:A little question.
I need something like get_camera_pos() but nothing found.
Is there a simple way to get it ?
Code: Select all
local pos = player:getpos()
pos.y = pos.y + 1.625
The server does not keep track of particles (or their positions), and if you look at the specification of add_particle, you will notice that it does not return an id. If you want something you can keep track of the position of, I would use an entity.googol wrote:Hi experts.
How i can get (pos) of the particles in time.
Is the some function for example?
minetest.get particle(id)
return:
pos - position of particle
meta - meta of particle
------------
P.S. If this is not. Please write the post in issues GitHub.
It is very necessary.
Thank.