• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

[Scripting Question] Problem with a move

TheShinyMew

Wild Challenger appeared!
125
Posts
13
Years
I made a move called Lucky Star, which revives any Pokemon in a party without the user fainting. Here are the code of it in PokeBattle_MoveEffects:
Code:
class PokeBattle_Move_171 < PokeBattle_Move
  def pbEffect(attacker,opponent,hitnum=0,alltargets=nil,showanimation=true)
    if attacker.effects[PBEffects::LuckyStar]
      @battle.pbDisplay(_INTL("But it failed!"))
      return -1
    end
    pbShowAnimation(@id,attacker,nil,hitnum,alltargets,showanimation)
    attacker.effects[PBEffects::LuckyStar]=true
    @battle.pbDisplay(_INTL("A lucky star had summoned somewhere."))
    return 0
  end
end

And in PokeBattle_Battle:
Code:
  def pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
    if pkmnidxTo>=0
      party=pbParty(idxPokemon)
      if pkmnidxTo>=party.length
        return false
      end
      if !party[pkmnidxTo]
        return false
      end
      if party[pkmnidxTo].isEgg?
        pbDisplayPaused(_INTL("An Egg can't battle!")) if showMessages 
        return false
      end
      if !pbIsOwner?(idxPokemon,pkmnidxTo)
        owner=pbPartyGetOwner(idxPokemon,pkmnidxTo)
        pbDisplayPaused(_INTL("You can't switch {1}'s Pokémon with one of yours!",owner.name)) if showMessages 
        return false
      end
      if party[pkmnidxTo].hp<=0
        if isConst?(party[pkmnidxTo].species,PBSpecies,:GENESUNE) ||
          isConst?(party[pkmnidxTo].species,PBSpecies,:PERFEON) ||
          isConst?(party[pkmnidxTo].ability,PBAbilities,:REBIRTH)
          party[pkmnidxTo].healHP
          party[pkmnidxTo].healStatus
          party[pkmnidxTo].healPP
          pbDisplayPaused(_INTL("{1} is revived by its power!",party[pkmnidxTo].name))
          return true
      thatpokemon=party[pkmnidxTo]
      effects=nil
      elsif thatpokemon.effects[PBEffects::LuckyStar]=true
        thatpokemon.healHP
        thatpokemon.healStatus
        thatpokemon.healPP
        pbDisplayPaused(_INTL("{1} came back to life!",party[pkmnidxTo].name))
        thatpokemon.effects[PBEffects::LuckyStar]=false
       return true
        else
        pbDisplayPaused(_INTL("{1} has no energy left to battle!",party[pkmnidxTo].name)) if showMessages 
        return false
        end
      end
      if @battlers[idxPokemon].pokemonIndex==pkmnidxTo ||
         @battlers[idxPokemon].pbPartner.pokemonIndex==pkmnidxTo
        pbDisplayPaused(_INTL("{1} is already in battle!",party[pkmnidxTo].name)) if showMessages 
        return false
      end
    end
    return true
  end

But when I used Lucky Star on a fainted Pokemon, I got a powerful error like this:
Code:
Exception: NoMethodError
Message: undefined method `effects' for nil:NilClass
PokeBattle_Battle:1350:in `pbCanSwitchLax?'
PokeBattle_Battle:1374:in `pbCanSwitch?'
PokeBattle_OtherScenes:2682:in `pbSwitch'
PokeBattle_OtherScenes:2664:in `loop'
PokeBattle_OtherScenes:2691:in `pbSwitch'
PokeBattle_Battle:1614:in `pbSwitchPlayer'
PokeBattle_Battle:2930:in `pbCommandPhase'
PokeBattle_Battle:2879:in `loop'
PokeBattle_Battle:2969:in `pbCommandPhase'
PokeBattle_Battle:2868:in `each'

What's wrong with this?
 

Maruno

Lead Dev of Pokémon Essentials
5,285
Posts
16
Years
The error message says there isn't a method called effects for a nil object. And it's right, there isn't. Why would there be? Yet you're calling it.

So let's have a look in your code (in the method it says the error is in, pbCanSwitchLax?, for what calls the effects method. Turns out the code is thatpokemon.effects, therefore thatpokemon must be nil if it's coming up with this error.

Let's have a look at all the lines which reference thatpokemon. I properly indented your code to make it a bit more obvious:
Code:
  def pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
    if pkmnidxTo>=0
      party=pbParty(idxPokemon)
      if pkmnidxTo>=party.length
        return false
      end
      if !party[pkmnidxTo]
        return false
      end
      if party[pkmnidxTo].isEgg?
        pbDisplayPaused(_INTL("An Egg can't battle!")) if showMessages 
        return false
      end
      if !pbIsOwner?(idxPokemon,pkmnidxTo)
        owner=pbPartyGetOwner(idxPokemon,pkmnidxTo)
        pbDisplayPaused(_INTL("You can't switch {1}'s Pokémon with one of yours!",owner.name)) if showMessages 
        return false
      end
      if party[pkmnidxTo].hp<=0
        if isConst?(party[pkmnidxTo].species,PBSpecies,:GENESUNE) ||
           isConst?(party[pkmnidxTo].species,PBSpecies,:PERFEON) ||
           isConst?(party[pkmnidxTo].ability,PBAbilities,:REBIRTH)
          party[pkmnidxTo].healHP
          party[pkmnidxTo].healStatus
          party[pkmnidxTo].healPP
          pbDisplayPaused(_INTL("{1} is revived by its power!",party[pkmnidxTo].name))
          return true
          [COLOR="Red"]thatpokemon=party[pkmnidxTo][/COLOR]
          effects=nil
        elsif [COLOR="Red"]thatpokemon.effects[PBEffects::LuckyStar]=true[/COLOR]
          [COLOR="Red"]thatpokemon.healHP
          thatpokemon.healStatus
          thatpokemon.healPP[/COLOR]
          pbDisplayPaused(_INTL("{1} came back to life!",party[pkmnidxTo].name))
          [COLOR="Red"]thatpokemon.effects[PBEffects::LuckyStar]=false[/COLOR]
          return true
        else
          pbDisplayPaused(_INTL("{1} has no energy left to battle!",party[pkmnidxTo].name)) if showMessages 
          return false
        end
      end
      if @battlers[idxPokemon].pokemonIndex==pkmnidxTo ||
         @battlers[idxPokemon].pbPartner.pokemonIndex==pkmnidxTo
        pbDisplayPaused(_INTL("{1} is already in battle!",party[pkmnidxTo].name)) if showMessages 
        return false
      end
    end
    return true
  end
I leave it to you to figure out the rest. You must have some amount of coding skill if you've come this far, so I presume you know how if statements work.
 

TheShinyMew

Wild Challenger appeared!
125
Posts
13
Years
I think the "effects" thing in "def pbCanSwitchLax?" don't need to be used, not the "thatpokemon" thing.

I got this error again even though I get rid of the "thatpokemon" thing. The "effects" thing an undefined method? Anyone know that?

Code:
---------------------------
Pokemon Clear Ice
---------------------------
Exception: NoMethodError

Message: undefined method `effects' for #<PokeBattle_Pokemon:0x5715b98>

PokeBattle_Battle:1348:in `pbCanSwitchLax?'

PokeBattle_Battle:1372:in `pbCanSwitch?'

PokeBattle_OtherScenes:2682:in `pbSwitch'

PokeBattle_OtherScenes:2664:in `loop'

PokeBattle_OtherScenes:2691:in `pbSwitch'

PokeBattle_Battle:1612:in `pbSwitchPlayer'

PokeBattle_Battle:2928:in `pbCommandPhase'

PokeBattle_Battle:2877:in `loop'

PokeBattle_Battle:2967:in `pbCommandPhase'

PokeBattle_Battle:2866:in `each'



This exception was logged in ./errorlog.txt.

Press Ctrl+C to copy this message to the clipboard.
---------------------------
OK   
---------------------------
 
Last edited by a moderator:
Back
Top