Results 1 to 11 of 11

Thread: Scripting help: can you get the GUID of a newly spawned object?

  1. #1

    Scripting help: can you get the GUID of a newly spawned object?

    I am trying to hack together some mods due to the inability to save textobjects to chest.

    I have a button that spawns a text object when pressed. Is there any way of getting the guid of that object in a variable so I can use it in place of a hard-coded guid in another part of the script?

  2. #2
    Yes, obj.getGUID(). There's not much need for using GUIDs outside of hard-coding them for faster access to objects already on the table and the onSave() event. If that wasn't what you needed, post your script snippet and we'll figure it out

  3. #3
    When you spawn the object the function that spawns it should return it as a variable. So you can do:

    Code:
    object = deck.takeObject({guid='aaaaaa'})
    local guid = object.getGUID()
    and then object.getGUID() will return the guid. However, I'm not sure if it'll be available right away: when you spawn objects they will appear in the game at some later frame. To do things with them you should use the callback options:

    Code:
    function foo()
        deck.takeObject({guid='aaaaaa', callback='store_guid', callback_owner=Global})
    end
    
    function store_guid(object)
       local guid = object.getGUID()
       -- do something with guid
    end
    foo() will spawn the object, and then once it appears store_guid will automatically be called with it.

  4. #4
    Join Date
    May 2016
    Posts
    1,072
    oneliveleft, you're right about the GUID not always being available right away. Most notably if you are spawning more than 1 object at once, you usually can't get the GUID at the same time (in my experience). The callback is the safe solution, as you mentioned. =)

  5. #5
    Thank you all for the help. I wasn't able to get it working because I have no idea where or what I am doing but thank you all the same.

  6. #6
    Quote Originally Posted by dzikakulka View Post
    Yes, obj.getGUID(). There's not much need for using GUIDs outside of hard-coding them for faster access to objects already on the table and the onSave() event. If that wasn't what you needed, post your script snippet and we'll figure it out
    https://pastebin.com/yig1hHA9 This code is attached to a tile and counts the values of the dice on them, it requires a texttool with a specific GUID to work.

    https://pastebin.com/gdMHP5mj This code is also attached to a tile and has a button that spawns a text object saying "I accept"

    I wanted to make it so the first tile has the button (or even better autospawns one on load), gets the GUID of the text object and uses it in place of the hardcoded GUID.

  7. #7
    I don't fully get what you're doing, but it seems like the tile should use the nearest texttool to it? In which case, in its onload you can find the nearest texttool and set it up:

    Code:
    function quick_distance(pos_a, pos_b)
        local dx = pos_a.x - pos_b.x
        local dy = pos_a.y - pos_b.y
        local dz = pos_a.z - pos_b.z
        return dx*dx + dy*dy + dz*dz
    end
    
    function onload()
        textObject = nil
        local distance
        for i, object in pairs(getAllObjects()) do
            if object.TextTool then
                local d = quick_distance(object.getPosition(), self.getPosition())
                if textObject == nil or d < distance then
                    textObject = object
                    distance = d
                end
            end
        end
        -- now have correct textObject, your code can continue from here
    end
    Last edited by onelivesleft; 06-28-2017 at 04:48 AM.

  8. #8
    No, it needs to spawn a texttool for it's exclusive use, not just use a random texttool it finds.

  9. #9
    Somebody on reddit was kind enough to code this up for me, using a 0 size button instead of a text object. The only problem is when I set the font to size 25 in order to fit on the object, it becomes extremely blurry. Is there any way I can fix this? https://pastebin.com/DdsKyB0g http://prntscr.com/fp83oc

  10. #10
    Use scale={0.25, 0.25, 0.25} and font_size=100 in the button parameters for 4 times the clarity.

  11. #11
    You are indeed a legend. Thank you!

Similar Threads

  1. Anti-Troll Suggestion: Spawned Object Ownership/Areas
    By Cesar in forum General Discussion
    Replies: 4
    Last Post: 04-12-2017, 07:31 PM
  2. [SOLVED] Menu missing options: Can't get GUID from a scripting zone?
    By qvazzler in forum Scripting
    Replies: 5
    Last Post: 01-23-2017, 08:07 PM
  3. GUID isn't unique on copied object
    By Korroz in forum Scripting Bug Reports
    Replies: 0
    Last Post: 01-15-2017, 09:40 AM
  4. Give Hidden Zones a Scripting GUID
    By Zero3ffect in forum Scripting
    Replies: 3
    Last Post: 07-03-2016, 12:10 AM
  5. Can't delete spawned button
    By larmatt in forum Scripting
    Replies: 0
    Last Post: 03-29-2016, 05:11 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
  •