Page 2 of 2 FirstFirst 12
Results 26 to 32 of 32

Thread: takeObject() doesn't take last card from deck

  1. #26
    Join Date
    May 2016
    Posts
    1,072
    Put a script zone over where your deck goes (or use deck.getPosition might work still) to find the last little card and hand it out.

    Basically, put all your takeObject code inside of an if statement checking if #deck.getObjects() > 0 then

    Then in the else portion, use the script zone and getObjects() to get a list of objects in the zone. Then go over them with a for loop checking if object.tag == "Card" and that should let you find the last card. Then use setPosition and setRotation and whatever directly on it instead of takeObject.

    Code:
    if #deck.getObjects() > 0 then
        (take object normally)
    else
        for _, obj in ipairs(scriptZone.getObjects()) do
            if obj.tag == "Card" then
                (set position)
                break
            end
        end
    end

  2. #27
    Quote Originally Posted by maximo1984@ymail.com View Post
    In plain words.

    There is a deck of cards, call it a discard pile. In this deck, there are cards that need returned to their own decks, defined by their descriptions. There could be between 2-5 different destinations.
    So, something like:

    Code:
    function processDiscard(deck)
      for index, carddata in ipairs(deck.getObjects()) do
        local cardObject = deck.takeObject({ flip = true })
        if cardObject.getDescription() == 'Bob' then
          cardObject.setPosition({Bob deck vector})
        end
      end
    end

  3. #28
    @brainsick - got it working finally with your code. Thanks for all the help everyone.

    Here is the working code

    Code:
    function start_return_cards()
      for _, object in ipairs(zone_past.getObjects()) do
        if object.tag == "Deck" then
          deck = object
          break
        end
      end
    
      while deck.guid == nil do
        coroutine.yield(0)
      end
      print(deck.guid)
    
      return_cards(deck)
    
      return 1
    end
    
    function return_cards(deck)
      local deckObjects = deck.getObjects()
      local deckSize = #deckObjects
    
      for index, card in ipairs(deckObjects) do
        if index < deckSize then
          local desc = card.description
          if string.find(desc, "exploration 1") then
            deck.takeObject({position = decks_explore.one, guid = card.guid, flip = true})
          else
            if not string.find(desc, "exploration") then
              deck.takeObject({position = deck_adv_pos, guid = card.guid, flip = true})
            end
          end
    
        else
          local card = deck.takeObject({position = deck.getPosition(), flip = true })
          local desc = card.getDescription()
          if string.find(desc, "exploration 1") then
            card.setPosition(decks_explore.one)
          else
            if not string.find(desc, "exploration") then
              card.setPosition(deck_adv_pos)
            end
          end
        end
      end
    end

  4. #29
    @maximo1984 - I don't think you need 70% of that code.

    I'm happy to help further, but don't feel good about discussing it in this topic; the report of a bug in takeObject() when selecting by guid. Side note, a freshly spawned deck's cards all have the same guid, so it's kind of meaningless to select by guid in a lot of contexts.

    Do you want to open another thread asking for a code review? Or perhaps you'd like to take the discussion private?

  5. #30
    Sure, I never understood the reason why you have to take the card by guid. I thought you had to use it for when you are taking a specific card. Which I'm going here by string.find(desc)

    PM me if you like, how best to trim this?

  6. #31
    In your use case, you don't need to select the cards by guid. You can simply treat the deck like a stack/array/queue because you're processing the whole thing.

    Code:
    -- this is context for my clean room
    local decks_explore = {}
    decks_explore.one = { x = 1, y = 1, z = 1 }
    local deck_adv_pos = { x = 5, y = 1, z = 5 }
    
    -- this is all you need, get your deck, call this method
    -- forget all that coroutine stuff; your deck is already initialized
    function return_cards(deck)
      for index, carddata in ipairs(deck.getObjects()) do
        local cardObject = deck.takeObject({ flip = true })
        local cardDesc = cardObject.getDescription()
        if string.find(cardDesc, "exploration 1") then
          cardObject.setPosition(decks_explore.one)
        elseif not string.find(cardDesc, "exploration") then
          cardObject.setPosition(deck_adv_pos)
        else
          print("found a card I wasn't expecting!")
          cardObject.destruct()
        end
      end
    end
    Last edited by brainsick; 11-07-2017 at 02:43 PM.

  7. #32
    Awesome! Thanks man. I will have a look at cleaning up the code tomorrow.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 07-06-2017, 12:28 PM
  2. Replies: 1
    Last Post: 03-22-2017, 10:28 AM
  3. Replies: 2
    Last Post: 07-07-2016, 05:15 PM
  4. Replies: 8
    Last Post: 04-18-2016, 07:11 PM
  5. Deck Builder doesn't open saved decks properly
    By Logarek in forum General Discussion
    Replies: 8
    Last Post: 08-18-2015, 11:57 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
  •