call stack overflow, recursive call problem?

Started by arj0n, Wed 15/07/2020 15:22:19

Previous topic - Next topic

arj0n

On top of my local script I have the following struct:
Spoiler

Code: ags

struct PaletteCycle
{
  String system;
  String game;
  String Year;
  int palette0r;
  int palette0g;
  int palette0b;
  int palette1r;
  int palette1g;
  int palette1b;
  int palette2r;
  int palette2g;
  int palette2b;
  int palette3r;
  int palette3g;
  int palette3b;
  bool UsedForCycle;
};

PaletteCycle palettecycles[60];

[close]

The room_AfterFadeIn first calls a function declaring the arrays (from 0 to 59), followed by calling the select_random_palette function (which randomly selects one of the 60 not yet selected palette, updates this palette, waits x time, and rerun the function until all 60 palettes has been chosen.
if a palette has been selected, the bool UsedForCycle is set to true and the function is called again.

Here's the looping select_random_palette function:

Spoiler

Code: ags

function select_random_palette()
{
  if (ranPaletteTotal < 60)
  {
    ranPalette=Random(59);
    
    if (palettecycles[ranPalette].UsedForCycle == true)
    {
      select_random_palette();
    }
    else if (palettecycles[ranPalette].UsedForCycle == false)
    {
      ranPaletteTotal++;
      palettecycles[ranPalette].UsedForCycle = true;
      
      Lamountpalettes.Text = String.Format("palettes shown: %d", ranPaletteTotal);
      
      palette[0].r = palettecycles[ranPalette].palette0r;
      palette[0].g = palettecycles[ranPalette].palette0g;
      palette[0].b = palettecycles[ranPalette].palette0b;
      
      palette[1].r = palettecycles[ranPalette].palette1r;
      palette[1].g = palettecycles[ranPalette].palette1g;
      palette[1].b = palettecycles[ranPalette].palette1b;
      
      palette[2].r = palettecycles[ranPalette].palette2r;
      palette[2].g = palettecycles[ranPalette].palette2g;
      palette[2].b = palettecycles[ranPalette].palette2b;
      
      palette[3].r = palettecycles[ranPalette].palette3r;
      palette[3].g = palettecycles[ranPalette].palette3g;
      palette[3].b = palettecycles[ranPalette].palette3b;
      
      UpdatePalette();
      Wait (10);//waittimecycle
      
      select_random_palette();
    }
  }
  else if (ranPaletteTotal == 60)
  {
    SetTimer (1, 300);
    SetTimer (2, 60);
    return;
  }
}

[close]

The select_random_palette function works fine until for some reason it gets a 'call stack overflow' error, usually somewhere between the 30th and 40th palette update (out of the 60 palettes)...
When it happens, the line highlighted in the editor is one of the two "select_random_palette();" lines in the select_random_palette function

The full error msg:
Error running function 'room_AfterFadeIn':
Error: call stack overflow, recursive call problem?


I can't seem to figure out why this is happening, any idea anyone?

Khris

#1
You're calling  select_random_palette()  inside itself, and the outer function will not finish until the inner function does. They do not run in sequence but parallel, and each new function call adds to the call stack, until it overflows.

To run a function multiple times in sequence, use a for loop (blocking) or a timer (non-blocking).

arj0n


SMF spam blocked by CleanTalk