[SOLVED] getCursorPos() and moveObj()

  • #1, by andy-rinaldiSunday, 21. September 2014, 23:00 10 years ago
    Hi,
    I have to use the cursor screen coordinates (x, y) in order to move an object to this position.

    I put this function as definition script:

    function moveObj(obj, x, y, delay, easing)
    obj = getObject("0016dt_on_nav_device.SceneObjects["XY_space_img"]")
    startObjectTween(obj, VObjectOffset, obj:getPoint(VObjectOffset), {x = x, y = y}, delay, easing)
    end
    


    Then in the hotspot Action tab, on the left click event:

    local curPos = getCursorPos()
    moveObj("XY_space_img", curPos.x, curPos.y, 5000, easeQuintOut)
    


    but it's no works.

    I'm not able to find useful info about these functions in google.

    Any idea?

    Forum Fan

    160 Posts


  • #2, by AlexSunday, 21. September 2014, 23:38 10 years ago
    Hi,

    there are many errors in your script. first you pass a string to the moveObj function which is not used at all. The getObject call ist also wrong. And you don't want to change the offset to cursor pos - instead it should be the distance to the original object position. finally it could be something more like this:
    left click action:
    local curPos = getCursorPos()
    local obj = getObject('Objects[XY_space_img]')
    moveObj(obj, curPos.x, curPos.y, 5000, easeQuintOut)
    

    definition script:
    function moveObj(obj, x, y, delay, easing)
      local objPos = obj:getPoint(VObjectPosition)
      startObjectTween(obj, VObjectOffset, obj:getPoint(VObjectOffset), {x = x - objPos.x, y = y - objPos.y}, delay, easing)
    end
    


    I tested it successfully for the demogame (with different object name) so this works. The wiki documentation for startObjectTween is not uploaded yet, we'll update it soon.

    you should check the log for errors, in your case there were probably errors shown in the log. And use the print function to query the status of your variables, objects etc. if you're not sure.

    Great Poster

    378 Posts

  • #3, by SimonSSunday, 21. September 2014, 23:38 10 years ago
    Watch your log file.
    The line
    obj = getObject("0016dt_on_nav_device.SceneObjects["XY_space_img"]")
    has errors, you can't use " without escaping in strings, escape like this: \"
    The rest seems okay.

    Thread Captain

    1581 Posts

  • #4, by afrlmeSunday, 21. September 2014, 23:41 10 years ago
    Why would you find anything in google about them?

    getCursorPos() is an exclusive visionaire studio function which returns the current x/y coordinates of the mouse cursor & moveObj() is a workflow function I wrote that simulates the move object action part, but with more control.

    I believe the problem is this line right here...
    obj = getObject("0016dt_on_nav_device.SceneObjects["XY_space_img"]")
    

    you should have left it as it was. the function does not need editing.

    Imperator

    7278 Posts

  • #5, by afrlmeMonday, 22. September 2014, 00:06 10 years ago
    add this as a definition script...
    function moveObj(obj, x, y, delay, easing)
     obj = getObject("Game.GameCurrentScene.SceneObjects[" .. obj .. "]")
     startObjectTween(obj, VObjectOffset, obj:getPoint(VObjectOffset), {x = x, y = y}, delay, easing)
    end
    

    ...to do what you were after, create an execute a script action containing...
    moveObj("XY_space_img", getCursorPos().x, getCursorPos().y, 5000, easeQuintOut) -- should work
    

    You should really only move stuff on the current active screen but if you want to move stuff on specific screens then you could rewrite the moveObj() function a little bit like so...
    function moveObj(obj, scn, x, y, delay, easing)
     obj = getObject("Scenes[" .. scn .. "].SceneObjects[" .. obj .. "]")
     startObjectTween(obj, VObjectOffset, obj:getPoint(VObjectOffset), {x = x, y = y}, delay, easing)
    end
    

    to use...
    moveObj("object_name", "scene_name", 300, 500, 5000, easeQuintOut) -- object & scene names are case sensitive
    

    ...hope this helps.

    http://wiki.visionaire-tracker.net/wiki/MoveObj_%28CMS%29 / http://wiki.visionaire-tracker.net/wiki/GetCursorPos_%28CMS%29 -- all the information you need on the 2 functions.

    Edit: @ Alex: why does he need to get current position & - whatever from whatever? I tested the workflow function multiple times before adding it to the wiki & it worked as it was supposed to work.

    Imperator

    7278 Posts

  • #6, by andy-rinaldiMonday, 22. September 2014, 00:19 10 years ago
    I was confusing. Thanks for the answers, the Alex solution is working but the object position is incorrect.
    I want move the object inside the same hotspot where I click.

    @AFRLme: the getCursorPos() is a lua function of the input library.
    http://gmodwiki.net/Lua/Libraries/input/GetCursorPos
    instead I found the moveObj in your wiki.

    Forum Fan

    160 Posts

  • #7, by afrlmeMonday, 22. September 2014, 00:34 10 years ago
    The page where you found moveObj() workflow function is where you will find all of the available Visionaire Studio functions (well most of them) besides community scripts, shader functions & the workflow functions. It is where I catalog all the functions; albeit I've loads still to add & pages to type up.

    You want to move the object to a specific coordinate but not the exact coordinate where the mouse is? I think the best approach to this would be to create a table containing all of the tile coordinates without adding any prefixes to the table, so that it automatically creates index values instead & then in left click you just get the coordinate based on index value.
    local t = {}
    t[1] = {x = 100, y = 250}
    t[2] = {x = 200, y = 500}
    ---etc...
    

    to call one of these we would do...
    moveObj("XY_space_img", t[1].x, t[1].y, 5000, easeQuintOut)
    

    .. the table would have to be in the same place as the workflow function else you would have to create a new function to return the required values.

    Or you could just manually enter the coordinates for each time you call the function.

    Imperator

    7278 Posts

  • #8, by andy-rinaldiMonday, 22. September 2014, 11:26 10 years ago
    Hi Lee,
    You want to move the object to a specific coordinate but not the exact coordinate where the mouse is?
    I want to move the object to a specific coordinate exact where I do left click with the mouse.

    There are 2500 coordinates, I prefer the getCursorPos lua function instead of write 2500 coordinates or create 2500 hotspots...
    If I have not choice I'll reduce the number of coordinates/hotspots.

    confuse

    Forum Fan

    160 Posts

  • #9, by AlexMonday, 22. September 2014, 11:59 10 years ago
    Alex: why does he need to get current position & - whatever from whatever? I tested the workflow function multiple times before adding it to the wiki & it worked as it was supposed to work.


    because when you click on coordinates 400,200 and the object is positioned at 100,100 you don't want to move the object by 400,200. It should only be moved by 300,100.

    I want to move the object to a specific coordinate exact where I do left click with the mouse.


    So what doesn't work with the example I provided above? you would have to consider the scroll position (if you have scrollable scenes) and maybe individual object offsets (in case your object images are not positioned at the object center) but these are just implementation details.

    Great Poster

    378 Posts

  • #10, by afrlmeMonday, 22. September 2014, 12:30 10 years ago
    What exactly is it that you are doing? You sent me an image via email the other day but I don't understand what it was, the image itself didn't make much sense to me. I know it was some kind of map thing or something with a grid, but that's about it.

    According to your answer then the script I posted for you (second reply) should work just fine. You need to remember though that object positions are based on their top left pixel & not the center of the image. If you want to get the center then you need to calculate the offset by half the image size on x & y.

    So it would be something like...
    local img_width = 16 -- 16 being 16 pixels, new getSize() function is not available in current public build...
    local img_height = 24
    local  cPos = getCursorPos()
    
    local t = {x = cPos.x - (img_width / 2), y = cPos.y - (img_height / 2)} -- get offset values
    
    moveObj("XY_space_img", t[1].x, t[1].y, 5000, easeQuintOut)
    


    P.S: I was considering adding a page to the wiki the day - well sometime this week - about lua tables.

    Imperator

    7278 Posts

  • #11, by andy-rinaldiMonday, 22. September 2014, 16:40 10 years ago
    It's very simple, I have a hotspot as a square (see attachment). In this hotspot I want click in any position and when I do this, the image (in center in the attachment) should move to coordinates where I have clicked with the mouse. The Alex script is working but when I click on 531,21 coordinates, the image is moved to 1455,423 for example, or click on 700,50 and the image move to 1700,450 etc I always have about 1000,400 of difference. You know what I mean?

    Lee, lo que quiero es hacer click con el ratón en cualquier posición dentro de ese cuadrado y haciendo eso mover la imagen a la posición donde haya clickado con el ratón. Es un sistema de navegación, con el click del ratón doy las coordinadas y la imagen debe seguir el click del ratón.

    Forum Fan

    160 Posts