Dragging objects around the scene

  • #1, by hella-Wednesday, 01. December 2021, 15:00 2 years ago
    Hello everyone, I am a bloody beginner with Visionaire so I hope you can help me.

    I would like to drag around objects on the scene. Let's say I have a stone that I want to drag with my left mouse button on hold to a different part of the scene and let go of it. So then the object was moved and stays where I put it.

    Furthermore, I would like to have some boundaries. So I can only drag the item in a specific area/ the item cannot leave the area.

    How would I set this up in Lua?

    Thanks in advance


    Newbie

    7 Posts


  • #2, by afrlmeWednesday, 01. December 2021, 15:07 2 years ago
    Do you have any screenshots or a diagram of how this puzzle is supposed to work?

    You said you are a bloody beginner with Visionaire... talk about jumping straight into the deep end - in regards to wanting to sort out something somewhat complex that will most likely involve scripting.

    Anyway, nice one on correctly typing Lua instead of LUA, & also welcome to the Visionaire Studio community. By the way, most of us are more active on the VS discord server, which you can find here. The only reason you are getting such a quick reply on here is that I just happened to be checking on the forum, just after you created your thread.

    Imperator

    7278 Posts

  • #3, by hella-Wednesday, 01. December 2021, 15:57 2 years ago
    Hey! Thanks for the fast reply. I attached a picture on what I want to achieve. It is only an example. I would like to use this dragging mechanism for other puzzles and objects, too.

    It's certainly bold to jump into the complex stuff right away. But I would like to try regardless.

    Newbie

    7 Posts

  • #4, by afrlmeWednesday, 01. December 2021, 16:16 2 years ago
    The simplest solution would probably be to hide the scene object when you click on it & then assign it as a dragged item. For the placing part you would probably need to use script to check if it's inside of a specific rectange/square area & then get the current position of the mouse & use that to specify where you want the actual object to be offset to.


    Even that isn't really all that simple - though it might be possible with minimal scripting if you create an object polygon that covers the area while that item is being dragged & then just check the position of the mouse cursor when clicked on & offset the object to that position.

    Quick tip: when you initially create the object don't touch it, just leave it in the top left corner of the scene. inside of an at begin of scene action check if offset is 0,0 & if it is then place it to where you want it to placed in the scene with Lua.


    Here's a quick example of getting & updating the offset (quick note: the reason I say to offset it inside of an at begin of scene action block is so you don't need to do any math calculations for absolute position placement, which you would need to do if you were to manually drag it into the correct starting position inside of the editor, as it offsets based on that position).

    if game.CurrentScene.Objects["example"].Offset.x == 0 and game.CurrentScene.Objects["example"].Offset.y == 0 then
    
    
    
      game.CurrentScene.Objects["example"].Offset = {x = 100, y = 100} --- move object to correct starting position
    
    
    
    end

    Imperator

    7278 Posts

  • #5, by hella-Wednesday, 01. December 2021, 18:20 2 years ago
    Your example script works like a charm.

    I'm still wondering if I can force the object to follow the cursor when a button is pressed instead of making it an item.
    I've set up a scene condition that becomes true when Left mouse button is pressed and becomes false when released. I've tried some coding but I am not familiar with the syntax yet. So bear with me and my faulty code.
    I'm sorry to ask but can you give me some more advice?



    local draggedPos
      draggedPos.x = getCursorPos().x + 100
      draggedPos.y = getCursorPos().y + 100

    if game.CurrentScene.SceneConditions["LM_pressed"].ConditionValue == true then

      setgame.CurrentScene.GameCurrentObject = {x = draggedPos.x, y = draggedPos.y }

    end


    Newbie

    7 Posts

  • #6, by afrlmeWednesday, 01. December 2021, 19:06 2 years ago
    You could register a mainLoop to handle it. You will need to unregister it when it's no longer needed by listening out for when mouse button is no longer being held. Maybe set a condition when you start dragging it & then check in the mouse button released actions if the condition is true & if it is then set it as false & unregister the event handler like in the second code example below.

    quick note: this example doesn't cover restricted boundaries.

    function drag_itm()
    
    
    
      game.CurrentScene.Objects["example"].Offset = { x = getCursorPos().x + 100, y = getCursorPos().y + 100 }
    
    
    
    end
    
    
    
    registerEventHandler("mainLoop", "drag_itm")


    unregisterEventHandler("mainLoop", "drag_itm")

    Imperator

    7278 Posts

  • #7, by hella-Wednesday, 01. December 2021, 21:18 2 years ago
    I managed to get the dragging and dropping to work. Eureka! grin

    The only issue I am running into now is that no matter where on the scene I press my left mouse button the example object appears at the offset of the cursor.
    game.CurrentScene.Objects["example"].Offset = { x = getCursorPos().x + 100, y = getCursorPos().y + 100 }
    But I only want to pick up an object that my cursor is above. However, if I type
    game.CurrentObject.Offset = { x = getCursorPos().x + 100, y = getCursorPos().y + 100 }

    I can only drag the object for a split second before its not the currentObject anymore due to the offset of the cursor.

    So how can I get the name of the currentObject and use it (maybe store it?) in a way so I can continue to drag that object around?



    Ps. after fixing this I will try tackling the boundary issue.

    Newbie

    7 Posts

  • #8, by afrlmeThursday, 02. December 2021, 13:48 2 years ago
    It's not really viable to just check for the object below the cursor because you don't know how fast the player will be moving their mouse around, or what dpi, etc. so it's possible that the player drags too fast & the cursor ends up shooting past the object & also you wouldn't be able to offset it either.

    My script example was meant for you to execute it inside of a mouse hold action belonging to the scene object itself. On successful execution of the action you should update a condition & execute the script to set the loop going. Then inside of the mouse button released section of the main mouse properties section in the editor you will check if that condition is true & if it is then you need to unregister that event handler using something like the second code example I provided in my previous post.

    In regards to boundaries, what exactly do you have in mind? Are they supposed to prevent the object from being moved past the boundaries, or if the player lets go of the mouse button outside of the boundaries is the object supposed to go back to the default position?

    Imperator

    7278 Posts

  • #9, by hella-Monday, 06. December 2021, 17:02 2 years ago
    It's not really viable to just check for the object below the cursor because you don't know how fast the player will be moving their mouse around, or what dpi, etc. so it's possible that the player drags too fast & the cursor ends up shooting past the object & also you wouldn't be able to offset it either.
    I get your point. However, pressing the left mouse button far away from the object and then having the object just appear/ jump to the cursor is not really the effect I was hoping to get. I was thinking to achieve a mechanism in which you can only pick up an object and drag it around if your cursor was above the object before you pressed the mouse button.
    In regards to boundaries, what exactly do you have in mind? Are they supposed to prevent the object from being moved past the boundaries, or if the player lets go of the mouse button outside of the boundaries is the object supposed to go back to the default position?

    Yes I was hoping to prevent the object from being moved past the boundary. In the example picture from the beginning you can see that I only want the object to be moved above a certain area/a book. I dont want the object to get past that.

    I was thinking that something like this might work for forcing the cursor and object to stay in a certain area, but it is clearly faulty.

    1. if Conditions.LM_pressed.ConditionValue == true and getCursorPos().x < {200} then
    2. setCursorPos.x = ({200})
    3. end

    Any tips for this one?
    I really appreaciate your help and I am sorry to need so much of it.



    Newbie

    7 Posts

  • #10, by SimonSMonday, 06. December 2021, 19:29 2 years ago
    function mainLoopCursor()
    if Conditions.LM_pressed.Value and getCursorPos().x < 200 then
      local pos = getCursorPos()
      pos.x = 200
      setCursorPos(pos)
    end
    end
    
    registerEventHandler("mainLoop", "mainLoopCursor")

    Thread Captain

    1580 Posts