Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

stack overflow on recursive function. Not sure how to fix.
Started by joey.defourneaux Nov 14 2013 02:20 AM

- - - - -
2 replies to this topic
lua corona

Best Answer WideAwakeGames , 14 November 2013 - 07:55 AM

try return scene.targeting(params) instead. 

 

http://www.lua.org/pil/6.3.html

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

joey.defourneaux

[GLOBAL: userInfoPane.html]
joey.defourneaux
  • Observer

  • 14 posts
  • Corona SDK

For some reason the code below throws a stack overflow error if the else statement gets executed too many times. I am trying to have the scene.targeting function select a target from the objTable passed in the params, but only targets with a .tgtFlag == false are valid selections. If the function selects a target that has a .tgtFlag == true, it recalls the scene.targeting function passing in the same set of params.

The line that breaks is "local theTarget = params.objTable[math.random( 1, #params.objTable )]" but only after "else scene.targeting(params) end" is called several times.

Any help would be greatly appreciated.

function scene.targeting( params ) -- Targeting functions
  
  function animateTarget( target )
    if target.savedFlag == false then
      transition.to( target, {time = 100, y = target.y - 15} )
      transition.to( target, {time = 100, delay = 150, y = target.y, onComplete = animateTarget} )
    end
  end
  
  -- something is broken on line 79 or line 84. Getting a stack overflow whene else statement gets executed repeatedly.
  local theTarget = params.objTable[math.random( 1, #params.objTable )]
  if theTarget.tgtFlag == false then
    theTarget.tgtFlag = true
    animateTarget(theTarget)
  else
    scene.targeting(params)
  end
end

 



[TOPIC: post.html]
#2

WideAwakeGames

[GLOBAL: userInfoPane.html]
WideAwakeGames
  • Contributor

  • 143 posts
  • Corona SDK

  Best Answer

try return scene.targeting(params) instead. 

 

http://www.lua.org/pil/6.3.html



[TOPIC: post.html]
#3

joey.defourneaux

[GLOBAL: userInfoPane.html]
joey.defourneaux
  • Observer

  • 14 posts
  • Corona SDK

Thanks. Didn't know about tail calls. Thats a handy trick.




[topic_controls]
[/topic_controls]