Trying to figure out how to create an object-based volume slider

  • #1, by charlieTuesday, 21. August 2018, 23:51 6 years ago
    Hi, Jeff and I have been trying to figure out all day how to create a object-based volume slider (and other sliders as well) in our settings menu, with no luck. We are new to this engine and lua, and I was hoping that someone could point us in the right direction.

    We saw that the suggestion a couple years ago was to use a single frame animation loop to do this, but we thought that for our purposes, object based would be better. However, if the animation loop would be better, we are open to suggestions.

    Our basic thought process was that we would add the slider thumb (handle, hereafter referred to as H-Thumb as that is how it is named in the script) to the scene as an object, create a polygon around it, and then attach the following script to it. What I am trying to do with the script is to find the cursor x value, and match the H-Thumb x value to it within the xMin and xMax constraints, which would be the edges of the slider area.
    cursorPos = getCursorPos()
    xMin = 755
    xMax = 1326
    
    hThumb = getObject("Objects[H-Thumb]"):getPoint(VObjectPosition)
    
    function onMouseEvent(eventType, mousePosition)
      if eventType == eEvtMouseLeftButtonHolding then
        -- Dragging function
        if cursorPos.x < xMin then
                hThumb:setValue(x, xMin)
            elseif cursorPos.x > xMax
                hThumb:setValue(x, xMax)
            else
                hThumb:setValue(x, cursorPos.x)
        end
        getObject("Objects[H-Thumb]"):setPoint(hThumb)
        print("Cursor Position is" .. cursorPos.x)
        print("H-Thumb Position is" .. hThumb.x)
      end
    end
    
    registerEventHandler("mouseEvent", "onMouseEvent", {eEvtMouseLeftButtonHolding})
    
    

    Is this the right way to go about it? Or is there a different/better way? The syntax is really confusing to me and there is very limited documentation, so I am having trouble determining when and where I should use the code I find.

    Thanks,

    Charlie

    Newbie

    2 Posts


  • #2, by afrlmeWednesday, 22. August 2018, 00:07 6 years ago
    Animations are easier because you can specify absolute position whereas object positions are offset based.

    https://pastebin.com/DMNsCWF7

    This is my slider script. To use it you will need to adjust some things. You will also need to use animations. I have not publicly shared this script, so there's no tutorials available on how to use it. Long story short, you create some animations, position them on scene where they should start, specify the start & end point of the slider rail inside of a table in the script & then you create an object polygon for each of the slider rails so that the script can detect when the mouse is over an object that can be dragged around.

    P.S: I tried pasting in my script into a code block here, but it messed it up.

    Imperator

    7278 Posts

  • #3, by sebastianWednesday, 22. August 2018, 08:57 6 years ago
    Hi Charlie,

    yes the documentation in the wiki and lua docs are a bit confusing because the syntax changed a bit and for backwards compatibility there are more ways to specify objects or getting/setting the values. 

    AFRLme's pasted script works great, especially when you want more than one slider or need to add one later to a menu. In the VS editor you mainly need to position an object polygon+animation and the rest is handled by the script. 

    It depends a bit on how deep you want to/can dive into the Lua world with VS to accomplish such things. 

    A more simpler slider solution was made by Einzekkaempfer long time ago. A bit outdated, but should still work. I cant find the original post, but i found a dropbox link by AFRLme in the forum which had an example project in it:


    Thread Captain

    2346 Posts

  • #4, by jeffWednesday, 22. August 2018, 18:55 6 years ago
    Hi Charlie,

    yes the documentation in the wiki and lua docs are a bit confusing because the syntax changed a bit and for backwards compatibility there are more ways to specify objects or getting/setting the values. 

    AFRLme's pasted script works great, especially when you want more than one slider or need to add one later to a menu. In the VS editor you mainly need to position an object polygon+animation and the rest is handled by the script. 

    It depends a bit on how deep you want to/can dive into the Lua world with VS to accomplish such things. 

    A more simpler slider solution was made by Einzekkaempfer long time ago. A bit outdated, but should still work. I cant find the original post, but i found a dropbox link by AFRLme in the forum which had an example project in it:



    Thanks, Sebastian! Unfortunately the link seems to be expired or no longer active. I think I was expecting to be able to do much more coding than repetitive UI based actions, etc. We might just have to do things with less coding if we can't find reliable documentation. 

    It would be great for someone to create a video tutorial to demonstrate LUA/VS scripts for those of us who are new to this dev tool. I'm having trouble even getting print("hello world") to show up in the log. I must be doing something wrong.

    Newbie

    17 Posts

  • #5, by sebastianWednesday, 22. August 2018, 19:04 6 years ago
    hmm i tested the link before posting and it was working this morning... 

    i did two german crash course tutorials about using Lua with VS on my Youtube Channel.
    Not sure if this would somehow help you, but i can send you the Links to these. 

    The first covers some basic Lua in general and the second goes into depth to access the data structure from VS. But your posted script shows me that you already know most of these things. 

    I somehow will also cover stuff like settings and sliders in my tutorials in the future but currently thats far away. 

    Thread Captain

    2346 Posts

  • #6, by afrlmeWednesday, 22. August 2018, 21:03 6 years ago
    where are you testing print("hello world")? in an execute a script action part? inside of a definition or execution type script? or directly via the developer console?

    Imperator

    7278 Posts

  • #7, by charlieWednesday, 22. August 2018, 21:55 6 years ago
    Thanks for your help, guys. Unfortunately, those german tutorials will not help roll I did see those, but we live in America and only know about three words in German, lol!

    I have been doing a lot of reading about Lua, so I understand enough to kinda know something about it, but I am not sure if my syntax is exactly right or if it is doing what I think it is doing.

    I will have to discuss with Jeff and see if an animation would be possible for us. AFRLme, I downloaded your script from another thread earlier to test it. I am still a little unsure about how to implement it in our game, but I guess that will just require testing. I thought I would give the object-based method a try since you had said in another thread that the animation method was a bit antiquated, and that there was a newer method that you had used, but hadn't said any more.

    Sorry for the ramble, just trying to figure out which is the best path to pursue.

    Thanks again for your help!

    Charlie

    Newbie

    2 Posts

  • #8, by afrlmeThursday, 23. August 2018, 02:55 6 years ago
    My method involved using object polygons & the mouse event handler as opposed to mainLoops & action parts all over the place. The script handles everything & all that's required for it to work are some animations & objects with specific names & interaction polygons to tell the script when the mouse is hovering over a specific slider rail - all in all it is just a more elegant solution that was not possible back when Einzelkaempfer wrote & shared his script.

    As for objects, you can drag those around too, but you need to understand that you can only update the offset position for them & the object position is based on the interaction position you specificy for the object & not the top left pixel of the image or animation you assign to the object. The easiest way to get absolute positioning is to leave the object position as 0, 0 because that means whatever position you set will be absolute & will not require any math to calculate the needed offset to set it to actual position you want it to be in.

    Imperator

    7278 Posts

  • #9, by EinzelkämpferSaturday, 25. August 2018, 23:10 6 years ago
    For your information:

    Sebastian mentioned the volume slider that I did some years ago. I have no idea, if it is still working, and I am sure there are easier ways to do it nowadays. But you can still download it, if you like (see link below). It comes with a detailed description of what I did there - in German, I'm afraid...

    Perhaps you can learn from it, even if it's outdated.

    Newbie

    81 Posts