Results 1 to 3 of 3

Thread: EXAMPLE: Using onCollision with Physics Cast to spawn tokens (or not)

  1. #1

    EXAMPLE: Using onCollision with Physics Cast to spawn tokens (or not)

    Situation. I want tokens to spawn onto a card when it is placed in the player's area but I do not want more tokens to spawn if there are already tokens there.

    function onCollisionEnter(collision_info)
      -- Variables
        local object = collision_info.collision_object
        local desc = object.getDescription()
    -- gets the Object position where it was dropped to trigger the collision
        local posX = object.getPosition().x
        local posY = object.getPosition().y
        local posZ = object.getPosition().z
        stamina_value = 0
        sanity_value = 0
    -- Physics cast triggers when the card is dropped and is cast from the position of the card - if any object from the physics cast has "token" 
    in the description then sets variable to true 
        local hitList = Physics.cast({origin={posX, posY + 0.15, posZ}, direction={0,1,0}, type=2, size={2, 0.5, 2}, max_distance = 0})
        hasTokens = false
        for _, entry in ipairs(hitList) do
          local descript = entry.hit_object.getDescription()
          if string.find(descript, "token") then
            hasTokens = true
    -- If there are no tokens found in the physics cast, then new tokens will spawn based on my conditions.
    if not hasTokens then
          if string.find(desc, "ally") then
            if string.find(desc, "1stamina") then
                stamina_value = 1
              stamina_token = bag_stamina.takeObject({position = {bag_stamina_pos.x, bag_stamina_pos.y +2, bag_stamina_pos.z}, 
                  rotation = {0,180,0}, callback = "set_Stamina_Value", callback_owner = self})
             -- sets position of tokens relative to the card, in this case I want the tokens offset slightly on the card.
              stamina_token.setPosition({posX - 0.62, posY + 0.2, posZ - 0.18})
    The taken object in this case needs a callback so I can call the add function on the counter there

    function set_Stamina_Value()
         for i=1, stamina_value do
    Thanks @Tragic for pointing out that flipping the card (aka "tapping") would cause new tokens to spawn when not needed and this was the fix.

  2. #2
    This would probably work.

    • Store the GUID of the object as it is dropped.. put it into a table. So as you drop items onto your object or found with the cast or w/e the table is recording all the items by GUID
    • then simply put a test into the code before adding the tokens to the card that cycles though the object list and only execute it is the object drooped is not in the list.

    So dropping an item would only ever execute once.
    My Boardgame uTube chan - Tragic's Table Top
    BGG Guild of BoardGame uTubers - Tube Tables

  3. #3
    That would work, but then would need to remove it from the table when it is discarded so if it is gained from the discard pile again, you can trigger when you play it again.

Similar Threads

  1. Physics.cast ray, debug extending past where it actually affects
    By DeathKnight in forum Scripting Bug Reports
    Replies: 1
    Last Post: 07-26-2017, 06:42 AM
  2. [ADDED] Physics.cast() should also collide with the table.
    By Mark in forum Scripting Suggestions
    Replies: 0
    Last Post: 06-11-2017, 10:11 AM
  3. Have measure sticks cast silhouette down on to board
    By Lebatron in forum Suggestions
    Replies: 2
    Last Post: 03-22-2015, 07:29 PM
  4. Replies: 2
    Last Post: 10-28-2014, 08:03 PM
  5. [CLOSED] Please cast your vote!
    By Kimiko in forum General Discussion
    Replies: 50
    Last Post: 03-20-2014, 08:31 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