Add state based on the current location of a card

Howdy my beloved forumers! For those who already know me, I'm making a game using a card game script, I have modified the original script severely. I have included more locations besides the originals presented on the script (graveyard, hand and deck/stack). I have added locations like Banish, Field, Extra Deck, etc. To make it more loyal to the original yugioh card game. Now, I'm struggling a little trying to create a method to constantly update the battle to watch if a card is in a currently location, and based on that, add the correspondent state. So for example:
Let's say I use the card Dark Magician. Dark Magician is now in the location "Field". While the Field location has a monster card on it, it should add that actor a state that correspond to Dark Magician.
To make it simple, I decided to make the monsters on the same skill id that the corresponding state.
So the Skill ID 58 if it is Dark Magician, there would be a state ID 58 which is also Dark Magician.
So far, so simple. Isn't?

Now, I'm pretty sure I could add a def method on the card game script to check constantly during the battle the location of the cards, and add an state to that actor based on the location of said card/skill. But I can't figure out 2 things:

1) In which class should I add this method?
2) How should I code this method?

I know that most method should be called some way, but I'm also sure that there should be some kind of method that check constantly the flow of the battle without being call every action. But I'm not sure how.

This is what I came with so far:

First, I choose the class Scene_Battle < Scene_Base to create this method.
Now the method was this one:

Ruby:
def def state_monster_field
    @field.each do |monster|
    monster = skill.id
    @actor.add_state(monster)
    update
    end
  end

But so far, this method is not doing anything. Nor good, nor bad. Is not crashing the game, is mostly not doing anything. I'm pretty sure that's because there's nothing calling this method constantly. That's why I tried with the "update" method. But it doesn't do anything. Also tried with a "while". Anyways, I'm sure I'm not doing the things right. That's why I'm recurring to you guys. Thanks in advance and sorry if I wasn't very clear with my explanations!
1) In which class should I add this method?
2) How should I code this method?
From what I can see for the base script the only place that makes sense to put it is in Scene_Battle
Where you call the method will depend on when you need to check it.
You said you can do a check for what cards are in 'field' so maybe there, but that would probably keep adding the state over and over, so it needs to go somewhere you are applying the card once.

But so far, this method is not doing anything. Nor good, nor bad. Is not crashing the game, is mostly not doing anything. I'm pretty sure that's because there's nothing calling this method constantly. That's why I tried with the "update" method. But it doesn't do anything. Also tried with a "while". Anyways, I'm sure I'm not doing the things right. That's why I'm recurring to you guys. Thanks in advance and sorry if I wasn't very clear with my explanations!
If what you posted is exactly how you have it in your game, then I'm surprised it's not giving you errors.
It should be
Ruby:
def state_monster_field
  @field.each do |monster|
    monster = skill.id
    @actor.add_state(monster)
    update
  end
end

But then again what is this method doing?
checking all values in @field. cool
your calling each value monster
Then you're overwriting the monster with skill.id. Where is the skill.id coming from?
should it not be the other way around? But then what does the value for monster look like?
You should add in a p(monster) to find the monster value so that you know what you're dealing with.
next you add a state. but the monster value needs to be a number. and it looks like you are setting the value monster to nil. so then it adds nil state, which really does nothing.

If you want someone to help with this we will most certainly need a working demo to test with.
Guessing what changes you made and knowing how to get the correct results is not possible.
@Roninator2 I knew I was missing some explanation!
Welp, here's the thing. The card game script has some kind of tags for cards that you can add, the original script came with Rarity, Type and Element_color.
In this case, I'm using the "Type" one, which has Spell, Trap, and Monsters in this case. I forgot to code it to only recognize if the cards on the location "Field" are of that type. Anyways, I'm planing to do it for Spell and Traps in the future too but with a different mechanic, so that would be the problem. There's also the Skill Type in the database for monsters. So you have 2 ways to recognize a skill by the type "monster".

But then again what is this method doing?
checking all values in @field. cool
your calling each value monster

Oh, so I was doing that wrong I see. Should I do something like @field each do |skill.id| then?
Which I really need is something to check the field, then if there's cards/skills, check their ID. If there's a card with the type monster, add the actor of that skill the same State ID than that monster ID (this is to make it simpler). So if actor has a monster with ID 58, the state 58 will be added.
So you have 2 ways to recognize a skill by the type "monster".
So that's a variable or value added to the cards and your looking for that value?

Oh, so I was doing that wrong I see. Should I do something like @field each do |skill.id| then?
Which I really need is something to check the field, then if there's cards/skills, check their ID. If there's a card with the type monster, add the actor of that skill the same State ID than that monster ID (this is to make it simpler). So if actor has a monster with ID 58, the state 58 will be added.
So basic programming (at least here in ruby) ...
@field.each
This is taking the variable @field and iterating through each value.
But what is each value, because this format assumes the @field is an array.
[value1, value2, .. etc]
If it is not an array then we need to know what data type it is in order to know how to process the information in it.
Next is |monster|
this is you taking the next value from the array and, for the sake of this small .each loop, calling it monster. (value1 is now referred to as monster while we process code)
So here we need to know what data is it that we have. RPG::Weapon, RPG::BGM, $Game_Actor, etc.
Normally this is not too problematic as long as we understand what data we have.
so this object can have multiple variables attached to it.
(since the card is likely a $data_item it will have whatever variables that have been created for it)
Then we can use this object to check for specific values.
if monster.card_type == "monster"
do stuff
end

So without knowing how this is all put together it becomes very hard to know how to help you.
This is why I usually ask people for a demo so that I can work with the data directly.

Short of this, I would suggest to get someone to be your programmer.
Because I understand you don't want to share your project for anyone to just copy and reproduce what you have done.
Rest assure if you give me a copy I won't use it for anything. I personally don't like card games and set ignore on every one in steam.
Short of this, I would suggest to get someone to be your programmer.
Because I understand you don't want to share your project for anyone to just copy and reproduce what you have done.
Rest assure if you give me a copy I won't use it for anything. I personally don't like card games and set ignore on every one in steam.
I would love to have a programmer for this. Real problem is that I don't have any money :c
And I don't really mind sharing a demo, the problem is that it will gonna be pretty heavy. I think I actually have shared one some time ago. And if I would trust my project in this forum, you will be one of them. You helped me a lot of times.

Anyways, here's the last update of it:

You will see the card game script very easily, is written in capitals.
Hey,

I'm also going to make some assumptions about the changes here, so if I'm wrong take the related paragraphs with a grain of salt. I'm also not sure if you are mainly looking for explanations or a solution here or which parts of Ruby you are familiar with, so I appologize for any repetitions.

I would assume that field, banish and extra deck are handled exactly like deck, hand or grave (they hold arrays and are used in Game_Actor) and that the code posted was the only relevant code added to Scene_Battle?

1) In which class should I add this method?
Generally it is often a good practise to add your methods to the classes whose instance variables you use. So if every actor has its own field, then Game_Actor would often be a good place to put related logic.
This is more of a design question here rather than a necessity. You can do it all from Scene_Battle if you only need that process during battles. However...

2) How should I code this method?
Roninator2 has already given a good rundown, but piling on some details:

Instance Variables:
Spoiler
Variables whose name start with an @ are interpreted as instance variables. They work similar to self-switches in that every object in your game has an independent set of them. So for example, when Pencilcase72 uses @grave = [] in the setup of Game_Actor, it means that now whenever one of your actors is set up, they will receive their own empty grave. This is also how each actor has their own HP value and can be afflicted independently by states.

Since those variables are bound to objects, you cannot use your @field variable outside of Game_Actor (if you use it in Scene_Battle, the game will just interpret it as the instance variable of your battle scene, which likely is not being used).
There are ways to make instance variables accessible from outside, but you would need to specify which actors field you are referring to at the very least (there might be multiple actors in your game).
$game_actors[1].deck
for example would give you the deck of actor#1.

Scene_Battle normally also does not use an @actor variable. That variable is only used by some of the windows to specify whose skills they should be showing during action selection, but not by the battle scene itself.


I'm pretty sure that's because there's nothing calling this method constantly. That's why I tried with the "update" method. But it doesn't do anything.
It looks to me like you got it the wrong way round. If you want to run your code with every update of your battle scene for example, you have to modify Scene_Battle's update method to include your code, not call it.

How to add code to an existing method:
Spoiler
There are two ways to change an existing method:

The first way is to copy the original method and the surrounding class into your script and modify it there:
Code:
class Scene_Battle
 
  def update
    # --- This is the original code ---
    super
    if BattleManager.in_turn?
      process_event
      process_action
    end
    BattleManager.judge_win_loss
    # --- This code was added ---
    state_monster_field
  end

end
Because scripts are executed in order from top to bottom, your definition will replace the previous one from the original Scene_Battle script.

This way is easy, but has one major downside: Because there can only be one version of each method, if you have multiple scripts modifying the same method then one would replace the other and you would likely have to manually merge the code.
To avoid that, there is a second way: "aliasing" the method:
Code:
class Scene_Battle
  
  alias_method(:update_alias_beforeFieldEffects, :update)
 
  def update
    update_alias_beforeFieldEffects
    state_monster_field
  end

end
alias_method creates an exact copy of the current version of the method under a new name. This way, you can replace the method and call your snapshot copy in it to essentially paste the original code back into the method (in addition to the stuff you added before or after).

This way only allows you to add code before or after, but as long as you use unique alias names you can alias a method any number of times without losing code.
Although I am not sure if the update method is the right place to put your code. Scene_Battle is unique in that regard that the "update" method is not actually called every frame since its other methods cause waits as well. You could use the "update_basic" method instead if you really want to run your code every frame. This might not be necessary though.

As to where to put the method, a few questions:

- Do you only need to apply the state after a monster enters the field or do you also want to maintain it? (like reappling it when the actor loses temporary resistance to the state or gets revived while the monster is still on the field, or removing the state from the actor after the monster leaves the field)

- Since you introduced your field variable yourself, you probably know best where you used it to change an actors field. So in theory, you could call your "state_monster_field" method after every change to the field variable if that is suitable.
This would have the advantage that there are no delays between a monster entering the field and the actor getting the state (for example, if a monster makes the actor invincible, you would not have to worry about a possibility of another effect killing the actor before the next update cycle) and possibly have performance benefits (you would not have to update every actor 60 times per second).

- You mentioned in your opening post you would otherwise have to call the update between every action. If the above is not suitable, would it be enough to update the field after each action (which is normally significantly less often than 60 times per second) or can the field change outside of actions too?


I am not sure if you were looking for a straight solution or wanted to try yourself, or if I understood everything about your changes correctly, so I'd just leave my attempt in spoilers:
Spoiler
Code:
# This updates field states in between actions and after battle only
class Game_Actor
  
  def update_field_effects
    if @field != @last_field
      removed_skills = (@last_field || []) - @field
      removed_skills.each { |skill| remove_state(skill.id) }
      @field.each { |skill| add_state(skill.id) }
      @last_field = @field.clone
    end
  end
  
  alias_method(:refresh_ILC_beforeFieldEffects, :refresh)
  def refresh
    refresh_ILC_beforeFieldEffects
    @field.each { |skill| add_state(skill.id) }
  end
  
  alias_method(:on_battle_end_ILC_beforeFieldEffects, :on_battle_end)
  def on_battle_end
    on_battle_end_ILC_beforeFieldEffects
    update_field_effects
  end
end

class Scene_Battle
  
  alias_method(:process_action_end_ILC_afterFieldEffects, :process_action_end)
  def process_action_end
    $game_party.members.each { |member| member.update_field_effects }
    process_action_end_ILC_afterFieldEffects
  end
end

I may have to go over the text again later, feel free to ask questions.
Hope this helps somehow.
I would love to have a programmer for this. Real problem is that I don't have any money :c
And I don't really mind sharing a demo, the problem is that it will gonna be pretty heavy. I think I actually have shared one some time ago. And if I would trust my project in this forum, you will be one of them. You helped me a lot of times.
Ah yes, I have an older copy of your project.
I help so many people I can't keep track of who has what project.
I'll take a look sometime.

Roninator2 has already given a good rundown
thanks. I try but I'm not a professional programmer yet.


Anyways, here's the last update of it:
Maybe a bit of advise as well. Don't have your game rar file inside your game folder to download.
You just doubled it in size.
- Do you only need to apply the state after a monster enters the field or do you also want to maintain it? (like reappling it when the actor loses temporary resistance to the state or gets revived while the monster is still on the field, or removing the state from the actor after the monster leaves the field)
Wow, I can't believe you took all that time to explain some of the basics of coding to some dumb-dumb like me, and actually I understood quite most of I expected! Now, answering this:
The states for monsters (and in a future this will also be applied to spell and traps) will be ALWAYS applied as long as the acording card is on the location "field". If there's more than 1 copy of that monster, the state will be applied twice, or up to trice if there's a third copy. (For that I have a stack scripts that allow me to have multiple copies of a same state, so that's covered). Now, if something move out a monster from the field (get back to the deck, hand or banish), the state will be removed. It doesn't matter from where or how that monster card reach the field, it will automatically apply the monster state on that actor. The function of the monster states is to give the actor the stats of that monster to them, its nothing like a classic state (like poison, confused, etc). They are more like a stats holders.
- Since you introduced your field variable yourself, you probably know best where you used it to change an actors field. So in theory, you could call your "state_monster_field" method after every change to the field variable if that is suitable.
This would have the advantage that there are no delays between a monster entering the field and the actor getting the state (for example, if a monster makes the actor invincible, you would not have to worry about a possibility of another effect killing the actor before the next update cycle) and possibly have performance benefits (you would not have to update every actor 60 times per second).
To be honest, as I said, I'm not a coder myself. I just copy-pasted how the "grave" location worked on the original script and modified it to work for Field, Banish and Extra. It can be checked here:

- You mentioned in your opening post you would otherwise have to call the update between every action. If the above is not suitable, would it be enough to update the field after each action (which is normally significantly less often than 60 times per second) or can the field change outside of actions too?

I'm very dumb to explain myself some times. I need the state to be checking constantly if a monster is on the field to add the state. I don't know if the base code of rpg maker is updating this every action or what. The field does can change outside of actions cuz I have several lunatic states that apply on the beginning and end of turns that can make monsters appear or dissapear from the field. I don't know if this works like I think, but I'm pretty sure Victor Passive States works something like this? I think it adds a state constantly as long as some stat fills a condition, and it doesn't check every action but every time? (I might be wrong).


# This updates field states in between actions and after battle only
class Game_Actor

def update_field_effects
if @field != @last_field
removed_skills = (@last_field || []) - @field
removed_skills.each { |skill| remove_state(skill.id) }
@field.each { |skill| add_state(skill.id) }
@last_field = @field.clone
end
end

alias_method🙂refresh_ILC_beforeFieldEffects, :refresh)
def refresh
refresh_ILC_beforeFieldEffects
@field.each { |skill| add_state(skill.id) }
end

alias_method🙂on_battle_end_ILC_beforeFieldEffects, 😮n_battle_end)
def on_battle_end
on_battle_end_ILC_beforeFieldEffects
update_field_effects
end
end

class Scene_Battle

alias_method🙂process_action_end_ILC_afterFieldEffects, 😛rocess_action_end)
def process_action_end
$game_party.members.each { |member| member.update_field_effects }
process_action_end_ILC_afterFieldEffects
end
end

Oh wow! Thanks very much! I would really know how to improve my little knowledge in ruby to not be asking for help everytime here, but some times I get stuck in things like this. So I really appreciate this enormous help. Thank you very much! I'm gonna try it out and tell you how it went!

EDIT:
So, sorry, I tried it and... maybe I'm doing something wrong?
I took the script you made it for me and posted it as a new script. But it didn't worked, then, I tried to add the parts you gave me in the corresponding class of the card game script. But in both interation it returns me the same error:
1690383321372.png

1690383343126.png


EDIT 2: So I removed the whole "def refresh" and now the game doesn't return any error and the game actually gave me the correct monster state! 😀
The problem is: each action I do, the script add another copy of the same state even if there's only 1 copy of the monster card on the field. So let's say I play the monster card Kuriboh: It add the Kuriboh state on it correctly. Now, if I play any other card on the field, it will add the corresponding state of that card BUT ALSO will add another copy of the Kuriboh state on the actor even if there's only 1 Kuriboh card on my field. And I'm pretty sure this would keep scaling to infinite kuribohs every card I play on the field?
Now, I'm struggling a little trying to create a method to constantly update the battle to watch if a card is in a currently location, and based on that, add the correspondent state.
Let's say I use the card Dark Magician. Dark Magician is now in the location "Field". While the Field location has a monster card on it, it should add that actor a state that correspond to Dark Magician.
So we first need to talk about how the card is getting added to the field.
Is every card played that is tagged field getting added to the field?

I'm looking at the code and yes you did have def def for the method. Surprised this did not give an error.
There are other parts that are also not completed as well.
card_to_banish? is not used anywhere. card_to_field? is currently not used anywhere.
card_to_stack? is used in on_skill_ok

So where in the game play are you thinking this card is added to the field?
Is it from the on_skill_ok, so then a card is added to the stack and the field?

Once that is accomplished then we can ask how are we checking the field for cards.
We can possibly do it at the end of the turn.

One though about this is if a dark magician is in the field and we add the state, then the next time we check the field again the dark magician card is still there, so the state is added again?
Should the cards not be removed?
It would sort make sense to add cards to the field then remove them when the state is applied.

But then I question if the battle needs a @field or just a card.field = true variable.
My mind is not following well because it's resisting learning this card game a bit, and I seem to try looking at it when I'm tired.

Of course at this moment we are not calling the state_monster_field method, so there should be no problems playing.

EDIT 2: So I removed the whole "def refresh" and now the game doesn't return any error and the game actually gave me the correct monster state! 😀
The problem is: each action I do, the script add another copy of the same state even if there's only 1 copy of the monster card on the field. So let's say I play the monster card Kuriboh: It add the Kuriboh state on it correctly. Now, if I play any other card on the field, it will add the corresponding state of that card BUT ALSO will add another copy of the Kuriboh state on the actor even if there's only 1 Kuriboh card on my field. And I'm pretty sure this would keep scaling to infinite kuribohs every card I play on the field?
Just saw this part. Glad it's working.
Your issue is what I was just saying, shouldn't the card be removed from field if the state is applied?
If you need to always have the card in field and only taken out for a specific reason, then we need to do a state check in the code where it applies the state.
So is this the state_monster_field you wrote?
I would say use the stacking states script function to check how many states are applied and compare to how many cards are in the field for that state.

In test playing I see a bug. I could not exit the deck scene if I did not have 40 cards in my deck, but there were no more cards to add.
So we first need to talk about how the card is getting added to the field.
Is every card played that is tagged field getting added to the field?
In the original script, by default, if you used a card, these card went automatically from the hand to the graveyard. As I tried to recreate the game a little more similar of how the original card game (yugioh) works, cards first need to be played on the field. So after creating the location field, I changed the resolution of the method that say where the card should go after resolving, and instead of going to the graveyard, it is played on the field instead. Yes, every card when played should normally go to the field. (There's a few exception of cards that when they activate their effects, they go directly from the hand to the graveyard. But I think I can handle that part without a problem.)

There are other parts that are also not completed as well.
card_to_banish? is not used anywhere. card_to_field? is currently not used anywhere.
card_to_stack? is used in on_skill_ok
Those methods are just there in case I need to create some custom effects with the lunatic states, or lunatic damage, etc. Its useful when an effect ask me to send an specific card from one location to the grave and I don't want to use the "impact effect".

So where in the game play are you thinking this card is added to the field?
Is it from the on_skill_ok, so then a card is added to the stack and the field?
In the original card game of yugioh, when you "use" a monster card, you're "summoning" that monster. (unless that "use" is it effect and is an effect in the hand, grave or other location, but let ignore this for now and focus on the summon.) The summon of a monster is considered when that monster is placed on the field. Even if its from the hand, grave, banish, or deck, (or extra deck). That's why I need a method that check constantly the monsters on the field to add or remove that state, because if it was just when I summon from the hand, I could easily fix this by just adding the "add state" to the skill itself. But if another effect place a monster card from a location to the field, is not like you're playing the skill, so it won't add said state.

One though about this is if a dark magician is in the field and we add the state, then the next time we check the field again the dark magician card is still there, so the state is added again?
Yes, this was the problem I had with Fen's script. Any action would check again if a card is on the field and replay that state. That's why I assumed a method that just checks constantly would over-add the same state over and over.

Should the cards not be removed?
It would sort make sense to add cards to the field then remove them when the state is applied.
No, cards are removed from the field when they get destroyed/removed by a card effect. Then when that happend, yes, the state should be removed along that card.

But then I question if the battle needs a @field or just a card.field = true variable.
Well, yes. The location field allow me to create custom Impact effects that move cards from location, and I need "field" to exist for that. Also, this add the command "view field" so the player is aware of what tools it has as disposition.

My mind is not following well because it's resisting learning this card game a bit, and I seem to try looking at it when I'm tired.
Yes, I assumed you weren't very familiar with the card game. But the basic part is not that hard: In yugioh you have monsters, spells and traps to play. You need a field to play those monsters so they can attack the opponent to win the game. In the case of the original script, it would be perfected if I played just around the original spell cards and ignored monsters and traps. But in that case, the game would be very empty in terms of gameplay, specially considering how many spell cards play around monsters and traps.

If you need to always have the card in field and only taken out for a specific reason, then we need to do a state check in the code where it applies the state.
So is this the state_monster_field you wrote?
Yup, there's opponent skills that will remove my monsters from my field, in that case, those cards will be removed from the field along that state. The same if I use other effects that also remove my own cards from the field. There's card that for example ask you to remove one of your monsters by cost to use a powerful effect, like "Send a Spellcaster monster you control to the graveyard; draw 2 cards". So you pay removing your powerful monster but gain another cool effect.

I would say use the stacking states script function to check how many states are applied and compare to how many cards are in the field for that state.
Yeah, you're right. I hadn't thinked of something very obvius. I think I can handle mostly of that part.

In test playing I see a bug. I could not exit the deck scene if I did not have 40 cards in my deck, but there were no more cards to add.
Yes sorry, this not actually a bug by itself. The original script didn't have a minimum card deck so I added the code by myself (I couldn't believe I managed to code that). The problem is that for testing I don't usually enter the deck editor, I just add cards manually in the card game script that allow you to create a deck for every actor by default.

Anyways, thanks for being watching for this Roni! You're awesome for taking all this time.
the original card game (yugioh)
With that explanation, I understand now. My mind doesn't really know yugioh, but I played a little bit of Magic the Gathering. So some of what you just said made sense.

I'm thinking now the way it can be done it to run the other method when the card is played and then check to see if it is a 'monster' card and add a state.
When that card is removed we check the card to see if it is a monster card and remove the state.

This is assuming the states remain for as long as you have that card in play.

So with myself understanding more, tell me what you need help with.
You seem to have a fair bit done yourself so you are learning the code.
Feel free to send me a pm if you want to talk outside this thread.

I think we got this figured out, now it's just deciding where and how the card is checked.
So lets tackle this part. I need to take this a step at a time. (been a tiring few weeks, need a nap now.)
Sorry if this seems like repeating myself, I just might be.

The card is played to it gets placed into the field. Where is the process that does that?
Is it when calling the skill or did I miss something and there is a specific method to play cards.
This is assuming the states remain for as long as you have that card in play.
Yes, the state remains as long as the card is on the field, and at the same time, if the card leaves the field by any means, the state is also removed.

So with myself understanding more, tell me what you need help with.
So the mini script that Fen posted technically did part of the job already. I haven't tested other interactions, but the only problem I found right now is that it keep duplicating the states that I already have. So let's say I play the monster card "Dark Magician", then the state "Dark Magician" is also added. Now, if I summon another monster like "Kuriboh", besides the state of Kuriboh, it will also add me another copy of Dark Magician and I end with 3 states besides I only played 2 monsters. So its Dark Magician + Dark Magician + Kuriboh at the end. Is like every action add another stack to that state instead of checking the amount of cards on the field to do so.
You already commented about that and I tried to find a solution myself, but I couldn't figure add a method to tell the script like: "Don't add more copies of that state if its bigger than the amount of cards on the field!".

Feel free to send me a pm if you want to talk outside this thread.
Of course I won't mind, but I would feel bad for being too annoying. But anything that is more comfortable to you!

I think we got this figured out, now it's just deciding where and how the card is checked.
I commented something related but, Victor's passive state has a method to check continuosly a condition to add automatically a state. Maybe there's some tip in his code to figure out. But I read it from top to bottom but couldn't figure out what's that continuos method.

Sorry if this seems like repeating myself, I just might be.
Bro... You don't need to be sorry. In any case I should be the one apologizing for being so useless xd

The card is played to it gets placed into the field. Where is the process that does that?
Is it when calling the skill or did I miss something and there is a specific method to play cards.
In the original script is the def "use_item"
I just modified the part that said grave and changed it to field:

Original:
Ruby:
alias pc27_cardgame_scenebattle_use_item                              use_item
  def use_item
    pc27_cardgame_scenebattle_use_item
    if @subject.is_a?(Game_Actor)
      effect_name = @subject.current_action.item.impact
      invoke_card_impact(effect_name) if effect_name
      @subject.grave += @subject.stack
      @subject.stack = []
      restore_taken_cards(:proceed)
    end
  end

Modified by me:
Ruby:
  #-----------------------------------------------------------------------------
  # Evaluates card effects and puts the stack to the grave (alias)
  #-----------------------------------------------------------------------------
  alias pc27_cardgame_scenebattle_use_item                              use_item
  def use_item
    pc27_cardgame_scenebattle_use_item
    if @subject.is_a?(Game_Actor)
      effect_name = @subject.current_action.item.impact
      invoke_card_impact(effect_name) if effect_name
      @subject.field += @subject.stack unless @subject.current_action.item.frame == 5
      @subject.stack = []
      restore_taken_cards(:proceed)
      wait_for_message
    end
  end

If you're wondering why I added that "unless" is for the case of token monsters (which can only exist on the field and not in the grave or any other location, but don't pay attention to it for now).
Let's take a look at this duplication issue by analyzing what the maker does when there's a state already included in a Game_Actor's or Game_Enemy's list of states.

Ruby:
class Game_Battler
  def add_state(state_id)
    if state_addable?(state_id)
      add_new_state(state_id) unless state?(state_id)
      reset_state_counts(state_id)
      @result.added_states.push(state_id).uniq!
    end
  end
end

You could either create a copy of that particular method for your field or you could use a call to the Array's uniq method to get rid of all copies of that state.
So I think I'm close to achiev what I want, but there's still the same problem. I can't figure out how to check of amount of cards with the same id to add an state.

Ruby:
  def monsters_on_field?(n)
    @subject = BattleManager.actor
    for card in @subject.field
      return true if card.type == n
      end
    return false
  end

I created this method to check if the actor has a card with the type 3 which is monster in the card game script. Maybe creating this was kind of unnecesary, but I made it to make it simple to me.

Then, in the def perform_action I added these new lines:

Ruby:
  def perform_action
    @subject = BattleManager.actor
    hide_instant_action_windows
    execute_action
    #New method added to add an state
      if monsters_on_field?(3)
      BattleManager.actor.add_state(@skill.id)
      end
    #End of new method
    process_event
    loop do
      @subject.remove_current_action
      break if $game_troop.all_dead?
      break unless @subject.current_action
      @subject.current_action.prepare
      execute_action if @subject.current_action.valid?
    end
    process_action_end
    @subject.make_actions
    @subject = nil
    show_instant_action_windows
  end

So, this work for the basics: If you play a monster card from your hand to the field, it add the state corresponding.

But there's still 2 problems: If a card place a monster card from the deck or any other location to the field, it doesn't add the corresponding state. That's because the process in "perform_action" is taking the @skill.id of the card I'm currently playing. Or at least, that's why I'm assuming right now.
That's why the method monsters_on_field? doesn't work for this kind of cards.
So if I play a spell card that place a monster from the deck in the field, it won't add me any state, even if there's an state with the same id of that spell. while that part is okay, is still missing of adding that monster state.

The other problem is that the same goes for if a monster card is removed from the field. There's not a method that check if a card goes off from the field location to remove_state with that equal skill.id

But so far, I think I'm close to solve the first problem.
If I created another method "monsters_on_field?" but instead of checking if there's a monster card on the field, it checks the id of skills on the actor's field (and the amount for the stacks), I could create a method on the "perform_action" to add a monster state even if its not a monster card the card I'm currently playing.

But I tried several variations but can't figure out how to properly write it. I tried this:

Ruby:
def monster_skill_id
    @subject = BattleManager.actor
    for card in @subject.field
    nr = 0
    nr += card.xxx
    end
    return nr
  end

Where xxx I tried "skill.id", "skill", (skill.id), (skill), and a lot more of combinations but every combination return an error. So I can't figure out how to return the id of the skills on the field. (and also it would be important the amount of the same cards with the same skill.id so I can work with the stacked states). If anyone know how I could wrote that checker for skill id I would appreciate it.

EDIT:

So I just realized it and almost solved it!
The method I needed was card.id

So I added this in the perform_action:

for card in @subject.field
@skill.id = card.id
BattleManager.actor.add_state(@skill.id)

Which it solved one of the main problems: it adds the monster state of a monster that is placed on the field due to another card effect. Great! Now the problem is the times it add a state, and I cant figure out this:
If I play a card that place a monster on the field (even if its just a monster card, or a spell/trap that do it). It add that monster state, great! But after that, if I play another card that place a monster on the field, it add stacks of the monster state I already has on the field. I tried removing the method "monsters_on_field?" which is useless now, but that wasn't the problem.
with the code:
for card in @subject.field
@skill.id = card.id

@skill.id is not longer the skill.id of the card I'm currently playing, but the id of the cards on my field. But I can't figure out why it add so many copies of a same stack.
You see, one of your problems is that you want to solve issues way beyond your current coding level or expertise. Then you come up with convoluted code that reflects that lack of understanding and creates issues like the state duplication or state addition every turn. It's also important to notice that you're not totally familiar with the original card game script's code.

One good piece of advice is to fully understand the original code first and learn when and why a certain event, no matter if it already exists or you're about to add it to the script, should be called and executed right there, at that precise moment.

Second, you should learn how loops like the for loop actually work in Ruby and RGSS. So far you've just been kind of lucky but quite imprecise at the same time. Just one example of this: you try to retrieve a card ID as a skill ID. At first it seems natural to do so, yet, you don't know how to cherry pick the right one and stop execution once you did. What you're doing so far is iterating through the whole list (array) and just returning the last available ID just because.

Normally, when you want to learn scripting, you don't pick complex scripts like battle systems like the card script. That should wait till you can deal with simple issues and game flows found in other default scripts. Just saying this to point out how evident it is that you're in a learning rush and should slow down for a while and review the pieces of code that you "think" you already understand.

Original thread: https://forums.rpgmakerweb.com/threads/add-state-based-on-the-current-location-of-a-card.159627/