help with random script

Gurtag

Member
hey guys, i been using an random script i got from digging old treads think ilu post it, use it to display ramdom backgounds/panels in a level, i use it in a none type entity with several follows i spawn on the level, my idea is to make kind of mortal kombat tower like game mode in wich every level is random(only the visual part) it works but there are 2 issues i would like to solve and they are out of my knoledge... first thing every time i start the game and start say gamemode it display the same follow# , random script only start to work after been ingame and restarting say gamemode, also how could be done to prevent the script from picking a follow# that was allready used so it doesnt repeat same follow..
here the code i am using i mod a bit to work with 10 follows so far...

Code:
@script
void self = getlocalvar("self");
if( frame == 0)
{
    int r = rand()%99;
    if (r <0)
        {            // If the r is negative,
            r = r*-1;    // Make it positive
        }
    if( r >=0 && r <=9)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW1"));
        }
        else if( r >=10 && r <=19)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW2"));
        }
        else if( r >=20 && r <=29)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW3"));
        }
        else if( r >=30 && r <=39)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW4"));
        }
        else if( r >=40 && r <=49)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW5"));
        }
        else if( r >=50 && r <=59)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW6"));
        }
        else if( r >=60 && r <=69)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW7"));
        }
        else if( r >=70 && r <=79)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW8"));
        }
        else if( r >=80 && r <=89)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW9"));
        }
        else if( r >=90 && r <=99)
        {
        changeentityproperty(self, "animation", openborconstant("ANI_FOLLOW10"));
        }

}
    @end_script
 
first thing every time i start the game and start say gamemode it display the same follow# , random script only start to work after been ingame and restarting say gamemode

I usually declare this script by msmalik in update.c which is run during main menu and title screen to give random seed to randomizer.
C:
if(openborvariant("in_menuscreen") || openborvariant("in_titlescreen")){
// Random starting seed from msMalik681
      int seed; //initialize seed.
      if(seed==NULL()){
        seed=1;
      } //start it off.
      seed++; //increment seed.
      srand(seed); //apply seed to random function.
}

how could be done to prevent the script from picking a follow# that was allready used so it doesnt repeat same follow..

I usually add check(s) before running the script after a random value is attained. If the check says that it is the same as the current one, run the random script again. If check says it's not, then run the script.
The tricky part is the coding the check.
I've coded this kind of check before, let me search for it.
 
I usually declare this script by msmalik in update.c which is run during main menu and title screen to give random seed to randomizer.
C:
if(openborvariant("in_menuscreen") || openborvariant("in_titlescreen")){
// Random starting seed from msMalik681
      int seed; //initialize seed.
      if(seed==NULL()){
        seed=1;
      } //start it off.
      seed++; //increment seed.
      srand(seed); //apply seed to random function.
}



I usually add check(s) before running the script after a random value is attained. If the check says that it is the same as the current one, run the random script again. If check says it's not, then run the script.
The tricky part is the coding the check.
I've coded this kind of check before, let me search for it.
thanks a lot man it works from the start now...
about the non repeat, i use 10 levels so far and want to prevent for example level 2 from repeat the same follow# of say entity that the random script piked in level 1 so every new level would pick random follow# of say entity i spawn in every level but avoid any follow# used by that entity in previus levels...
 
i spawn in every level but avoid any follow# used by that entity in previus levels...

That would require storing spawn code in global variable.
I'll see what I can do :cool:

[couple hours later]

Alright, I've managed to code the script but before sharing it, you need to use a function declared in an animationscript.
This is the function:
C:
void PickAni()
{// Randomly picks animation
    int r = rand()%50+50;
    char Anim;

    if(r < 10){
      Anim = "ANI_FOLLOW1";
    } else if(r < 20){
      Anim = "ANI_FOLLOW2";
    } else if(r < 30){
      Anim = "ANI_FOLLOW3";
    } else if(r < 40){
      Anim = "ANI_FOLLOW4";
    } else if(r < 50){
      Anim = "ANI_FOLLOW5";
    } else if(r < 60){
      Anim = "ANI_FOLLOW6";
    } else if(r < 70){
      Anim = "ANI_FOLLOW7";
    } else if(r < 80){
      Anim = "ANI_FOLLOW8";
    } else if(r < 90){
      Anim = "ANI_FOLLOW9";
    } else {
      Anim = "ANI_FOLLOW10";
    }

    return Anim;
}

That function will be called by this script:
Code:
@script
  if(frame==1){
    void self = getlocalvar("self");
    void Anim1 = getglobalvar("N1");
    void Anim2 = getglobalvar("N2");
    void Anim = PickAni();
    int Code;

    while(Anim==Anim1||Anim==Anim2){
      Anim = PickAni();
    }

    setglobalvar("N1", Anim2);
    setglobalvar("N2", Anim);

    Code = openborconstant(Anim);
    changeentityproperty(self, "animation", Code);
  }
@end_script
...

This script stores last defined animations in global variable N1 and N2. Before doing the actual animation change, the script picks a random animation then do check. If the current pick is the same as either first pick or second pick before, it will repick another random animation. This will be repeated until current pick is different to both first and second picks.

HTH
 
Last edited:
thanks a lot bloodbane, i have 10 follows to pick from and use 5 levels, tryed to mod it with N3 and N4 so in level 3, 4 and 5 doesnt repeat any of the follows allready used in the previus levels but i cant seem to make it work, i just frankenstain it as i dont have any real programing knoledge..
 
Back
Top Bottom