Object creation in a for loop

I’m trying to read and then create objects in a for loop. The problem is that, the algorithm below is probably overwriting the same object because when I try to pull the data, it gives the same ID. How can I solve this issue?

I’m using OWL library. It is an early stage, so I’m open to any other OOP library suggestions.

Any help would be appreciated.

[lua]for key, data in pairs(levelData.objects) do
local object = owl.instance {from = GameObject}
object:setPNGImage(data.src)
object:getImage().x = data.x
object:getImage().y = data.y
object:setID(data.id)
object:setSoundOnClick(data.soundOnClick)

table.insert(loadedImages, object)
end[/lua] [import]uid: 154911 topic_id: 32255 reply_id: 332255[/import]

local objectsTable = {}
local tempInt = 0

for key, data in pairs(levelData.objects) do
tempInt = tempInt + 1
local object = owl.instance {from = GameObject}
object:setPNGImage(data.src)
object:getImage().x = data.x
object:getImage().y = data.y
object:setID(data.id)
object:setSoundOnClick(data.soundOnClick)

objectsTable[tempInt] = object
table.insert(loadedImages, object)
end

Hope that works (I haven’t tested it myself). :slight_smile:

David [import]uid: 152065 topic_id: 32255 reply_id: 128358[/import]

Hi,

Actually, looking at OWL source (which seems pretty clean) , you shoudn’t run into such an issue. OWL.instance is supposed to return new instances on each call. So maybe the problem is not OWL, but the way you’re using it.

From what I can see, you generate new objects looping though a table of level objects.
So, first of all, is that sure that all objects inside levelData.objects are disinct ones, with differents ids ?

I’m trying to read and then create objects in a for loop. The problem is that, the algorithm below is probably overwriting the same object because when I try to pull the data, it gives the same ID. How can I solve this issue?

Well, you store each new object inside a table loadedImages. So you will have to loop through this table to get your instances. But I don’t see any overwriting issue here.
Maybe you could show how you try to pull out these IDs ?

I’m using OWL library. It is an early stage, so I’m open to any other OOP library suggestions.

As I said, OWL look good. Four your information, though, I have a lightweight OOP framework, written in 30lines, with good stuff inside. You might want to check this out.

Hope this helps. [import]uid: 142361 topic_id: 32255 reply_id: 128389[/import]

@davidgmonical - Thank you for the help and I think it will work but that was something I didn’t want to do. If I run out of options, I’ll definitely give it a try.

@roland.yonaba - Yes, I checked it again and they are definitely different objects with different ID’s. I also tried printing out the values after and before I filled them. It does the work pretty good in the loop, reading different values but after the loop, I see it is returning same ID’s for different objects, again. I pasted the code below for how I get the objects.

[lua]local function fillLevelGroup(images, group)
local background = images[1]
background.isVisible = true
group:insert(background)

–THAT’S WHERE I PULL THEM
for i = 2, #images do
images[i]:getImage().isVisible = true
group:insert(images[i]:getImage())
end
end[/lua]

I was aware of your 30log library but preferred OWL because of more clear examples. You’re the developer so I should ask you, can I do the same loop without running into this problem with 30log? [import]uid: 154911 topic_id: 32255 reply_id: 128415[/import]

Hi,

Well, I am pretty sure you won’t get that kind of issue if everything is done right, with 30log. But, as I don’t know how you model your classes/instances, maybe you might want to stick with OWL. Fact is, OWL has hardcoded mechanisms that makes it compatible with Corona’s SDK API (predefined methods like that display_object stuff), while 30log just uses pure Lua, in order to be compatible with any game programming framework embedding Lua.
So if you want to use 30log , you might have some extra work to do.

Yet i’m not sure to catch the problem. So when you write this:

for key, data in pairs(levelData.objects) do  
 local object = owl.instance {from = GameObject}  
 -- etc   

Are you saying the local object returned by OWL.instance is always the same ?
If it is indeed the problem, you can try to print (for debug purposes) the table object itself. Lua will output the raw memory address of this table. If, at each step (in the for loop) you have the same address, then it means that the returned value from OWL.instance is the problem.

for key, data in pairs(levelData.objects) do  
 local object = owl.instance {from = GameObject}  
 print(key,object) --\> will show something like "... table: 0044AD28" each step  
end  

[import]uid: 142361 topic_id: 32255 reply_id: 128425[/import]

local objectsTable = {}
local tempInt = 0

for key, data in pairs(levelData.objects) do
tempInt = tempInt + 1
local object = owl.instance {from = GameObject}
object:setPNGImage(data.src)
object:getImage().x = data.x
object:getImage().y = data.y
object:setID(data.id)
object:setSoundOnClick(data.soundOnClick)

objectsTable[tempInt] = object
table.insert(loadedImages, object)
end

Hope that works (I haven’t tested it myself). :slight_smile:

David [import]uid: 152065 topic_id: 32255 reply_id: 128358[/import]

I guess I’ll stick with OWL, thanks for the tip :slight_smile:

I tried your way and it returned different table addresses but same ID.

[lua]function GameObject:setID(id)
GameObject.private.id = id
end

function GameObject:getID()
return GameObject.private.id
end[/lua]

[lua]for key, data in pairs(levelData.objects) do
local object = owl.instance {from = GameObject}
object:setPNGImage(data.src)
object:getImage().x = data.x
object:getImage().y = data.y
object:setID(data.id)
object:setSoundOnClick(data.soundOnClick)

table.insert(loadedImages, object)
end
print (loadedImages[2]:getID()) --returns cake, should return knife
print (loadedImages[3]:getID()) --returns cake
print (loadedImages[2]) – table: 00B75F90
print (loadedImages[3]) – table: 00B76530[/lua]

I also tried getting the image, and it returned the same for either objects. You see, they are different objects but I guess there is an addressing issue because it gets all the other fields same.I don’t know how Lua(or OWL) works with addresses and pointers but it just seems that way. Like the second object I created, points to the first object. [import]uid: 154911 topic_id: 32255 reply_id: 128504[/import]

Hi,

Actually, looking at OWL source (which seems pretty clean) , you shoudn’t run into such an issue. OWL.instance is supposed to return new instances on each call. So maybe the problem is not OWL, but the way you’re using it.

From what I can see, you generate new objects looping though a table of level objects.
So, first of all, is that sure that all objects inside levelData.objects are disinct ones, with differents ids ?

I’m trying to read and then create objects in a for loop. The problem is that, the algorithm below is probably overwriting the same object because when I try to pull the data, it gives the same ID. How can I solve this issue?

Well, you store each new object inside a table loadedImages. So you will have to loop through this table to get your instances. But I don’t see any overwriting issue here.
Maybe you could show how you try to pull out these IDs ?

I’m using OWL library. It is an early stage, so I’m open to any other OOP library suggestions.

As I said, OWL look good. Four your information, though, I have a lightweight OOP framework, written in 30lines, with good stuff inside. You might want to check this out.

Hope this helps. [import]uid: 142361 topic_id: 32255 reply_id: 128389[/import]

You have one more check to do. Introspection within the levelData.objects

print('Contents of levelData.objects')  
table.foreach(levelData.objects, function(k,v)  
 print((' Key: %s - Object : %s' - id: %s):format(k,v,v.id))  
end)  

Hope there are no errors inside, I wrote it from scratch.
This snippet will output all objects within levelData.objects, so that you can be sure that there are no duplicates inside.
If so, then it might be a bug from the lib.
Well, you could give a try to 30log (and I’ll be pleased to help) if your classes/objects attributes/methods are all defined in the code and do not inherit any property/behaviour from Corona’s SDK.

[import]uid: 142361 topic_id: 32255 reply_id: 128513[/import]

I’m really grateful for your efforts, thank you very much. I worked out the problem. It was something in the examples that I misread. The problem was all about defining properties like this:
[lua]GameObject.private.id = 1
–OR
local id = 1[/lua]

I had to define them like this and it solved the problem:
[lua]GameObject.id = 1

function GameObject:getID()
return self.id
end[/lua]

By the way, I will try and use your library for my attempts with Lua on PC. Thank you for all your efforts @roland.yonaba. [import]uid: 154911 topic_id: 32255 reply_id: 128516[/import]

You’re welcome. [import]uid: 142361 topic_id: 32255 reply_id: 128522[/import]

@davidgmonical - Thank you for the help and I think it will work but that was something I didn’t want to do. If I run out of options, I’ll definitely give it a try.

@roland.yonaba - Yes, I checked it again and they are definitely different objects with different ID’s. I also tried printing out the values after and before I filled them. It does the work pretty good in the loop, reading different values but after the loop, I see it is returning same ID’s for different objects, again. I pasted the code below for how I get the objects.

[lua]local function fillLevelGroup(images, group)
local background = images[1]
background.isVisible = true
group:insert(background)

–THAT’S WHERE I PULL THEM
for i = 2, #images do
images[i]:getImage().isVisible = true
group:insert(images[i]:getImage())
end
end[/lua]

I was aware of your 30log library but preferred OWL because of more clear examples. You’re the developer so I should ask you, can I do the same loop without running into this problem with 30log? [import]uid: 154911 topic_id: 32255 reply_id: 128415[/import]

Hi,

Well, I am pretty sure you won’t get that kind of issue if everything is done right, with 30log. But, as I don’t know how you model your classes/instances, maybe you might want to stick with OWL. Fact is, OWL has hardcoded mechanisms that makes it compatible with Corona’s SDK API (predefined methods like that display_object stuff), while 30log just uses pure Lua, in order to be compatible with any game programming framework embedding Lua.
So if you want to use 30log , you might have some extra work to do.

Yet i’m not sure to catch the problem. So when you write this:

for key, data in pairs(levelData.objects) do  
 local object = owl.instance {from = GameObject}  
 -- etc   

Are you saying the local object returned by OWL.instance is always the same ?
If it is indeed the problem, you can try to print (for debug purposes) the table object itself. Lua will output the raw memory address of this table. If, at each step (in the for loop) you have the same address, then it means that the returned value from OWL.instance is the problem.

for key, data in pairs(levelData.objects) do  
 local object = owl.instance {from = GameObject}  
 print(key,object) --\> will show something like "... table: 0044AD28" each step  
end  

[import]uid: 142361 topic_id: 32255 reply_id: 128425[/import]

I guess I’ll stick with OWL, thanks for the tip :slight_smile:

I tried your way and it returned different table addresses but same ID.

[lua]function GameObject:setID(id)
GameObject.private.id = id
end

function GameObject:getID()
return GameObject.private.id
end[/lua]

[lua]for key, data in pairs(levelData.objects) do
local object = owl.instance {from = GameObject}
object:setPNGImage(data.src)
object:getImage().x = data.x
object:getImage().y = data.y
object:setID(data.id)
object:setSoundOnClick(data.soundOnClick)

table.insert(loadedImages, object)
end
print (loadedImages[2]:getID()) --returns cake, should return knife
print (loadedImages[3]:getID()) --returns cake
print (loadedImages[2]) – table: 00B75F90
print (loadedImages[3]) – table: 00B76530[/lua]

I also tried getting the image, and it returned the same for either objects. You see, they are different objects but I guess there is an addressing issue because it gets all the other fields same.I don’t know how Lua(or OWL) works with addresses and pointers but it just seems that way. Like the second object I created, points to the first object. [import]uid: 154911 topic_id: 32255 reply_id: 128504[/import]

You have one more check to do. Introspection within the levelData.objects

print('Contents of levelData.objects')  
table.foreach(levelData.objects, function(k,v)  
 print((' Key: %s - Object : %s' - id: %s):format(k,v,v.id))  
end)  

Hope there are no errors inside, I wrote it from scratch.
This snippet will output all objects within levelData.objects, so that you can be sure that there are no duplicates inside.
If so, then it might be a bug from the lib.
Well, you could give a try to 30log (and I’ll be pleased to help) if your classes/objects attributes/methods are all defined in the code and do not inherit any property/behaviour from Corona’s SDK.

[import]uid: 142361 topic_id: 32255 reply_id: 128513[/import]

I’m really grateful for your efforts, thank you very much. I worked out the problem. It was something in the examples that I misread. The problem was all about defining properties like this:
[lua]GameObject.private.id = 1
–OR
local id = 1[/lua]

I had to define them like this and it solved the problem:
[lua]GameObject.id = 1

function GameObject:getID()
return self.id
end[/lua]

By the way, I will try and use your library for my attempts with Lua on PC. Thank you for all your efforts @roland.yonaba. [import]uid: 154911 topic_id: 32255 reply_id: 128516[/import]

You’re welcome. [import]uid: 142361 topic_id: 32255 reply_id: 128522[/import]