Log In  


function _init()
 poke(0x5f2d,1)
 mx,my=stat(32),stat(33)
 cursor_x,cursor_y=64,64
 nodes={}
 constant={n="constant",x=0,y=0,c=8}
 counter={n="counter",x=0,y=0,c=3}
 cam_x,cam_y=-60,-63

end

function _update60()

 if (btnp(🅾️)) new(constant)

 if (btn(⬆️)) cam_y-=1
 if (btn(⬇️)) cam_y+=1
 if (btn(➡️)) cam_x+=1
 if (btn(⬅️)) cam_x-=1

end

function new(n)
 add(nodes,n)
 n.x=cursor_x+cam_x
 n.y=cursor_y+cam_y
end

function _draw()
    chosen = nil
    cls(0)

    for i, node in ipairs(nodes) do
        print(node.n .. " " .. node.x .. " " .. node.y, 0, i * 6, node.c)
        rect(node.x - cam_x, node.y - cam_y, node.x + 8 - cam_x, node.y + 4 - cam_y, node.c)

        if stat(34) ~= 0 and 
           cursor_x >= node.x - cam_x and 
           cursor_x <= node.x + 8 - cam_x and 
           cursor_y >= node.y - cam_y and 
           cursor_y <= node.y + 4 - cam_y then
            chosen = node
        end
    end

    if stat(32) ~= mx or stat(33) ~= my then
        cursor_x = stat(32)
        cursor_y = stat(33)
    end

    if chosen ~= nil then
        print(chosen.x, 0, 0, 12)
        chosen.x = cursor_x + cam_x - 4
        chosen.y = cursor_y + cam_y - 2
    end

    pset(cursor_x + 1, cursor_y, 6)
    pset(cursor_x, cursor_y + 1, 6)
    pset(cursor_x - 1, cursor_y, 6)
    pset(cursor_x, cursor_y - 1, 6)
end

The goal: when you create a new node object, it should be at the cursor's position. You can drag them around. Press the circle button to create a new node. Use the arrow keys to move the camera. Use the mouse to drag nodes.

The problem: whenever a node is created or clicked, every single existing node's position moves to the mouse.

I'm sure it's some small detail I'm missing but I just can't find it. I would be very thankful for your help



1

It's because you're adding the same node ("constant") to the nodes table every time. When you set n.x and n.y in the node, everything you've added to the nodes table is pointing to that same variable, so they all update.

What you probably want to do instead is change your button press event handler to do this:

if btnp(🅾️) then
   local c={n="constant",x=0,y=0,c=8}
   new(c)
end

2

I do that mistake all the time :

In Lua,
numbers, string booleans and nil are passed by value in function calls
arrays/objects and functions are passed by reference.

When you call new(constant), you are adding a reference to constant into the nodes array.
All the references are pointing to your constant object.

function new(node)
 local copy
 copy={n=node.n,
       x=cursor_x+cam_x,
       y=cursor_y+cam_y,
       c=node.c}
 add(nodes,copy)
end

Thank you guys! It's fixed! I'm grateful for the help and now I can know this information for future projects.


Glad to help! Can you mark this post as "Resolved"? (click the little down arrow under the heart & star to the right of your post title and select "Mark as Resolved"). That will let people know you got the help you needed.



[Please log in to post a comment]