Need help on calling a function as a string

Ok, I’m not sure if what I need is possible in lua, but I need to call a function and pass parameters that I will be generating dynamically, so what I need is something like this:

for example:
[lua]callfunc(“display.newRect(param1,param2,param3)”)[/lua]

Not sure if it’s even possible, but I want to be able to call the function as a generated string, sometimes it’ll have 100 parameters, sometimes it will have 4, it depends on the scenario, it will be generated randomly, and I can’t change the function to receive a list, since it’s for a corona function that can recieve as many parameters as I wish [import]uid: 53195 topic_id: 31911 reply_id: 331911[/import]

Hi there,

No, I don’t believe you can do what you’re describing.

But I’d like to understand better why you think you need to do it that way. Can you explain your use case in more detail. My guess is that you may want to pass a table to your function with all the parameters, and if need be, use the [lua]unpack()[/lua] function in lua to unpack the table into a list of parameters.

  • Andrew [import]uid: 109711 topic_id: 31911 reply_id: 127281[/import]

Thanks for the quick reply. The reason I’m trying to do this is because I have this function that generates a certain amount of shapes for a physics body, and there’s no predicting how many shapes it will have, I’ve got the following:

[lua]shapes = defineShapes()
physics.addBody(drawingLine,
shapes[1], shapes[2], shapes[3], shapes[4], shapes[5], shapes[6], shapes[7], shapes[8], shapes[9], shapes[10], shapes[11], shapes[12], shapes[13], shapes[14], shapes[15], shapes[16], shapes[17], shapes[18], shapes[19], shapes[20], shapes[21], shapes[22], shapes[23], shapes[24], shapes[25], shapes[26], shapes[27], shapes[28], shapes[29], shapes[30], shapes[31], shapes[32], shapes[33], shapes[34], shapes[35], shapes[36], shapes[37], shapes[38], shapes[39], shapes[40], shapes[41], shapes[42], shapes[43], shapes[44], shapes[45], shapes[46], shapes[47], shapes[48], shapes[49], shapes[50], shapes[51], shapes[52], shapes[53], shapes[54], shapes[55], shapes[56], shapes[57], shapes[58], shapes[59], shapes[60], shapes[61], shapes[62], shapes[63], shapes[64], shapes[65], shapes[66], shapes[67], shapes[68], shapes[69], shapes[70], shapes[71], shapes[72], shapes[73], shapes[74], shapes[75], shapes[76], shapes[77], shapes[78], shapes[79], shapes[80], shapes[81], shapes[82], shapes[83], shapes[84], shapes[85], shapes[86], shapes[87], shapes[88], shapes[89], shapes[90], shapes[91], shapes[92], shapes[93], shapes[94], shapes[95], shapes[96], shapes[97], shapes[98], shapes[99], shapes[100], shapes[101], shapes[102], shapes[103], shapes[104], shapes[105], shapes[106], shapes[107], shapes[108], shapes[109], shapes[110], shapes[111], shapes[112], shapes[113], shapes[114], shapes[115], shapes[116], shapes[117], shapes[118], shapes[119], shapes[120], shapes[121], shapes[122], shapes[123], shapes[124], shapes[125], shapes[126], shapes[127], shapes[128], shapes[129], shapes[130], shapes[131], shapes[132], shapes[133], shapes[134], shapes[135], shapes[136], shapes[137], shapes[138], shapes[139], shapes[140], shapes[141], shapes[142], shapes[143], shapes[144], shapes[145], shapes[146], shapes[147], shapes[148], shapes[149], shapes[150], shapes[151], shapes[152], shapes[153], shapes[154], shapes[155], shapes[156], shapes[157], shapes[158], shapes[159], shapes[160], shapes[161], shapes[162], shapes[163], shapes[164], shapes[165], shapes[166], shapes[167], shapes[168], shapes[169], shapes[170], shapes[171], shapes[172], shapes[173], shapes[174], shapes[175], shapes[176], shapes[177], shapes[178], shapes[179], shapes[180], shapes[181], shapes[182], shapes[183], shapes[184], shapes[185], shapes[186], shapes[187], shapes[188], shapes[189], shapes[190], shapes[191], shapes[192], shapes[193], shapes[194], shapes[195], shapes[196], shapes[197], shapes[198], shapes[199], shapes[200]
)

function defineShapes()
local shapes = nil
shapes = {}
local num = math.floor(drawingLine.numChildren)

print(num)
for i=1, num do
lw = lineWidth/2
sh=nil
sh = {-lw+drawingLine[i].x, -lw+drawingLine[i].y,
-lw+drawingLine[i].x, lw+drawingLine[i].y,
lw+drawingLine[i].x, lw+drawingLine[i].y,
lw+drawingLine[i].x, -lw+drawingLine[i].y}

shapes[i] = {shape=sh, filter={groupIndex=1}}
end
return shapes
end [/lua]

This is just horrible, and there’s no predicting how many shapes there will be, this example works if there are 200 shapes, but there can be 3, 9, 186, 290, anything. And so I wanted to be able to generate the params as a string, and define there how many calls will it have [import]uid: 53195 topic_id: 31911 reply_id: 127283[/import]

Ok, the unpack() worked perfectly for that, now the problem that I have is that the object collisions are not being detected for some reason, it just falls and has interaction with the ground since it stops, but can’t collide with other objects generated in the same way, and it doesn’t rotate or anything, just stays there… there’s something weird with collision detection on multibody objects [import]uid: 53195 topic_id: 31911 reply_id: 127288[/import]

Hi there,

Thanks for posting some code, that helps me understand your question a bit better.

First, there’s one question I wanted to clarify. Are you (1) trying to create a single complex physics body with a large number of parts (i.e. a single body with X parts, where X can be any number)? Or are you (2) trying to create X number of separate , independent physics bodies (where X can be any number)? It sounds like you’re trying to do (1), but I wanted to check.

To do (1), you should do something like this:
[blockcode]
shapes = defineShapes()
physics.addBody(drawingLine, “dynamic”, unpack(shapes))
[/blockcode]

Perhaps that’s what you’re already doing, since you said you got that aspect working.

As for the problem you’re having with collisions, I haven’t had any collision issues with multibody objects. I’d suggest you try two things. First, try removing the collision filter that you set up with groupIndex, just to rule that out as a potential issue. Second, in your [lua]defineShapes()[/lua] function, it looks to me like you’re defining the points on the shape in counterclockwise order, but you need to define them in clockwise order (see here: http://developer.coronalabs.com/reference/index/physicsaddbody).

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31911 reply_id: 127305[/import]

It was the (1), you really helped me here! The unpack() worked wonders, and the collision problem was as you said, I was creating the shapes in counter-clockwise order. THANK YOU! ^^ [import]uid: 53195 topic_id: 31911 reply_id: 127360[/import]

Hi there,

No, I don’t believe you can do what you’re describing.

But I’d like to understand better why you think you need to do it that way. Can you explain your use case in more detail. My guess is that you may want to pass a table to your function with all the parameters, and if need be, use the [lua]unpack()[/lua] function in lua to unpack the table into a list of parameters.

  • Andrew [import]uid: 109711 topic_id: 31911 reply_id: 127281[/import]

Thanks for the quick reply. The reason I’m trying to do this is because I have this function that generates a certain amount of shapes for a physics body, and there’s no predicting how many shapes it will have, I’ve got the following:

[lua]shapes = defineShapes()
physics.addBody(drawingLine,
shapes[1], shapes[2], shapes[3], shapes[4], shapes[5], shapes[6], shapes[7], shapes[8], shapes[9], shapes[10], shapes[11], shapes[12], shapes[13], shapes[14], shapes[15], shapes[16], shapes[17], shapes[18], shapes[19], shapes[20], shapes[21], shapes[22], shapes[23], shapes[24], shapes[25], shapes[26], shapes[27], shapes[28], shapes[29], shapes[30], shapes[31], shapes[32], shapes[33], shapes[34], shapes[35], shapes[36], shapes[37], shapes[38], shapes[39], shapes[40], shapes[41], shapes[42], shapes[43], shapes[44], shapes[45], shapes[46], shapes[47], shapes[48], shapes[49], shapes[50], shapes[51], shapes[52], shapes[53], shapes[54], shapes[55], shapes[56], shapes[57], shapes[58], shapes[59], shapes[60], shapes[61], shapes[62], shapes[63], shapes[64], shapes[65], shapes[66], shapes[67], shapes[68], shapes[69], shapes[70], shapes[71], shapes[72], shapes[73], shapes[74], shapes[75], shapes[76], shapes[77], shapes[78], shapes[79], shapes[80], shapes[81], shapes[82], shapes[83], shapes[84], shapes[85], shapes[86], shapes[87], shapes[88], shapes[89], shapes[90], shapes[91], shapes[92], shapes[93], shapes[94], shapes[95], shapes[96], shapes[97], shapes[98], shapes[99], shapes[100], shapes[101], shapes[102], shapes[103], shapes[104], shapes[105], shapes[106], shapes[107], shapes[108], shapes[109], shapes[110], shapes[111], shapes[112], shapes[113], shapes[114], shapes[115], shapes[116], shapes[117], shapes[118], shapes[119], shapes[120], shapes[121], shapes[122], shapes[123], shapes[124], shapes[125], shapes[126], shapes[127], shapes[128], shapes[129], shapes[130], shapes[131], shapes[132], shapes[133], shapes[134], shapes[135], shapes[136], shapes[137], shapes[138], shapes[139], shapes[140], shapes[141], shapes[142], shapes[143], shapes[144], shapes[145], shapes[146], shapes[147], shapes[148], shapes[149], shapes[150], shapes[151], shapes[152], shapes[153], shapes[154], shapes[155], shapes[156], shapes[157], shapes[158], shapes[159], shapes[160], shapes[161], shapes[162], shapes[163], shapes[164], shapes[165], shapes[166], shapes[167], shapes[168], shapes[169], shapes[170], shapes[171], shapes[172], shapes[173], shapes[174], shapes[175], shapes[176], shapes[177], shapes[178], shapes[179], shapes[180], shapes[181], shapes[182], shapes[183], shapes[184], shapes[185], shapes[186], shapes[187], shapes[188], shapes[189], shapes[190], shapes[191], shapes[192], shapes[193], shapes[194], shapes[195], shapes[196], shapes[197], shapes[198], shapes[199], shapes[200]
)

function defineShapes()
local shapes = nil
shapes = {}
local num = math.floor(drawingLine.numChildren)

print(num)
for i=1, num do
lw = lineWidth/2
sh=nil
sh = {-lw+drawingLine[i].x, -lw+drawingLine[i].y,
-lw+drawingLine[i].x, lw+drawingLine[i].y,
lw+drawingLine[i].x, lw+drawingLine[i].y,
lw+drawingLine[i].x, -lw+drawingLine[i].y}

shapes[i] = {shape=sh, filter={groupIndex=1}}
end
return shapes
end [/lua]

This is just horrible, and there’s no predicting how many shapes there will be, this example works if there are 200 shapes, but there can be 3, 9, 186, 290, anything. And so I wanted to be able to generate the params as a string, and define there how many calls will it have [import]uid: 53195 topic_id: 31911 reply_id: 127283[/import]

Ok, the unpack() worked perfectly for that, now the problem that I have is that the object collisions are not being detected for some reason, it just falls and has interaction with the ground since it stops, but can’t collide with other objects generated in the same way, and it doesn’t rotate or anything, just stays there… there’s something weird with collision detection on multibody objects [import]uid: 53195 topic_id: 31911 reply_id: 127288[/import]

Hi there,

Thanks for posting some code, that helps me understand your question a bit better.

First, there’s one question I wanted to clarify. Are you (1) trying to create a single complex physics body with a large number of parts (i.e. a single body with X parts, where X can be any number)? Or are you (2) trying to create X number of separate , independent physics bodies (where X can be any number)? It sounds like you’re trying to do (1), but I wanted to check.

To do (1), you should do something like this:
[blockcode]
shapes = defineShapes()
physics.addBody(drawingLine, “dynamic”, unpack(shapes))
[/blockcode]

Perhaps that’s what you’re already doing, since you said you got that aspect working.

As for the problem you’re having with collisions, I haven’t had any collision issues with multibody objects. I’d suggest you try two things. First, try removing the collision filter that you set up with groupIndex, just to rule that out as a potential issue. Second, in your [lua]defineShapes()[/lua] function, it looks to me like you’re defining the points on the shape in counterclockwise order, but you need to define them in clockwise order (see here: http://developer.coronalabs.com/reference/index/physicsaddbody).

Hope this helps.

  • Andrew [import]uid: 109711 topic_id: 31911 reply_id: 127305[/import]

It was the (1), you really helped me here! The unpack() worked wonders, and the collision problem was as you said, I was creating the shapes in counter-clockwise order. THANK YOU! ^^ [import]uid: 53195 topic_id: 31911 reply_id: 127360[/import]