Runtime question

Hi guys, im having a hard time trying to solve this.

 

Basically, i want to make this work:

 

 

 

 

 

 

for i = 1,stickQuantity do function npc\_dmg(i) if npc[i].x == castle.x then castle.hp = castle.hp - npc[i].dmg end end Runtime:addEventListener("enterFrame",npc\_dmg(i))

Something like that, where you make a function per npc and then assign a runtime event.

First of all, its not working because it seems like i cant pass parameters to a function when I call it from a runtime event listener.

Second, I find it very un-optimized to add one function per mob, specially if i make 100 and that includes their event listeners too. 

Is there any way to make this simplier(and that it works ofc haha) i’ve been hitting my head into a wall for the last day trying to figure this out.

Thanks in advance.

First, code is wrong.

inside for loop ‘end’ is missing, what is ‘i’ in Runtime:addEventListener and why creating global function in loop ?

Correct this if you pasted just part of code 

Sorry, my bad with the code, it had a lot more so i had to cut off some stuff, but seems i made it useless.

This should be ok:

local index = {} local npc = {} local castleT = {} local castle = display.newImage("castle.bmp") castleT.hp = 1000 local function spawn() index = index +1 --spawns npc npc[index] = dispay.newImage("npc.bmp") npc[index].dmg = 20 --moves the npc to the castle position end timer.performWithDelay(300,spawn,3) for i = 1,stickQuantity do local function npc\_dmg(i) if npc[i].x == castleT.x then castle.hp = castle.hp - npc[i].dmg end end end Runtime:addEventListener("enterFrame",npc\_dmg(SI)) 

First, adding one function for each mob, i feel its not a good idea. Second, i have problem with the runtime, i cant pass parameters to it.

Again, I think we need to assume you haven’t posted all of your code, because stickQuantity still isn’t specified. Let’s presume for the time being that you have specified stickQuantity; in the function you posted, you are already specifying i as the variable to npc_dmg, which already exists in your for loop. You might want to change it from i to n or something else, if you want to pass variables to a function. 

Two other things: First, I don’t see SI defined in your code above, but I’m going to presume that exists somewhere as well. Second, if you’re going to pass variables to a locally nested function, you should declare the function name at the beginning of your code to ensure everything runs smoothly. Something like this:

local npc\_dmg for i = 1, stickQuantity do function npg\_dmg() --do something end end

I would think something more like this would be in order:

  local function npc\_dmg(i)     if npc[i].x == castleT.x then         castle.hp = castle.hp - npc[i].dmg     end end   local function check\_npc\_damage()     for i = 1, #npc do         npc\_dmg(i)     end end Runtime:addEventListener("enterFrame",check\_npc\_damage) 

That will loop over all your npc’s.  As posted above we still don’t know what the stickQuantity is trying to loop over.

Thanks for the answers guys.

I use stickQuantity to manage the ammount of enemies the level is going to have

local stickQuantity = 4

for i = 1,stickQuantity do

    table.insert(stickTest,i)

end

and SI is just a table   (SI = {})

Thanks panc for the tip. Rob i’ll test it and let you know

First, code is wrong.

inside for loop ‘end’ is missing, what is ‘i’ in Runtime:addEventListener and why creating global function in loop ?

Correct this if you pasted just part of code 

Sorry, my bad with the code, it had a lot more so i had to cut off some stuff, but seems i made it useless.

This should be ok:

local index = {} local npc = {} local castleT = {} local castle = display.newImage("castle.bmp") castleT.hp = 1000 local function spawn() index = index +1 --spawns npc npc[index] = dispay.newImage("npc.bmp") npc[index].dmg = 20 --moves the npc to the castle position end timer.performWithDelay(300,spawn,3) for i = 1,stickQuantity do local function npc\_dmg(i) if npc[i].x == castleT.x then castle.hp = castle.hp - npc[i].dmg end end end Runtime:addEventListener("enterFrame",npc\_dmg(SI)) 

First, adding one function for each mob, i feel its not a good idea. Second, i have problem with the runtime, i cant pass parameters to it.

Again, I think we need to assume you haven’t posted all of your code, because stickQuantity still isn’t specified. Let’s presume for the time being that you have specified stickQuantity; in the function you posted, you are already specifying i as the variable to npc_dmg, which already exists in your for loop. You might want to change it from i to n or something else, if you want to pass variables to a function. 

Two other things: First, I don’t see SI defined in your code above, but I’m going to presume that exists somewhere as well. Second, if you’re going to pass variables to a locally nested function, you should declare the function name at the beginning of your code to ensure everything runs smoothly. Something like this:

local npc\_dmg for i = 1, stickQuantity do function npg\_dmg() --do something end end

I would think something more like this would be in order:

  local function npc\_dmg(i)     if npc[i].x == castleT.x then         castle.hp = castle.hp - npc[i].dmg     end end   local function check\_npc\_damage()     for i = 1, #npc do         npc\_dmg(i)     end end Runtime:addEventListener("enterFrame",check\_npc\_damage) 

That will loop over all your npc’s.  As posted above we still don’t know what the stickQuantity is trying to loop over.

Thanks for the answers guys.

I use stickQuantity to manage the ammount of enemies the level is going to have

local stickQuantity = 4

for i = 1,stickQuantity do

    table.insert(stickTest,i)

end

and SI is just a table   (SI = {})

Thanks panc for the tip. Rob i’ll test it and let you know