Frictional Games Forum (read-only)
[SCRIPT] Adding delay to for-loop - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [SCRIPT] Adding delay to for-loop (/thread-27105.html)



Adding delay to for-loop - Neelke - 11-05-2014

So I made this custom function that allows me to turn off spider eggs and spiders by writing in int parameters.

Code:
string gNameOfSpiderEggs = "organic_spider_egg_small_";
string gNameOfBrokenSpiderEggs = "organic_spider_egg_small_broken_";
string gNameOfSpiderExplodeSound = "enemy_spider_egg_open";
string gNameOfSpider = "necr_spider_";

void SetSpiderEggOff(int iMinAmount, int iMaxAmount)
{
for(int i=iMinAmount; i<=iMaxAmount; ++i)
{
  SetEntityActive(gNameOfSpiderEggs+""+i, false);
  SetEntityActive(gNameOfSpider+""+i, true);
  SetEntityActive(gNameOfBrokenSpiderEggs+""+i, true);
  
  //CreateParticleSystemAtEntity(gNameOfSpiderEggs,
         //"ps_spider_egg_splat",
         //gNameOfSpiderEggs+""+i,
         //false);
  
  PlaySoundAtEntity(gNameOfSpiderEggs,
       gNameOfSpiderExplodeSound,
       gNameOfSpiderEggs+""+i,
       0.0f,
       false);
}
}

Now what I want to add in, is a slight delay for the for-loop. For example, first spider spawn immediately, then after 1 second next one spawn, then after 0.5 seconds next one spawn and etc. I tried using a while loop, but it just froze my game. Is there a way of doing this another way? Or is there an actual way to do it with the while loop?

Sidenote: The particle system crashes the game. Reason its //.


RE: Adding delay to for-loop - Mudbill - 11-05-2014

Why use a for-loop? How about a timer loop?

PHP Code:
int count 0;

void TimerLoop(string &in asTimer)
{
    
SetEntityActive(gNameOfSpiderEggs+""+countfalse);
    
SetEntityActive(gNameOfSpider+""+counttrue);
    
SetEntityActive(gNameOfBrokenSpiderEggs+""+counttrue);

    
count++; 
    
AddTimer(asTimerRandFloat(0.3f1.7f), "TimerLoop");


A for-loop will repeat the same action for a certain amount of times. It would work if Amnesia had any form of Thread.sleep(); function, but it does not as far as I'm aware of. A while-loop crashes you because it loops infinitely as long as the boolean question is accepted.

This Timer-loop is basically just an infintely looping timer. It repeats with a random delay to each iteration. Some of the variables I used can be replaced by the function versions (by that I mean the SetLocalVarInt instead of primitive int) if you prefer those.

To start the loop, just call
PHP Code:
TimerLoop("loop"); 
To stop the loop, just call
PHP Code:
RemoveTimer("loop"); 

You can add further checks to it so that it will only repeat for your specified Min and Max amounts, but those are simple if-statements. You can probably figure out what needs tweaking.


RE: Adding delay to for-loop - burge4150 - 11-06-2014

Can also just have the function call itself from within itself (recursive I think it's called?) with a timer. Use an IF statement to check when it no longer needs to call itself.


RE: Adding delay to for-loop - Neelke - 11-06-2014

Considering I have around 20 spider eggs or something in this level, it would make the script quite long eventually. So I'm simplifying it this way. But either way, I got it working just recently. So I used Mudbill's suggestion with adding in a timer to control the spawning functions easier.

Code:
string gNameOfSpiderEggs = "organic_spider_egg_small_";
string gNameOfBrokenSpiderEggs = "organic_spider_egg_small_broken_";
string gNameOfSpiderExplodeSound = "enemy_spider_egg_open";
string gNameOfSpider = "necr_spider_";

void SetSpiderEggOff(string &in asTimer, int iMinimumValue, int iMaximumValue)
{
    AddTimer(asTimer, RandFloat(0.6f, 1.0f), asTimer);
    SetLocalVarInt("MaxValue", iMaximumValue);
    SetLocalVarInt("MinValue", iMinimumValue);
}

void TimerSetSpiderEggOff(string &in asTimer)
{    
    if(GetLocalVarInt("MinValue") == GetLocalVarInt("MaxValue"))
    {
        SetLocalVarInt("MaxValue", 0);
        SetLocalVarInt("MinValue", 0);
        return;
    }
    
    AddLocalVarInt("MinValue", 1);
    
    SetEntityActive(gNameOfSpiderEggs+""+GetLocalVarInt("MinValue"), false);
    SetEntityActive(gNameOfSpider+""+GetLocalVarInt("MinValue"), true);
    SetEntityActive(gNameOfBrokenSpiderEggs+""+GetLocalVarInt("MinValue"), true);
        
    //CreateParticleSystemAtEntity(gNameOfSpiderEggs,
                                //"ps_spider_egg_splat",
                                //gNameOfSpiderEggs+""+GetLocalVarInt("MinValue"),
                                //false);
        
    PlaySoundAtEntity(gNameOfSpiderEggs,
                        gNameOfSpiderExplodeSound,
                        gNameOfSpiderEggs+""+GetLocalVarInt("MinValue"),
                        0.0f,
                        false);
    
    AddTimer(asTimer, RandFloat(0.6f, 1.0f), asTimer);
}

Either way, Mudbill solved most things. Thanks guys.