Results 1 to 3 of 3

Thread: Loop Logic Help

  1. #1

    Loop Logic Help

    Hi there... I am having an issue with working out the logic of a loop....

    I am trying to get x items from a script zone.

    This is what I want to happen

    • I get a input or amount of times a button is pressed. (pressed)
    • I search the script zone with getObjects()
    • I do a If test to find the objects with the correct name
    • I move those object
    • I stop the loop when we reach the "pressed" value.


    I can not work out how to break out of the for loop... I always seem to pick up everything in the script zone... not just say 3 or 5 or what'evs


    Here are the two codes (stripped down).... the 1st loops for the amount of times the button was pressed. The 2nd loops through the objects in the script zone. (which is a bowl of tokens) and then takes only the "clay" tokens and moves them.

    Code:
    for i=1, num, 1 do
         --Loops the amount of times I click the object
    end
    Code:
        for _, obj in ipairs(zone) do
            if obj.getName() == 'Clay' then
                obj.setPosition(pos)
                obj.setRotation(rot)
                obj.setPositionSmooth(destination, false, false)
            end
        end
    TL;DR: How can I can I combine those loops so it only takes the amount of objects defined by "num". As the for loop will move ALL the objects that match that If Statement.

    Thanks!
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  2. #2

    isn't it just one loop to get n things from a zone?

    Code:
        for _, obj in ipairs(getObjects(zone)) do
            if obj.getName() == 'Clay' then
                obj.setPosition(pos)
                obj.setRotation(rot)
                obj.setPositionSmooth(destination, false, false)
                pressed = pressed - 1
                if pressed == 0 then
                    break
                ene
            end
        end
    should do the trick.
    you can also use return if you are in a function and that is the last thing the function is to do.

  3. #3
    Join Date
    May 2016
    Posts
    1,072
    The simplest way I can think of to do this would be
    Code:
    --Call from your function that determines how many to pull
    --Feed this function the script zone as well as the # of obj to take
    function takeFromZone(zone, howMany)
        for i=1, howMany do
            zone.takeObject(params)
        end
    end
    You could make a list of eligible objects to pull in that function too, like this, if you wanted them to have certain names. In this example, it would pull objects from a script zone that are tagged as being a "Dice". It also has built-in error protection, not allowing you to try to take more dice from the zone than there are.

    Code:
    --Call from your function that determines how many to pull
    --Feed this function the script zone as well as the # of obj to take
    function takeFromZone(zone, howMany)
        local func_dice = function(o) return o.tag=="Dice" end
        
        local objList_all = zone.getObjects()
        local objList_dice = tableRefine(objList_all, func_dice)
        
        for i, dice in ipairs(objList_dice)
            dice.setPosition(whatever)
            if i == howMany then
                break
            end
        end
    end
    
    --Refines a table, only returning entries that match func's requirement
    --Example func: function(o) return o.name=="Entry's Name" end
    function tableRefine(t, func)
        if func==nil then error("No func supplied to refineTableBy") end
        local refinedTable = {}
        for _, v in ipairs(t) do
            if func(v) then
                table.insert(refinedTable, v)
            end
        end
        return refinedTable
    end

Similar Threads

  1. Replies: 4
    Last Post: 12-11-2017, 07:49 AM
  2. Logic statements with moving objects
    By vortex260 in forum Scripting
    Replies: 6
    Last Post: 05-20-2017, 04:21 PM
  3. Replies: 2
    Last Post: 05-17-2017, 02:59 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
  •