Solved Random palettes (no hidden maps)

Question that is answered or resolved.

dantedevil

Well-known member
I make some test with this script, but I do not understand how to prevent it from using the hidden palettes. It seems, that the script needs an update.

Code:
void randomPal ()
{
    // Change the entity palette to a random palette
    // Douglas Baldan - 10/22/2016
    void self = getlocalvar("self"); //Get calling entity.
    int Pal = rand()%5;// 5 is the palette count. Chance this number to match the number of palettes your entity has
    if (Pal <0){ // if the random value is negative
        Pal*-1;// make it positive   
    }
    changeentityproperty(self, "map", Pal); //
   
}

I think it would be very useful, put together the best of these two script:
Code:
void changepal(int Pal)
{// change the current pallete
    void self = getlocalvar("self"); //Get calling entity.
    changeentityproperty(self, "map", Pal);
}

The idea is to create a random alternate palette script, where you can specify which palette will you use.

Te same like this script, but instead of names, palettes:
Code:
@end_script
    loop    0
    @script
  int i = rand()%28;
  void s = "";
  void self = getlocalvar("self");

  if (i < 0 )
     {i= -i;}

  if(i==0){s = "AKio";}  else
  if(i==1){s = "Daiki";} else
  if(i==2){s = "Eiji";} else
  if(i==3){s = "Fumio";} else
  if(i==4){s = "Gorou";} else
  if(i==5){s = "Haru";} else
  if(i==6){s = "Isamu";} else
  if(i==7){s = "Juro";} else
  if(i==8){s = "Katsuo";} else
  if(i==9){s = "Manabu";} else
  if(i==10){s = "Nobu";} else
  if(i==11){s = "Osamu";} else
  if(i==12){s = "Rokuro";} else
  if(i==13){s = "Satoru";} else
  if(i==14){s = "Takao";} else
  if(i==15){s = "Yasuo";} else
  if(i==16){s = "Arata";} else
  if(i==17){s = "Hotaka";} else
  if(i==18){s = "Izanagi";} else
  if(i==19){s = "Junichi";} else
  if(i==20){s = "Kazuhiko";} else
  if(i==21){s = "Madoka";} else
  if(i==22){s = "Noboru";} else
  if(i==23){s = "Orochi";} else
  if(i==24){s = "Ryota";} else
  if(i==25){s = "Sadao";} else
  if(i==26){s = "Takehiko";} else
  if(i==27){s = "Yoshinori";} else
  if(i==28){s = "Yuudai";}
  changeentityproperty(self, "name", s);
@end_script

He tried to do it on my own, but I did not succeed ...
 
Last edited:
Solution
Well my friends, the sytem of DC works great, thanks my firiend.

And if you don't want use this method, to avoid the onspawnscript, and use the random palettes only in specific animations, here another way courtesy of Bloodbane:

Code:
@script
  int i = rand()%7+7;
  void self = getlocalvar("self");
    int HL = getentityproperty(self,"hmapl");
    int HU = getentityproperty(self,"hmapU");

  if(i>=HL && i<=HU){ // if i = hidden palette, change to 6
    i = 6;
  }

  changeentityproperty(self, "map", i);
@end_script

This script has wider limit i.e all hidden palettes are prohibited. I choose change to 6 assuming it's not used as hidden palette by any enemy.

All tested, so you can choose the best method depending on what you want...
To prevent using hidden palette, you simply need to add check and replace line like this:

Code:
@script
  int i = rand()%7+7;
  void self = getlocalvar("self");

  if(i==1){i = 2;} // if i = hidden palette, change to 2
  changeentityproperty(self, "map", i);
@end_script

This script randomizes palette but will prevent using palette 1
 
Bloodbane said:
To prevent using hidden palette, you simply need to add check and replace line like this:

Code:
@script
  int i = rand()%7+7;
  void self = getlocalvar("self");

  if(i==1){i = 2;} // if i = hidden palette, change to 2
  changeentityproperty(self, "map", i);
@end_script

This script randomizes palette but will prevent using palette 1

Thanks my friend!

The script works ok, but I try to add more palettes to hidde, beccause my enemies has hidden from 1 to 5.

This character has 10 palettes and hmap 1 5, so I try to adjust the script, but not work.
Code:
@script
  int i = rand()%7+7;
  void self = getlocalvar("self");

  if(i==1){i = 6;} // if i = hidden palette, change to 6
  changeentityproperty(self, "map", i);}  else
  if(i==2){i = 6;} // if i = hidden palette, change to 6
  changeentityproperty(self, "map", i);}  else
  if(i==3){i = 6;} // if i = hidden palette, change to 6
  changeentityproperty(self, "map", i);}  else
  if(i==4){i = 6;} // if i = hidden palette, change to 6
  changeentityproperty(self, "map", i);}  else
  if(i==5){i = 6;} // if i = hidden palette, change to 6
  changeentityproperty(self, "map", i);
@end_script
 
The hmap attributes are available to script.

I'm on a phone, but what you need is get the hmap upper and lower, then encapsulate your random selector in a do-while loop.

The loop will cycle through random maps until it finds one that isn't in the hmap range.

Also note the number of maps is available too. There's no reason to hard code the number like you're doing.

DC
 
Damon Caskey said:
The hmap attributes are available to script.

I'm on a phone, but what you need is get the hmap upper and lower, then encapsulate your random selector in a do-while loop.

The loop will cycle through random maps until it finds one that isn't in the hmap range.

Also note the number of maps is available too. There's no reason to hard code the number like you're doing.

DC

Thanks DC!
I got the idea, but I don't know how can add that in the script.
 
Bloodbane said:
To prevent using hidden palette, you simply need to add check and replace line like this:

Code:
@script
  int i = rand()%7+7;
  void self = getlocalvar("self");

  if(i==1){i = 2;} // if i = hidden palette, change to 2
  changeentityproperty(self, "map", i);
@end_script

This script randomizes palette but will prevent using palette 1

I try replacing this line:
Code:
if(i==1){i = 2;} // if i =

For this:
Code:
if(i==1, 2, 3, 4, 5){i = 6, 7, 8, 9, 10;} // if i =
Now the script will prevent use palette 1, 2, 3, 4 and 5. But the problem is always use palette 6 and never use 7, 8, 9 or 10.
What is wrong?
 
I got you covered. Here is my complete palette control system, and I'll walk you through step by step. Tried, tested, and proven. Just now packaged up into github, but I've used the code for years.

1. Throw away all that in text script. Putting scripts directly into model text should always be a last resort.

2. Download my D20 and Kanga libraries. Follow the Readme instructions. If you did it right, you'll have dc_d20 and dc_kanga folders in your data/scripts folder.

3. Create an onspawn.c file for your model. In the model text, add the following: 
Code:
onspawnscript <path to the onspawn script>

4. Open the onspawn.c file you created, and add the following:

Code:
#include "data/scripts/dc_kanga/main.c"

void main()
{

    void self;  // Calling entity.
    
    // Get calling entity.
    self = getlocalvar("self");

    // Set calling entity as target.
    setlocalvar(DC_KANGA_KEY_TARGET, self);

    // Apply a random choice from the entity's 
    // available map indexes.
    dc_kanga_random_index();
}

Fire up your module and give it a try. The model that you set up in the above steps will now select one of its non hidden maps when spawned into play. You don't need to worry about lists or any of that. Now you can reference that same file with every model you want to have a random map.

DC
 
Thanks a lot my friend!

Your system look great, I'll try it when I get home.

I hope you can update the links of this:
http://www.caskeys.com/dc/tutorial-random-names/

Random names are easy money. Have a look, and let me know if you have any questions.

DC

That looks great too.

Thanks for all your help!
 
Well my friends, the sytem of DC works great, thanks my firiend.

And if you don't want use this method, to avoid the onspawnscript, and use the random palettes only in specific animations, here another way courtesy of Bloodbane:

Code:
@script
  int i = rand()%7+7;
  void self = getlocalvar("self");
    int HL = getentityproperty(self,"hmapl");
    int HU = getentityproperty(self,"hmapU");

  if(i>=HL && i<=HU){ // if i = hidden palette, change to 6
    i = 6;
  }

  changeentityproperty(self, "map", i);
@end_script

This script has wider limit i.e all hidden palettes are prohibited. I choose change to 6 assuming it's not used as hidden palette by any enemy.

All tested, so you can choose the best method depending on what you want.

Thanks to both friends!
 
Solution
You can (and should for consistency) use my system in animations too. I just walked you through using the onspawn event because that's the most optimal way to do what you asked.

Add this to the top of your animation script (create a blank one if you don't already have it).
Code:
#include "data/scripts/dc_kanga/main.c"

Now you can call any of the function methods in my system on an animation frame, like this:
Code:
frame data/chars/john/0_1.png
@cmd setentityvar DC_KANGA_KEY_TARGET getlocalvar("self")
@cmd dc_kanga_random_index
frame data/chars/john/0_2.png #Random palette function will execute on this frame.
...

That goes for Bloodbane's method too. If you want to use it, then you should package his code into a function and execute it with @cmd. When scripting in general, you should always try to prioritize where you put scripts in the following order:

[list type=decimal]
[*]Event scripts - This is the best and most optimal way by far to execute a script, and allows very easy reuse of any given function.
[*]Predefined functions called on an animation frame - Define function methods, import them into an animation script, and then execute them with @cmd. This is the preferred method for executing actions on an animation frame when there isn't an event that can do it for you.
[*]Inline script (@script & @end_script tags) - This is the least optimal way to add scripts. You should only ever do it for extremely simple (one or two line at most) custom logic, never for anything else. It is very sub optimal compared to running defined functions and does not allow reuse of code.
[/list]
 
Good to know my friend, thanks!

My next demo have lot of adds, It looks like it will have a really new and improved ok.

Thanks for all your help my friend, I appreciate the help of all, it is what makes this forum a great community.
 
Back
Top Bottom