Results 1 to 6 of 6

Thread: Random state when removed from bag?

  1. #1

    Random state when removed from bag?

    I'm sure this is simple enough. I have a custom token that has 4 different designs. They are purely cosmetic and have no game function. I combined the 4 designs into states and added it to an infinite bag. Is it possible to have the token choose a random state when one is removed (created) from the bag? Screenshot attacked for reference.

    20180711093935_1.jpg
    "Do not meddle in the affairs of dragons, for you are crunchy and good with ketchup."
    My TTS Mods

  2. #2
    Put this into the script for the object before initializing the bag with the object...

    Code:
    function onLoad()
        local statelist = self.getStates()
    
        self.setState(statelist[math.random(#statelist)].id)
    end
    This will randomize the state each time the object is spawned, including game load time.

    If you know the number of states beforehand, you just set it directly... object.setState(math.,random(numberOfStates))

    If you want to set the state only when leaving the bag, use

    Code:
    onObjectLeaveContainer(container, contained_object)
        if container = self then
            local statelist = contained_object.getStates()
    
            contained_object.setstate(statelist[math.random(#statelist)].id)
        end
    end

  3. #3
    I might need a little more hand-holding. I tried both of these and couldn't get them working. The first one, the token chose it's state when the game loaded, but then would only use that state when being removed from the bag. The second one just didn't work, saying there was an issue around the =.
    "Do not meddle in the affairs of dragons, for you are crunchy and good with ketchup."
    My TTS Mods

  4. #4
    Hmmm, Can you let put up a workshop item with a bag and your multi-state tile? I will attempt to make it a bag with the appropriate properties

  5. #5
    OH! I had a typo.

    Code:
    onObjectLeaveContainer(container, contained_object)
        if container == self then
            local statelist = contained_object.getStates()
    
            contained_object.setstate(statelist[math.random(#statelist)].id)
        end
    end

  6. #6

    A better implementation

    Well it turns out to be a bit more complicated than I thought.

    the above (as corrected) works, but very slowly.
    by adding a short delay using the new Wait class, I think you'll be satisfied.

    Code:
    -- this goes in the script on your bag.
    function onObjectLeaveContainer(container, object)
        if container == self then
            Wait.frames(function() object.setState(math.random(#object.getStates())) end, 2)
        end
    end
    I have made a demo table with an ugly bag and an 8 state pawn.
    https://steamcommunity.com/sharedfil...?id=1445564625

    Enjoy!

Similar Threads

  1. Replies: 2
    Last Post: 02-19-2019, 01:09 PM
  2. Replies: 3
    Last Post: 05-05-2018, 11:14 PM
  3. Provide a way to identify objects removed or marked for removal
    By my_hat_stinks in forum Scripting Suggestions
    Replies: 1
    Last Post: 03-12-2017, 01:10 PM
  4. Replies: 7
    Last Post: 01-15-2017, 04:33 AM
  5. Replies: 3
    Last Post: 07-23-2016, 08:17 AM

Posting Permissions

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