Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Adding delay to for-loop
Neelke Offline
Senior Member

Posts: 668
Threads: 82
Joined: Apr 2013
Reputation: 26
#1
Adding delay to for-loop

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

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 //.

Derp.
(This post was last modified: 11-05-2014, 08:58 AM by Neelke.)
11-05-2014, 08:57 AM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#2
RE: Adding delay to for-loop

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

PHP Code: (Select All)
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: (Select All)
TimerLoop("loop"); 
To stop the loop, just call
PHP Code: (Select All)
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.

(This post was last modified: 11-05-2014, 12:28 PM by Mudbill.)
11-05-2014, 12:23 PM
Find
burge4150 Offline
Member

Posts: 56
Threads: 15
Joined: Feb 2014
Reputation: 0
#3
RE: Adding delay to for-loop

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.
11-06-2014, 06:43 PM
Find
Neelke Offline
Senior Member

Posts: 668
Threads: 82
Joined: Apr 2013
Reputation: 26
#4
RE: Adding delay to for-loop

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.

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.

Derp.
11-06-2014, 10:10 PM
Find




Users browsing this thread: 1 Guest(s)