Forum

> > Stranded II > Scripts > loop free units deletes all units
Forums overviewStranded II overview Scripts overviewLog in to reply

English loop free units deletes all units

7 replies
To the start Previous 1 Next To the start

old loop free units deletes all units

JasJack67
Super User Off Offline

Quote
So in example #1 all units id 52 is deleted on the loop.

in example #2 the same happens, all units are deleted, but I wanted the units that are dead to remain, and only have the loop delete the units that are alive "lives".

More >


Can anyone tell me why #2 it still deletes both live and dead units? Instead, I want it to separate the live from dead, and delete the live ones, so the player can loot the dead?

If it can not be done this way...might I have to "alter object" so when the player kills the unit, it is deleted and a new dead unit has a new id...so the loop above does not count them? I can do this script, but I just think the above would be better in this case, if i can get it to work.
edited 1×, last 31.12.13 11:13:35 pm

old Re: loop free units deletes all units

Hurri04
Super User Off Offline

Quote
1
2
3
4
5
6
7
8
9
on:clearevents {
    loop("units", 52) {
         if(lives("unit", loop_id()) == 1);
             free "unit", loop_id();
         }
    }
    msg "WOLVES FLED!", 1, 6000;
    freetext 3;
}
this is all you need.

old Re: loop free units deletes all units

JasJack67
Super User Off Offline

Quote
your so awsome Hurri!!!

ty much again!

hope your having great holidays !

EDIT:
hmm...it is not deleting any of the wolves. I fixed 2 errors in the script...replaced ; added { bracket at the end of If lives ...then fixed a mismatched brackets error in editor as shown below in the if lives statement too.

1
2
3
4
5
6
7
8
9
10
11
if (count("unit",52)>=1){
                         freetext 3;
                         msg "WOLVES FLED!",1,6000;
                         loop("units", 52) {
                                if ((lives "unit", loop_id()) == 1){
                                        free "unit", loop_id();
                                }
                         }
                }else{
                         freetext 3;
                }

i need to count units first to see if any are spawned or I dont want it to even loop check anything...and i needed to clear a previous text 3 if there are no wolves too. But the sctual loop is not deleting live or dead as it stands. Anything else you might see here?
edited 6×, last 01.01.14 12:33:31 am

old Re: loop free units deletes all units

Nova
User Off Offline

Quote
Wrong:
1
if ((lives "unit", loop_id()) == 1)
Right:
1
if (lives ("unit", loop_id()) == 1)


Stranded II has sometimes problems with the use of return values inside of commands. Try to save the value of s2 cmd loop_id() inside a variable and use this for s2 cmd lives.
1
2
$id_loop = loop_id();
if (lives ("unit", $id_loop) == 1)

old Re: loop free units deletes all units

Hurri04
Super User Off Offline

Quote
you dont need to check how many units are on the map because if their number is 0 then the loop will simply run 0 times.

and you do not need the if because you use the "freetext 3" either way anyways.

here's a script that should basically do the same but it writes 2 lines into the console so you can see how many wolves there are on the map and how many of them are dead:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
on:clearevents {
    $wolves = count("unit", 52);
    $dead = 0;
    echo "There are $wolves wolves";
    loop("units", 52) {
        $loop_id = loop_id();
        if(lives("unit", $loop_id) == 1) {
            free "unit", $loop_id;
        }else{
            $dead++;
        }
    }
    echo "$dead wolves are dead";
    msg "WOLVES FLED!", 1, 6000;
    freetext 3;
}

old Re: loop free units deletes all units

JasJack67
Super User Off Offline

Quote
ok guys i got what i wanted now...thankyou!

With your help, I have what I wanted working...the reason I still needed to "count" units at the start is because there is more then just the wolves I needed the script to check...as you can see in the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
on:clearevents { 
                if (count("unit",52)>=1){
                       loop("units", 52) {
                           $loop_id = loop_id();
                           if(lives("unit", $loop_id) == 1) {
                               free "unit", $loop_id;
                           }
                       }
                       freetext 3;
                       msg "WOLVES FLED!", 1, 6000;
                }elseif (count("unit",54)>=1){
                       loop("units", 54) {
                           $loop_id = loop_id();
                           if(lives("unit", $loop_id) == 1) {
                               free "unit", $loop_id;
                           }
                       }
                       freetext 3;
                       msg "BATS FLED!", 1, 6000;
                }elseif (count("unit",53)>=1){
                       loop("units", 53) {
                           $loop_id = loop_id();
                           if(lives("unit", $loop_id) == 1) {
                               free "unit", $loop_id;
                           }
                       }
                       freetext 3;
                       msg "DEER FLED!", 1, 6000;
                }elseif (count("unit",55)>=1){
                       loop("units", 55) {
                           $loop_id = loop_id();
                           if(lives("unit", $loop_id) == 1) {
                               free "unit", $loop_id;
                           }
                       }
                       freetext 3;
                       msg "BEAR FLED!", 1, 6000;
                }else{
                    freetext 3;
                }
        }

So this loops through the units to the one type, where a different script has spawned an x amount of units. Once it chooses the type that is present, it deletes the ones that are alive, leaving the dead ones for the player to loot. if it does not find any of the units it will simply delete the text3 saying one type is present. (but there is none present because the player killed them all, there fore it needs to just delete the text3)
edited 3×, last 01.01.14 04:24:01 am

old Re: loop free units deletes all units

JasJack67
Super User Off Offline

Quote
Absolutely! Thank you Hurri for your help on this...now I can post my new years update with this fix i been working on.

For anyone who is interested in using that above script, here is the other script that actually spawns the animals that the above script clears.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
on:nightevents {                     //SPAWN NIGHT EVENT ANIMALS.
                if ($difficulty>1) {         //ONLY ON NORMAL OR HARD MODE.
                      $spn=random(1,3);
                      if ($spn==1){
                                 msg "WOLVES ARE NEAR!",3,6000;   // WOLF EVENT
                                 play "wolfhowl.wav";
                                 freetext 3;
                                 text 3,"WOLVES!",3,36,65,2;
                                 loop ("count",3){ $id=randomcreate("unit",52,10,100000); }
                      }elseif ($spn==2){
                                 msg "BATS ARE NEAR!",1,6000;   //BAT EVENT
                                 play "kiwi_die.wav";
                                 freetext 3;
                                 text 3,"BATS!",1,36,65,2;
                                 loop ("count",30){ $id=randomcreate("unit",54,50,100000); }
                      }elseif ($spn==3){
                                 msg "BEARS ARE NEAR!",3,6000;   //BEAR EVENT
                                 play "bear.wav";
                                 freetext 3;
                                 text 3,"BEAR!",3,36,65,2;
                                 loop ("count",1){ $id=randomcreate("unit",55,50,100000); }
                      }else{
                        //nothing - you can add something else here.
                      }
                }
        }

        on:dayevents {                      //SPAWN DAY EVENT ANIMALS.
                $spn=random(1,3);
                      if ($spn==3){                            //ON EASY, NORMAL, OR HARD MODE.
                                 msg "DEER ARE NEAR!",4,6000;   //DEER EVENT   
                                 play "deer.wav";
                                 freetext 3;
                                 text 3,"DEER!",4,36,65,2;
                                 loop ("count",3){ $id=randomcreate("unit",53,50,100000); }
                      }
                      if ($difficulty>1) {                     //ONLY ON NORMAL OR HARD MODE.
                                 if ($spn==1){
                                            msg "WOLVES ARE NEAR!",3,6000;   // WOLF EVENT
                                            play "wolfhowl.wav";
                                            freetext 3;
                                            text 3,"WOLVES!",3,36,65,2;
                                            loop ("count",3){ $id=randomcreate("unit",52,10,100000); }
                                 }elseif ($spn==2){
                                            msg "A BEAR IS NEAR!",3,6000;   //BEAR EVENT
                                            play "bear.wav";
                                            freetext 3;
                                            text 3,"BEAR!",3,36,65,2;
                                            loop ("count",2){ $id=randomcreate("unit",55,50,100000); }
                                 }else{
                                            //nothing - you can add something else here.
                                 }
                     }
        }

i dont know why the "code" box is not working
To the start Previous 1 Next To the start
Log in to reply Scripts overviewStranded II overviewForums overview