Moving Node - code help

Post Reply
Darthon12
Member
Posts: 26
Joined: Thu Nov 21, 2019 19:38

Moving Node - code help

by Darthon12 » Post

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??

loosewheel
Member
Posts: 155
Joined: Mon Dec 28, 2020 01:19
GitHub: loosewheel
In-game: loosewheel

Re: Moving Node - code help

by loosewheel » Post

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.

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
What you need to do with the transfer of data depends on what the node does.

Darthon12
Member
Posts: 26
Joined: Thu Nov 21, 2019 19:38

Re: Moving Node - code help

by Darthon12 » Post

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...

loosewheel
Member
Posts: 155
Joined: Mon Dec 28, 2020 01:19
GitHub: loosewheel
In-game: loosewheel

Re: Moving Node - code help

by loosewheel » Post

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)

Darthon12
Member
Posts: 26
Joined: Thu Nov 21, 2019 19:38

Re: Moving Node - code help

by Darthon12 » Post

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

User avatar
sirrobzeroone
Member
Posts: 593
Joined: Mon Jul 16, 2018 07:56
GitHub: sirrobzeroone
Contact:

Re: Moving Node - code help

by sirrobzeroone » Post

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

loosewheel
Member
Posts: 155
Joined: Mon Dec 28, 2020 01:19
GitHub: loosewheel
In-game: loosewheel

Re: Moving Node - code help

by loosewheel » Post

This worked for me:

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,
})
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.

Darthon12
Member
Posts: 26
Joined: Thu Nov 21, 2019 19:38

Re: Moving Node - code help

by Darthon12 » Post

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

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

Re: Moving Node - code help

by Termos » Post

Darthon12 wrote:
Mon Mar 08, 2021 23:41
I may still need a way to load the node out of range(if possible)...
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.

Darthon12
Member
Posts: 26
Joined: Thu Nov 21, 2019 19:38

Re: Moving Node - code help

by Darthon12 » Post

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

Termos
Member
Posts: 417
Joined: Sun Dec 16, 2018 12:50

Re: Moving Node - code help

by Termos » Post

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.

User avatar
runs
Member
Posts: 3225
Joined: Sat Oct 27, 2018 08:32

Re: Moving Node - code help

by runs » Post

Termos wrote:
Tue Mar 09, 2021 13:04
Darthon12 wrote:
Mon Mar 08, 2021 23:41
I may still need a way to load the node out of range(if possible)...
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.
advanced trains use a cool system to workaround this.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests