Moving Node - code help
Moving Node - code help
What is the best way to hard code a node to move from know to know pos(X Y Z)? Anyone have any code examples??
-
- Member
- Posts: 155
- Joined: Mon Dec 28, 2020 01:19
- GitHub: loosewheel
- In-game: loosewheel
Re: Moving Node - code help
I’ve made a moving node by removing it from one position (with minetest.remove_node) and placing it at the new position (with minetest.add_node). It doesn’t so much move, but is destroyed and recreated. If there is anything customised with the node, such as metadata, read it out of the one being destroyed and write it into the new one. Handle orientation or anything else, not handled by the node’s handlers.
What you need to do with the transfer of data depends on what the node does.
Code: Select all
local node = minetest.get_node_or_nil (old_pos)
if node then
local meta = minetest.get_meta (old_pos)
local inv = meta:get_inventory ()
-- read whatever
minetest.remove_node (old_pos)
minetest.add_node (new_pos, node)
meta = minetest.get_meta (new_pos)
inv = meta:get_inventory ()
-- write whatever
end
Re: Moving Node - code help
Thanks for the reply, need to create a sense of movement with some sort of delay between remove / add...that's where I am lost...
-
- Member
- Posts: 155
- Joined: Mon Dec 28, 2020 01:19
- GitHub: loosewheel
- In-game: loosewheel
Re: Moving Node - code help
You can use the node's timer, and handle the action from the node's on_timer handler. Because you're destroying the node at each movement, call
minetest.get_node_timer (old_pos):stop ()
before
minetest.remove_node (old_pos)
and then
minetest.get_node_timer (new_pos):start (NEXT_INTERVAL)
after
minetest.add_node (new_pos, node)
minetest.get_node_timer (old_pos):stop ()
before
minetest.remove_node (old_pos)
and then
minetest.get_node_timer (new_pos):start (NEXT_INTERVAL)
after
minetest.add_node (new_pos, node)
Re: Moving Node - code help
code works, well minus a few caveats, 1 - once you leave the world and go back in the code doesn't run, 2 - if the set nodes X Y Z is in your view the set timer doesn't set the node.
seems to almost do what I need...
on_timer = function(pos, elapsed)
minetest.set_node(pos, { name = "air", param2 = minetest.get_node(pos).param2 })
minetest.set_node({x=174, y=1, z=347}, { name = "neontraffic:cara", param2 = minetest.get_node(pos).param2 })
end,
on_construct = function(pos, self, staticdata)
timer = minetest.get_node_timer(pos)
timer:start(10)
minetest.after(.2,
function(self)
minetest.set_node(pos, { name = "air", param2 = minetest.get_node(pos).param2 })
minetest.set_node({x=pos.x, y=pos.y, z=pos.z-1}, {name = "neontraffic:cara" })
end,
self)
end,
https://youtu.be/cZIQwbYsVos
seems to almost do what I need...
on_timer = function(pos, elapsed)
minetest.set_node(pos, { name = "air", param2 = minetest.get_node(pos).param2 })
minetest.set_node({x=174, y=1, z=347}, { name = "neontraffic:cara", param2 = minetest.get_node(pos).param2 })
end,
on_construct = function(pos, self, staticdata)
timer = minetest.get_node_timer(pos)
timer:start(10)
minetest.after(.2,
function(self)
minetest.set_node(pos, { name = "air", param2 = minetest.get_node(pos).param2 })
minetest.set_node({x=pos.x, y=pos.y, z=pos.z-1}, {name = "neontraffic:cara" })
end,
self)
end,
https://youtu.be/cZIQwbYsVos
- sirrobzeroone
- Member
- Posts: 593
- Joined: Mon Jul 16, 2018 07:56
- GitHub: sirrobzeroone
- Contact:
Re: Moving Node - code help
Hi Darthon,
Have you seen this mod? It might help with some of the issues or maybe not, still thought it mgiht be worth pointing you at it:
viewtopic.php?t=17522
Cya
Rob
Have you seen this mod? It might help with some of the issues or maybe not, still thought it mgiht be worth pointing you at it:
viewtopic.php?t=17522
Cya
Rob
-
- Member
- Posts: 155
- Joined: Mon Dec 28, 2020 01:19
- GitHub: loosewheel
- In-game: loosewheel
Re: Moving Node - code help
This worked for me:
Re-entering to world, and moving out or range and returning.
Something you may need to consider, depending on what your needs are. Nodes are only active within a certain distance from the player. This is a game setting so depends on the setting. For me it's only about 30 nodes from the player. If you need your mod node to be active beyond that, you may need to force load the block. Because the node is moving this can be a little complex. Even if moving only one node away, it may be in a different block. If minetest.get_node_or_nil returns nil, you can load that position. Free the forced loading of the old position, and force load the new one.
There is an example for ensuring a position is loaded here.
If your code is in a function that operates the node (not directly in on_timer), on_timer can always return true, and you can start and stop the timers as necessary with minetest.get_node_timer.
Code: Select all
minetest.register_node("foobar:foobar", {
description = "Foo Bar",
tiles = { "foobar.png" },
drawtype = "normal",
node_box = { type = "regular" },
groups = { cracky = 2, oddly_breakable_by_hand = 2 },
on_construct = function (pos)
minetest.get_node_timer (pos):start (1.0)
end,
on_destruct = function (pos)
-- timer should only run once (returned false from on_timer)
-- and may be destroyed with node, but just in case
minetest.get_node_timer (pos):stop ()
end,
on_timer = function (pos, elapsed)
local node = minetest.get_node_or_nil (pos)
if node then
local new_pos = { x = pos.x + 1, y = pos.y, z = pos.z }
minetest.remove_node (pos)
minetest.add_node (new_pos, node)
end
-- don't run again
return false
end,
})
Something you may need to consider, depending on what your needs are. Nodes are only active within a certain distance from the player. This is a game setting so depends on the setting. For me it's only about 30 nodes from the player. If you need your mod node to be active beyond that, you may need to force load the block. Because the node is moving this can be a little complex. Even if moving only one node away, it may be in a different block. If minetest.get_node_or_nil returns nil, you can load that position. Free the forced loading of the old position, and force load the new one.
There is an example for ensuring a position is loaded here.
If your code is in a function that operates the node (not directly in on_timer), on_timer can always return true, and you can start and stop the timers as necessary with minetest.get_node_timer.
Re: Moving Node - code help
I have moved to an entity for the improvement in animation and speed control, how ever the initial spawn is still handled by a static node with a timer, I may still need a way to load the node out of range(if possible)...
\/ cyber car as entity
https://youtu.be/0o2AhAim3xA
\/ cyber car as entity
https://youtu.be/0o2AhAim3xA
Re: Moving Node - code help
Neither nodes nor entities can activate outside active block range, except if you forceload some blocks, but that's somewhat impractical.
But the game area looks quite small and there are probably few ABMs if any, so you could probably experiment with extending the range instead. The default is 3 blocks.
Re: Moving Node - code help
the city with-in The Grid is sort of large(and may grow) 1500x1500, I would love to have traffic and NPCs up and down the streets for a real active city feel.
here is some of the city(in early build phase)
https://www.youtube.com/watch?v=0ebic_1dyB8&t=428s
here is some of the city(in early build phase)
https://www.youtube.com/watch?v=0ebic_1dyB8&t=428s
Re: Moving Node - code help
If you need traffic up and down, the only way I know is to increase active_block_range gradually to see how much it can handle.
Re: Moving Node - code help
advanced trains use a cool system to workaround this.Termos wrote: ↑Tue Mar 09, 2021 13:04Neither nodes nor entities can activate outside active block range, except if you forceload some blocks, but that's somewhat impractical.
But the game area looks quite small and there are probably few ABMs if any, so you could probably experiment with extending the range instead. The default is 3 blocks.
Who is online
Users browsing this forum: No registered users and 12 guests