Results 1 to 7 of 7

Thread: Deselect Object onObjectDrop event

  1. #1

    Deselect Object onObjectDrop event

    Hello everyone, new member here with newbie question.

    I want to do something that I thought would be very simple but I can not figure it out. When multiple objects are dropped (let's say 3 dice are thrown) I want all the objects dropped to be immediately deselected. Here is some code that does not work starting with my biggest travesty. =)



    Code:
    isDoneDropping = true
    diceDropTable = {}
    function onObjectDrop(player_color, dropped_object)
        print("The " .. player_color .. " player dropped object " .. dropped_object.guid .. "(" .. dropped_object.tag .. ")")
        table.insert(diceDropTable, dropped_object.guid)
        dropped_object.interactable = false
    
        if isDoneDropping == true then
            isDoneDropping = false
            startLuaCoroutine(Global, "makeInteractable")
        end
    end
    
    function makeInteractable()
        for i=1, 60 do
            coroutine.yield(0)
        end
        isDoneDropping = true
        for _, guid in ipairs(diceDropTable) do
            print(guid)
            getObjectFromGUID(guid).interactable = true
        end
        diceDropTable = {}
        return 1
    end
    That has many "theoretical" flaws but as I mentioned before that was my last "bang my head against the wall" attempt.

    Some other things I tried to no avail.

    Code:
    -- this just produces an error and I know I was using it wrong but hey gotta try
    --dropped_object.held_by_color = ""
    -- not to sure how to use this but want to remove the yellow highlight, again quite positive this would not work being "highlighting" is not the same as the objects being "selected"
    --dropped_object.highlightOff(true)
    --dropped_object.highlightOff("Black")
    -- this sort of works but with results I do not want on many levels. It not only stops the dice from rolling but then the player can't use the dice again until unlocked
    --dropped_object.setLock(true)
    The result I seek: dice are thrown and using the onObjectDrop event deselects the objects [as they continue to roll].

    I found a multitude of other functions *sort of* related to "selected" objects but I couldn't find one that would deselect the objects. I can't even a find a way to trick the system aside from one that would cause other problems which is" onDropObject { select some invisible/ or very small/or offset object}


    Any help would be very much appreciated.

    Berserk3k! (I am; lol)
    Last edited by berserk3k; 07-07-2018 at 10:29 AM. Reason: SOLVED

  2. #2
    I don't think there's a clean way of doing that but something coul be hacked together I think. If locking does that, you might want to try saving an object velocity and angular velocity, locking it and then unlocking and reapplying velocities a frame later.

    What goes wrong with setting them to uninteractable? Does it not deselect them simply?

    PS. At the part where you're inserting objects GUIDs in the table, you could be just inserting object refs there, saves a few lines

  3. #3
    It's a timing issue with setting them non-interactable plus it's super hacky and unwieldy for further script development. Really seems deselecting objects with a script should be foundational core level functionality.

    I'm coming up with various other ways to circumvent this limitation such as a boolean onObjectDrop that turns something on that checks the objects (on a loop; onUpdate) if they're resting and when one is true it copies the object, makes a table of properties (position in the world, value of the roll, etc), destroying the object, and making a new object using the custom table but it seems way over the top for something, again, that should be core level functionality (yes I'm bitching at this point; heh). I may go as far as running a third-party app that tracks memory and "fake" clicks the table just to clear the selection highlighting on the objects.

    At a total loss but I'll look into what you metioned by creating my own physics while the objects are in a locked state.

    (I seriously thought this would take me a minute of research on the wiki.... 4 days ago)

  4. #4
    If setting stuff to uninteractable for one or two frames would do it, it seems like a quite valid solution. Its code could be adjusted to be a little more flexible and allow any amount of simultaneous drops too. It shouldn't mess with physics or anything afaik.

  5. #5
    It doesn't, thus why I said it's a timing issue (though not apparent). What happens is if the frames passed is long enough AND then the player *tries* to click a die by default (because clicking through to the table) they'd unselect everything. Very unwieldy and hacky and confusing. =)

    Let me set the scenario that keeps happening with all players, inculding myself, I wish to remove from my game. Player A grabs X amount of dice to roll and then wants to sort perhaps a few dice after the roll OR roll another die or two. What happens 50% (way conservative btw lol) of the time is the player goes to grab the die, forgets to click the table first to "clear selected" and ends up A) picking up all the dice and setting them down in haste *but it was enough to jostle some other dice into spinning* or B) the player doesn't notice all dice are in hand when they want to roll only one die but end up rolling all dice.

    If the dice just unselected after a roll (not by default; I understand not messing with the communities game but through scripting) it would 100% solve our problems and my games would go from 60-80 minutes a game to 30-40 minutes because everyone isn't yelling "reroll", "I f'ed up", "i didn't mean to do that".

  6. #6
    Quote Originally Posted by berserk3k View Post
    Let me set the scenario that keeps happening with all players, inculding myself, I wish to remove from my game. Player A grabs X amount of dice to roll and then wants to sort perhaps a few dice after the roll OR roll another die or two. What happens 50% (way conservative btw lol) of the time is the player goes to grab the die, forgets to click the table first to "clear selected" and ends up A) picking up all the dice and setting them down in haste *but it was enough to jostle some other dice into spinning* or B) the player doesn't notice all dice are in hand when they want to roll only one die but end up rolling all dice.
    Ok solved. After many reviews of games on the workshop a large handful of them spawn dice using buttons that drops them in to dice towers and buttons clear the dice--thus inadvertently bypassing this pesky behavior through method of use. It's all starting to make sense why this way of doing it is so prevalent. I am dropping the madness of trying to do it my way and will now do it like everyone else. ~cheers

  7. #7
    Decided to not be beat by this so I'd like to present a [URL="https://steamcommunity.com/sharedfiles/filedetails/?id=1434843441"]working example[/URL]!

    It does have a few other things not needed to accomplish what I wanted being it's for my real mod, so I didn't bother simplify it. ~laughs~

    Code:
    shuffledAmount = 0
    frameCounter = 0
    diceDropTable = {}
    diceSmoothMoving = {}
    dieSlotCoords = {
        {-4,1.29,-16},
        {-2,1.29,-16},
        {0,1.29,-16},
        {2,1.29,-16},
        {4,1.29,-16}
    }
    openDieSlots = {false, false, false, false, false}
    numObjsPlayerPickedUp = 0
    
    
    function onLoad() end
    
    
    function onObjectRandomize(_, str)
        if shuffledAmount < 1 then
            shuffledAmount = countTable(Player[str].getHoldingObjects())
        end
    end
    
    
    function onUpdate()
        frameCounter = frameCounter + 1
        if frameCounter == 4 then checkResting(); end
        if frameCounter == 6 then checkSmoothMoving(); end
        if frameCounter > 7 then frameCounter = 0; end
    end
    
    
    function checkSmoothMoving()
        local o, vPos, vRot, newObject, passKey
        local next = next
        if next(diceSmoothMoving) ~= nil then
            for key, sGuid in pairs(diceSmoothMoving) do
                o = getObjectFromGUID(sGuid)
                if o.isSmoothMoving() == false then
                    for j = 1,5 do
                        if openDieSlots[j] == sGuid then passKey = j; break; end
                    end
                    vPos = o.getPosition(); vRot = o.getRotation()
                    destroyObject(o)
                    table.remove(diceSmoothMoving, key)
                    newObject = spawnObject({ type = "Die_6", position = {x=vPos.x, y=vPos.y, z=vPos.z}, rotation = {x=vRot.x,y=vRot.y,z=vRot.z},
                        sound = false, callback = "assignName", callback_owner = Global,
                        params = { name = "Example Dice", iKey = passKey }
                    })
                end
            end
        end
    end
    
    
    function checkResting()
        local oDice
        local next = next
        if next(diceDropTable) ~= nil then
            for key, sGuid in pairs(diceDropTable) do
                oDice  = getObjectFromGUID(sGuid)
                if oDice.resting == true then
                    for i = 1,5 do
                        if openDieSlots[i] == false then
                            openDieSlots[i] = sGuid
                            oDice.rotate({x=0, y=(360 - oDice.getRotation().y), z=0})
                            oDice.setPositionSmooth(dieSlotCoords[i],false,false)
                            break
                        end
                    end
                    table.insert(diceSmoothMoving, sGuid)
                    table.remove(diceDropTable, key)
    
                end
            end
        end
    end
    
    
    function onObjectDrop(player_color, dropped_object)
        if shuffledAmount > 0 and dropped_object.getName() == "Example Dice" then
            table.insert(diceDropTable, dropped_object.guid)
        end
        shuffledAmount = shuffledAmount - 1
    end
    
    
    function onObjectPickUp(p, o)
        for i = 1,5 do
            if openDieSlots[i] == o.guid then openDieSlots[i] = false; break; end
        end
    end
    
    
    function assignName(o, t)
        o.setName(t.name); openDieSlots[t.iKey] = o.guid;
    end
    
    function countTable(t)
        local c = 0; for _ in pairs(t) do c = c + 1; end return c
    end

Similar Threads

  1. Button/Input Event Callback Index
    By LastTalon in forum Scripting Suggestions
    Replies: 0
    Last Post: 08-28-2017, 07:38 PM
  2. Event needed when object enters a "bag/deck" (not global).
    By francogp in forum Scripting Suggestions
    Replies: 3
    Last Post: 07-06-2017, 09:16 AM
  3. onFlip event
    By Huffel in forum Scripting Suggestions
    Replies: 1
    Last Post: 01-15-2017, 03:18 PM
  4. How do I catch the Roll event?
    By Tumba in forum Scripting
    Replies: 5
    Last Post: 06-25-2016, 04:51 AM
  5. Event log
    By Concerned Party host in forum General Discussion
    Replies: 0
    Last Post: 04-02-2016, 02:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •