Results 1 to 17 of 17

Thread: Cloned object has wrong guid

  1. #1

    Cloned object has wrong guid

    When cloning an object, the new object returns the guid of the original object and not the new guid.

    Code:
    local o = self.clone({position = pos})
    print(o.guid)  -- This prints the guid of self

  2. #2

  3. #3
    Berserk Games needs a bug tracker.

  4. #4
    Join Date
    May 2016
    Posts
    1,072
    A way to work around this bug is to add a coroutine or a timer to add a short delay before continuing, to give it a moment to spawn the object and assign it a new GUID in the next frame.

    If this is a technical limitation, like that is just how clone has to work for some reason, giving clone a callback would also provide a workable solution.

  5. #5
    I tried the coroutine and yield. The GUID is still wrong. I'm not sure, when it is replaced. I've also tried storing the reference to the object itself. And that seems to be disposed.
    I'm trying to spawn a bunch of objects and remember them in a list for disposing them later.

  6. #6
    Object ref returned by .clone() is definitely valid, im working with it extensively

  7. #7
    Have you tried putting it in an array and retrieving it later? Certainly didn't work for me.

  8. #8
    Yeah, I did, what didn't work out for you?

  9. #9
    All the entries in the array are null.

  10. #10
    Post the code then or even better, share a save with a minimal example of that behaviour, we'll help.

  11. #11
    Maybe you see something I don't see. LUA isn't exactly my favourite language.

    The idea is to spawn some objects. The next time the function is called, the objects should be removed and new objects spawned.

    Code:
    spawned_objects = {}
    
    ...
    
        function doSomething()  -- a coroutine
            if next(spawned_objects) != nil then
                for i = 1, #spawned_objects do
                    o = spawned_objects[i]
                    print(spawned_objects[i])
                    o.destruct()
                end
                spawned_objects = {}
            end
    
            coroutine.yield(0)
    
            ...
    
            for i = 1, n do
                pos.x = pos.x + 1
                local o = self.clone({position = pos})
                coroutine.yield(0)
                table.insert(spawned_objects, o)
            end
            return 1
        end
    
    ...
    Does this forum have LUA highlighting?

  12. #12
    @Banbury
    Your code looks OK and it works for me as well. I added just a few lines to make it standalone and pasted on an object:
    Code:
    spawned_objects = {}
    
    function doSomething()  -- a coroutine
        if next(spawned_objects) != nil then
            for i = 1, #spawned_objects do
                o = spawned_objects[i]
                print('Destroy ' .. o.getName())
                o.destruct()
            end
            spawned_objects = {}
        end
    
        coroutine.yield(0)
    
        local pos = self.getPosition()
        for i = 1, n do
            pos.x = pos.x + 1
            local o = self.clone({position = pos})
            o.setName('Clone ' .. i)
            print('Create ' .. o.getName())
            coroutine.yield(0)
            table.insert(spawned_objects, o)
        end
        return 1
    end
    
    
    function onPickedUp(player_color)
        n=4
        startLuaCoroutine(self, 'doSomething')
    end
    You can see the result on this gif: https://gfycat.com/TidyMixedDeviltasmanian
    On each pickup, spawned_objects is iterated, destroying objects it contains and clearing the table (since its empty in the first place, nothing happens the first time). Then, "n" clones are created and each is put in the spawned_objects. Its contents are pointing to the right objects since on next pickup, old clones are deleted just OK. Let me know if you had any questions

    I don't think we can do Lua highlight here. You can use e.g. GitHub Gist sharing to have your syntax highlighted nicely.

  13. #13
    I'm calling the function from Global. Maybe that's the difference.
    Interestingly the object reference is null not nil. This might be a C# bug.

  14. #14
    Yeah, that's interesting. Mind posting the save to take a look?

  15. #15
    I'll do that tomorrow. I'll try to make a simpler test case.

  16. #16
    I found the solution of my problem. I am using an object with states. After changing the state I have to save the new object returned by setState.

    Thanks for the help.

  17. #17
    And here's the result:


Similar Threads

  1. Replies: 10
    Last Post: 06-28-2017, 12:15 PM
  2. [Bug] Buttons display on wrong object for guests in server
    By Eskander in forum Scripting Bug Reports
    Replies: 5
    Last Post: 01-28-2017, 05:26 AM
  3. [SUPPORT] Starting Up On Wrong Monitor
    By diablo10001 in forum Technical Support
    Replies: 1
    Last Post: 01-17-2017, 10:43 AM
  4. GUID isn't unique on copied object
    By Korroz in forum Scripting Bug Reports
    Replies: 0
    Last Post: 01-15-2017, 09:40 AM
  5. [SOLVED] your normal mapping is all wrong!
    By ollj in forum Technical Support
    Replies: 8
    Last Post: 02-16-2015, 03:02 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
  •