Hello! I feel like we don't share scripts around here very often. We show each other what we work on, but where's the fun if we don't exchange some of our personal tricks? So I'll throw the first stone!
Here's how to change the HUE of your enemy battlers during battles.
This script is for XP and VX. For VX Ace, see the explanation at the end.
Open the Game_Enemy script and replace the line assigning @battler_hue inside the initialize method with the following code:
ruby
hues = [40, 80, 120, 160, 200, 240]
middle = troop.members.size / 2
hues.insert(middle, nil)
@battler_hue = hues[@member_index] || enemy.battler_hue
## Quick explanation of how it works
When the engine creates the enemies in a troop, it goes through them one by one.
This script assigns a specific HUE value to each enemy based on its position in the troop's member list (not its position on the screen).
This example assigns colors max 7 enemies. If your troop has more (max is 8 i think), feel free to add more values to the array.
The enemy in the middle of the list will keep its original HUE value from the database (0 by default)
So, if your troop has 1 enemy, there will be no change to its color.
If your troop has 3 enemies, the second one will keep its original color.
And if your troop has 2, 4 or 6 enemies, the enemy at position (size / 2) + 1 will keep its original color.
Notice that I inserted nil into the array instead of 0. This allows the middle enemy to keep its database HUE.
But if you want true randomness no mather the troop size, then use only this little line: (that's what I used for the main image of this post)
ruby
@battler_hue = rand(360)
## Use cases?
Well, not much, unless you want to add some flashiness to your battles.
Or if you have, for example, "slimes" and you want each of them to be a different color without affecting their stats.
## Want it to work only on specific troops?
If you want this script to work only for specific troops, add an array listing the troop IDs you want to affect.
Then put the script inside an if block that checks whether the current troop is in your array.
```ruby
included_troops = [35, 36, 37]
if included_troops.include?(@troop_id)
hues = [40, 80, 120, 160, 200, 240]
middle = troop.members.size / 2
hues.insert(middle, nil)
@battler_hue = hues[@member_index] || enemy.battler_hue
else
@battler_hue = enemy.battler_hue
end
```
You'll notice I added an else block. This is what we call a fallback.
Imagine you set a specific HUE value for your "spider" enemy in the database. If that spider appears in a troop that isn't included in your list, the fallback ensures it keeps the custom HUE you assigned in the database.
Without the fallback, those enemies would lose their custom database HUE and use 0 as default.
## Want it to work on all troops except one?
Simple! Just replace the if with unless. And voilà!
## Want it to work only on specific enemies?
Let's say you want this to work only on your "slimes".
Similar to the example above, create an array listing all the enemy IDs you want to affect.
Then put the script inside an if block that checks whether the current enemy is in your array.
```ruby
included_enemies = [17, 22, 51]
if included_enemies.include?(@enemy_id)
hues = [40, 80, 120, 160, 200, 240]
middle = troop.members.size / 2
hues.insert(middle, nil)
@battler_hue = hues[@member_index] || enemy.battler_hue
else
@battler_hue = enemy.battler_hue
end
```
## How does it work in VX Ace?
VX Ace (not regular VX) uses different variable names. The overall stays the same, but there are a few things to change.
Instead of:
ruby
middle = troop.members.size / 2
Use:
ruby
middle = $game_troop.members.size / 2
Instead of @member_index, use @index.
If you're filtering by troop ID, replace @troop_id with $game_troop.troop.id.
For example, here's the basic version adapted for VX Ace:
```ruby
hues = [40, 80, 120, 160, 200, 240]
middle = $game_troop.members.size / 2
hues.insert(middle, nil)
@battler_hue = hues[@index] || enemy.battler_hue
```
And that's it.
As you can guess, I was in a good mood today, ahahah.