[VX Ace] VE Materia - AP Gain Script Command

Howdy! 👋

I'm wondering if anyone could help me out with what I hopefully believe is a simple script edit to Victor's Materia script : https://victorenginescripts.wordpress.com/rpg-maker-vx-ace/materia-system/

By default, the materia gains AP upon defeating foes. I'm hoping to change it so I can manually set the AP of Materia via a script call. For reference, In game I have different groups of materia (Fire/Water/Ice/etc.). When you first get a fire materia, it will have 1 total AP, once you get a second fire materia, I'll then set ALL fire materia to have 2 total AP. I'll handle keeping track of owned materia, and the AP gain via an event, all I need it a way to set all materia with an armor ID of X to a certain amount of AP.

I've made a couple small tweaks that, while I don't think any would affect the change to AP gain, I figured I should include just in case. (NOTE: I attached it at the bottom of this post, as the script exceeded the max post size)

Notable at line 574 you'll see:
@ap ||= $game_variables[198]___
With this, I've been able to make it so when I obtain a materia, it's initial total AP is set to the value of variable 198, but I cannot then increase the AP of those Materia after first obtaining them. I thought I'd point it out, in case it proves helpful.

Hopefully this makes sense. Let me know if you have any questions.

Thank you!
Hey there,
I may have overlooked something while skimming through the script (only the original one though since I wasn't logged in).

The main way of getting AP seems to be the "ap=" method from the Game_Actor class, which just forwards the amount to the "gain_ap" methods from all equipped materias (RPG::Armor).

A method to process all materias that are either equipped by an actor or are held in the parties inventory could probably look like this:
Code:
class Game_Interpreter  # <- Put into Interpreter class for use in script commands
 
  def iterate_all_materias(&block)
    return enum_for(:iterate_all_materias)  unless block_given?  # Makes the { ... } block when calling this method optional, not necessarily needed to make the code work.
    $game_party.materias.each(&block)  # <- Materias in Party Inventory
    $data_actors.each_index { |index|  # Iterate all database actor slots...
      actor = $game_actors[index]
      next  unless actor
      actor.all_materias.each(&block)  # <- Materias held by "index"-th actor
    }
  end
end

which would allow you to do script commands like this:
Code:
iterate_all_materias { |materia|
  if materia.id == 15  # If Materia derived from Armor #15...
    # ...Increase AP by Variable 176 (minus the AP the materia already has):
    materia.gain_ap($game_variables[176] - materia.ap, 1)
  end
}

That said, it seems that instead of
materia.gain_ap( ... )
you could also use
materia.ap = $game_variables[176]
here to set the value directly (if you are not using Materia Breeding).

As an alternative to using the armor id to determine the element, you could also make use of the armors notebox.
For example, you could give every fire materia the notetag
<fire-materia>
and search the notebox for it instead of checking multiple ids:
(the angle brackets are not required of course, but they have become popular for note-tags)
Code:
iterate_all_materias { |materia|
  if materia.note.include?("<fire-materia>")  # <- Check if notebox includes that substring
    materia.ap = $game_variables[176]  # <- Set Materia AP
  end
}
@Another Fen
Thank you very much for your help! Everything seems to be working great. In particular, I've used the second script call.
@Another Fen
Thank you very much for your help! Everything seems to be working great. In particular, I've used the second script call.
This may not do with your question, but when I go and use the materia in the demo and beat an enemy the AP doesn't show that it was added to the materia. How do i know if the AP was added?
This may not do with your question, but when I go and use the materia in the demo and beat an enemy the AP doesn't show that it was added to the materia. How do i know if the AP was added?
You should make your own thread for a new question.
If you have a materia equipped to an item/weapon/armour then just go into the materia screen and look at the number for the materia. Confirm that it has increased or not.
This may not do with your question, but when I go and use the materia in the demo and beat an enemy the AP doesn't show that it was added to the materia. How do i know if the AP was added?
@slickdeath97 Does this happen whenever you're using the materia system, or only when you have used @Another Fen's suggested fix? If it's whenever you're using the materia system, you should create a new thread rather than necro someone else's (where most people will look at the original post) - much greater chance your own question will be answered that way. If it's directly related to Fen's fix, then this was an OK place to ask.
Was wondering what the fix deals with and what is different?

Original thread: https://forums.rpgmakerweb.com/threads/vx-ace-ve-materia-ap-gain-script-command.113953/