Change Equipment Based on Variable Instead of Specified Actor?

Hi all! As you know, going from Script Commands to Actor > Change Equipment forces you to select a specific actor, like so:
1716146143470.png


However, due to a mechanic in my game I need the ability to use a variable to determine the actor instead. I checked the Script Call Collection for VXAce stickied at the top of this subforum and found this for changing equipment:

$game_actors[id].change_equip_by_id(slot, equip) if $game_actors[id]
# id is actor id
# slot is the slot number (0=weapon, 1=shield, 2=head, 3=body, 4=accessory)
# equip is the weapon or armor id

I was wondering if there would be a way to replace the Actor ID to reference a variable, let's say 70. So if say Variable 70 is set to 1, it would change Actor 1's armor to that, or if it was set to 4, it would set Actor 4's armor to that.

On that note, for some reason no matter what I do even just the script call that uses a specified actor doesn't actually equip anything... and seems to remove the equipment from the inventory as well. Any idea why that may be?
I was wondering if there would be a way to replace the Actor ID to reference a variable
If you look up the script call for a game variable value, you should be able to simply paste that code in wherever this says id

On that note, for some reason no matter what I do even just the script call that uses a specified actor doesn't actually equip anything... and seems to remove the equipment from the inventory as well. Any idea why that may be?
No one can say what you might have wrong in your code without seeing what you actually have typed in your project.
No one can say what you might have wrong in your code without seeing what you actually have typed in your project.

Thanks for the reply!
For example, if I do this, trying to change Actor 1's Shield Armor slot to Armor 400:

$game_actors[1].change_equip_by_id(1, 400) if $game_actors[1]

Doesn't change the equipment for Actor 1, and for some reason removes the item from the inventory too. No script crash but it also doesn't work either. The armor can be applied from the equipment menu, so it's not the actor being unable to equip the item altogether.
$game_actors[1].change_equip_by_id(1, 400)
This is all you need to make it work. But it needs one thing. The item has to be in your inventory for it to change.
To make this for a variable for the actor id
$game_actors[$game_variables[10]].change_equip_by_id(1, 400) if $game_variables[10] == 1
Slot 1 is shields.
Is the slot number correct? Armor ID 400 is a Shield?

Also, I notice you posted the exact same question in another thread. That's basically never useful - the most you can accomplish is getting different people wasting their time trying to help you simultaneously.


if $game_variables[10] == 1
Isn't that making the script call only work if variable 10 is set to 1? I should think the OP wouldn't want that conditional there, right?
What Ron had suggested should be more than enough to equip that shield.
Here's a wrapper method to help you reuse that code as many times as possible.

Ruby:
class Game_Interpreter
  def actor_equip_by_id(var_id, target_id, slot_id, equip_id)
    var = $game_variables[var_id]
    $game_actors[var].change_equip_by_id(slot_id, equip_id) if var == target_id
  end
end

You copy and paste it in a new script section.

This is how you'd call it:

Ruby:
actor_equip_by_id(10, 1, 1, 400)

And make sure it's a shield (the first option available on any item page).
Is the slot number correct? Armor ID 400 is a Shield?

Also, I notice you posted the exact same question in another thread. That's basically never useful - the most you can accomplish is getting different people wasting their time trying to help you simultaneously.
Yup, Armor 400 is set as a shield in the Database. Thanks for the advice, too. I'll keep that in mind in the future. While my account has been around for a bit I'm still getting used to posting etiquette.

What Ron had suggested should be more than enough to equip that shield.
Here's a wrapper method to help you reuse that code as many times as possible.

Ruby:
class Game_Interpreter
  def actor_equip_by_id(var_id, target_id, slot_id, equp_id)
    var = $game_variables[var_id]
    $game_actors[var].change_equip_by_id(slot_id, equip_id) if var == target_id
  end
end

You copy and paste it in a new script section.

This is how you'd call it:

Ruby:
actor_equip_by_id(10, 1, 1, 400)

And make sure it's a shield (the first option available on any item page).

Thanks for making this! There is a typo in the second line that made it crash, but after fixing that it no longer crashed the game. Unfortunately, it seems like it is still doing the same thing where the armor doesn't equip and then vanishes from the inventory which is sad because this would really streamline a lot if I could use it.

It seems I have found the culprit for why the equip keeps getting erased rather than being equipped even though everyone's suggestions should work. It is Yanfly Engine Ace - Ace Equip Engine v1.06. I removed the script and all of these suggestions work without it. This is odd because the script says that the Change Equipment call should still work and function fine, and that the equipment being changed will simply go in the first available slot. This is true for the Change Equipment shortcut I have a screenshot of in the first post, but for some reason Yanfly's script breaks the script call version. This is a shot in the dark and might be a big ask, but would anyone know about creating compatability?
If you're getting desperate enough as to skip some internal checks and force the change, you could add this piece of code instead. It relies on the force_change_equip method instead.

Ruby:
class Game_Actor
  def force_change_equip_by_id(slot_id, item_id)
    if equip_slots[slot_id] == 0
      force_change_equip(slot_id, $data_weapons[item_id])
    else
      force_change_equip(slot_id, $data_armors[item_id])
    end
  end
end

class Game_Interpreter
  def force_actor_equip_by_id(var_id, target_id, slot_id, equip_id)
    var = $game_variables[var_id]
    $game_actors[var].force_change_equip_by_id(slot_id, equip_id) if var == target_id
  end
end
Is the variable set to the correct number, I put Yanfly ace equip engine script into my test project and it works fine.
To make this for a variable for the actor id
$game_actors[$game_variables[10]].change_equip_by_id(1, 400) if $game_variables[10] == 1
Slot 1 is shields.
Is the variable set to the correct number, I put Yanfly ace equip engine script into my test project and it works fine.
Yep, when I remove Ace Equip Engine everything works totally fine as intended. But when I re-add it, it no longer does. It's really strange.

If you're getting desperate enough as to skip some internal checks and force the change, you could add this piece of code instead. It relies on the force_change_equip method instead.

Ruby:
class Game_Actor
  def force_change_equip_by_id(slot_id, item_id)
    if equip_slots[slot_id] == 0
      force_change_equip(slot_id, $data_weapons[item_id])
    else
      force_change_equip(slot_id, $data_armors[item_id])
    end
  end
end

class Game_Interpreter
  def force_actor_equip_by_id(var_id, target_id, slot_id, equip_id)
    var = $game_variables[var_id]
    $game_actors[var].force_change_equip_by_id(slot_id, equip_id) if var == target_id
  end
end

Thank you! I'll try this. To call this, I would put
"force_actor_equip_by_id(var_id, target_id, slot_id, equip_id)" ?

My only question is what would I put for target_id? I am unfamiliar with that.
The actor's ID, of course.
The force change method works, but only when Yanfly's script is removed, just like the other attempts. This is really strange, and I'm not sure why. At this point, I think I'll have to endure the conditional branches.
The force change method works, but only when Yanfly's script is removed, just like the other attempts. This is really strange, and I'm not sure why. At this point, I think I'll have to endure the conditional branches.
That just makes me think you have a pirated version of rpg maker.
It is a common issue with pirated versions that the commands don't work.

Original thread: https://forums.rpgmakerweb.com/threads/change-equipment-based-on-variable-instead-of-specified-actor.168846/