Jump to content
Sign in to follow this  
Mattsuharu

Id and Quantity of something?

Recommended Posts

Hello, me again with another script issue.

I was breaking my head for 2 days in a row with this. In a script I'm trying to edit, I can't figure out how to create the next script call.

This is the original script call that work perfectly:
 

def has_card_in_deck?(id)
actor = $game_actors[$game_variables[27]]
for card in actor.deck
return true if card.id == id
end
return false
end


This return me if the card with a certain ID is in the deck of my actor.

Now, the problem is that I tried to make another similar script call and I though it would be easy, but is not...
 

def has_2_cards?(id)
actor = $game_actors[$game_variables[27]]
for card in actor.deck
return true if card.id == id * 2
end
return false
end


(This is just an example, I know the * 2 won't return me the amount of that card)

I want to create the almost exact script call but instead to return me if there's any copy of the card with that ID, I need to return me if there's 2 or more with the same id. I read a lot of articles of ruby code, but can't figure out how to separate the "ID" and the "object quantity". I can't find a way to ask by code how many copies of something with the same ID I have in the deck. I'm really struggling with this one. Thanks in advance to whoever hero who can help me out with this!

Share this post


Link to post
Share on other sites

To use the existing format you have, you could alter it like this:

def has_2_cards?(id)
  actor = $game_actors[$game_variables[27]]
  amount = 0
  for card in actor.deck
    amount += 1 if card.id == id
    return true if amount == 2
  end
  return false
end

Basically, instead of returning true as soon as desired card is found, I created a local variable and assigned 0 to it. Then, each time the desired card is found, it adds 1 to the variable. Then, as soon as the variable hits 2, it breaks from the loop and returns true, meaning the script call will return true if the actor has at least 2 cards you're checking for.

 

Alternatively, I'm thinking of using the .count method. I assume "deck" is an array, that consists of skills, given there's checking for .id for each card, which I assume to be an ID of the skill. If I'm guessing correctly, something like this would work too:

def has_2_cards?(id)
  actor = $game_actors[$game_variables[27]]
  amount = actor.deck.count($data_skills[id])
  return true if amount >= 2
  return false
end

Going a step further with the alternate method, perhaps a method to count cards would be helpful to you?

def count_cards(id)
  actor = $game_actors[$game_variables[27]]
  return actor.deck.count($data_skills[id])
end

This method would count how many cards with specified ID the actor has.

With that, you could for example, put this conditional branch:

page 4: script: count_cards(3) >= 5

and it would be true if the actor (under $game_variables[27]) would have 5 or more cards with ID 3.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
Top ArrowTop Arrow Highlighted