PDA

View Full Version : Cloned object has wrong guid



Banbury
08-03-2017, 04:02 AM
When cloning an object, the new object returns the guid of the original object and not the new guid.



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

dzikakulka
08-03-2017, 04:56 AM
http://www.berserk-games.com/forums/showthread.php?4170-Bug-With-Copy-of-Objects

Banbury
08-03-2017, 05:05 AM
Berserk Games needs a bug tracker.

MrStump
08-03-2017, 10:10 AM
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.

Banbury
08-03-2017, 10:15 AM
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.

dzikakulka
08-03-2017, 10:49 AM
Object ref returned by .clone() is definitely valid, im working with it extensively :)

Banbury
08-03-2017, 11:04 AM
Have you tried putting it in an array and retrieving it later? Certainly didn't work for me.

dzikakulka
08-03-2017, 11:14 AM
Yeah, I did, what didn't work out for you?

Banbury
08-03-2017, 11:28 AM
All the entries in the array are null.

dzikakulka
08-03-2017, 01:24 PM
Post the code then or even better, share a save with a minimal example of that behaviour, we'll help.

Banbury
08-03-2017, 02:06 PM
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.



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?

dzikakulka
08-03-2017, 02:26 PM
@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:


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 (https://gist.github.com/) to have your syntax highlighted nicely.

Banbury
08-03-2017, 02:38 PM
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.

dzikakulka
08-03-2017, 02:42 PM
Yeah, that's interesting. Mind posting the save to take a look?

Banbury
08-03-2017, 04:50 PM
I'll do that tomorrow. I'll try to make a simpler test case.

Banbury
08-04-2017, 04:26 AM
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.

Banbury
08-04-2017, 05:42 AM
And here's the result:


https://vimeo.com/228351789