• 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.

Creating a new constant, PBCards, uninitialized constant error

3
Posts
8
Years
    • Seen May 17, 2016
    I'm learning more and more about Essentials every day.
    I'm currently stuck though.

    Okay Pokécommunity, are you able to help me?  

    I want to create a new Constant, in the same way as PBItems and PBTrainers and others.  I am naming it PBCards. I got an uninitialized constant error.

    SKIP TO THE BOTTOM IF YOU UNDERSTAND THIS

    Being a lazy person, and since this constant works in the exact same way as items, I copied and renamed the methods of creating PBItems.

    This is where I assume I created the class for the constant, in the compiler

    Code:
    def pbCompileCards
      records=[]
      constants=""
      itemnames=[]
      itemdescs=[]
      maxValue=0
      pbCompilerEachCommentedLine("PBS/items.txt"){|line,lineno|
         linerecord=pbGetCsvRecord(line,lineno,[0,"vnsuusuuUN"])
         record=SerialRecord.new
         record[ITEMID]        = linerecord[0]
         constant=linerecord[1]
         constants+="#{constant}=#{record[0]}\r\n"
         record[ITEMNAME]      = linerecord[2]
         itemnames[record[0]]=linerecord[2]
         record[ITEMPOCKET]    = linerecord[3]
         record[ITEMPRICE]     = linerecord[4]
         record[ITEMDESC]      = linerecord[5]
         itemdescs[record[0]]=linerecord[5]
         record[ITEMUSE]       = linerecord[6]
         record[ITEMBATTLEUSE] = linerecord[7]
         record[ITEMTYPE]      = linerecord[8]
         if linerecord[9]!="" && linerecord[9]
           record[ITEMMACHINE] = parseMove(linerecord[9])
         else
           record[ITEMMACHINE] = 0
         end
         maxValue=[maxValue,record[0]].max
         records.push(record)
      }
      MessageTypes.setMessages(MessageTypes::Items,itemnames)
      MessageTypes.setMessages(MessageTypes::ItemDescriptions,itemdescs)
      writeSerialRecords("Data/cards.dat",records)
      code="class PBCards\r\n#{constants}"
      code+="\r\ndef PBCards.getName(id)\r\nreturn pbGetMessage(MessageTypes::Items,id)\r\nend\r\n"
      code+="\r\ndef PBCards.getCount\r\nreturn #{records.length}\r\nend\r\n"
      code+="\r\ndef PBCards.maxValue\r\nreturn #{maxValue}\r\nend\r\nend"
      eval(code)
      pbAddScript(code,"PBCards")
      Graphics.update
    end

    As you can see, I'm using the data (for now) from PBItems, and I just practically replaced 'PBItems' with 'PBCards'

    So then I called this code from the slightly edited module called CardStorageHelper (It is ItemStorageHelper with a couple replacements).  It is in PSystem_Utilities, right next to ItemStorageHelper.   (I also made a new global variable somewhere else, $CardData, which mocks $ItemData, and $CardCollection, mocking $PokemonBag).

    Code:
    module CardStorageHelper
      # Returns the quantity of the given item in the items array, maximum size per slot, and item ID
      def self.pbQuantity(items,maxsize,item)
        ret=0
        for i in 0...maxsize
          itemslot=items[i]
          if itemslot && itemslot[0]==item
            ret+=itemslot[1]
          end
        end
        return ret
      end
    
      # Deletes an item from items array, maximum size per slot, item, and number of items to delete
      def self.pbDeleteItem(items,maxsize,item,qty)
        raise "Invalid value for qty: #{qty}" if qty<0
        return true if qty==0
        ret=false
        for i in 0...maxsize
          itemslot=items[i]
          if itemslot && itemslot[0]==item
            amount=[qty,itemslot[1]].min
            itemslot[1]-=amount
            qty-=amount
            items[i]=nil if itemslot[1]==0
            if qty==0
              ret=true
              break
            end
          end
        end
        items.compact!
        return ret
      end
    
      def self.pbCanStore?(items,maxsize,maxPerSlot,item,qty)
        raise "Invalid value for qty: #{qty}" if qty<0
        return true if qty==0
        for i in 0...maxsize
          itemslot=items[i]
          if !itemslot
            qty-=[qty,maxPerSlot].min
            return true if qty==0
          elsif itemslot[0]==item && itemslot[1]<maxPerSlot
            newamt=itemslot[1]
            newamt=[newamt+qty,maxPerSlot].min
            qty-=(newamt-itemslot[1])
            return true if qty==0
          end
        end
        return false
      end
    
      def self.pbStoreItem(items,maxsize,maxPerSlot,item,qty,sorting=false)
        Kernel.pbMessage("Inside Module")
        raise "Invalid value for qty: #{qty}" if qty<0
        return true if qty==0
        for i in 0...maxsize
          itemslot=items[i]
          if !itemslot
            Kernel.pbMessage("Went to if 1")
            items[i]=[item,[qty,maxPerSlot].min]
            qty-=items[i][1]
            if sorting
              items.sort! if POCKETAUTOSORT[$CardData[item][ITEMPOCKET]]
            end
            return true if qty==0
          elsif itemslot[0]==item && itemslot[1]<maxPerSlot
            Kernel.pbMessage("went to if 2")
            newamt=itemslot[1]
            newamt=[newamt+qty,maxPerSlot].min
            qty-=(newamt-itemslot[1])
            itemslot[1]=newamt
            return true if qty==0
          end
        end
        Kernel.pbMessage("Did not work in the end")
        return false
      end
    end

    I call this Module's function, pbStoreItem, from the script Kernel.pbReceiveCard, in PField_Field (mocks pbReceiveItem)
    Code:
    #***Custom***
    def Kernel.pbReceiveCard(item,quantity=1,plural=nil)
      if item.is_a?(String) || item.is_a?(Symbol)
        item=getID(PBCards,item)
      end
      return false if !item || item<=0 || quantity<1
      itemname=PBCards.getName(item)
      pocket=pbGetPocket(item)
      if $ItemData[item][ITEMUSE]==3 || $ItemData[item][ITEMUSE]==4
        Kernel.pbMessageBlack(0,false,_INTL("\\se[itemlevel]{1} obtained <c3=52b5ff,29638c>{2}\\c[0]!\\nIt contained <c3=52b5ff,29638c>{3}\\c[0].\\wtnp[15]",
           $Trainer.name,itemname,PBMoves.getName($ItemData[item][ITEMMACHINE])))
      elsif isConst?(item,PBCards,:LEFTOVERS)
        Kernel.pbMessageBlack(0,false,_INTL("\\se[itemlevel]{1} obtained some <c3=52b5ff,29638c>{2}\\c[0]!\\wtnp[15]",
           $Trainer.name,itemname))
      elsif quantity>1
        if plural
          Kernel.pbMessageBlack(0,false,_INTL("\\se[itemlevel]{1} obtained <c3=52b5ff,29638c>{2}\\c[0]!\\wtnp[15]",
             $Trainer.name,plural))
        else
          Kernel.pbMessageBlack(0,false,_INTL("\\se[itemlevel]{1} obtained <c3=52b5ff,29638c>{2}s\\c[0]!\\wtnp[15]",
             $Trainer.name,itemname))
        end
      else
        Kernel.pbMessageBlack(0,false,_INTL("\\se[itemlevel]{1} obtained <c3=52b5ff,29638c>{2}\\c[0]!\\wtnp[15]",
           $Trainer.name,itemname))
      end
      if $CardCollection.pbStoreCard(item,quantity)   # If item can be added
        Kernel.pbMessage("Item was added!")
        if quantity>1
          if plural
            Kernel.pbMessageBlack(0,false,_INTL("{1} put the <c3=52b5ff,29638c>{2}\\c[0]\r in the <c3=52b5ff,29638c>{3}\\c[0] Pocket.",
               $Trainer.name,plural,PokemonBag.pocketNames()[pocket]))
          else
            Kernel.pbMessageBlack(0,false,_INTL("{1} put the <c3=52b5ff,29638c>{2}s\\c[0]\r in the <c3=52b5ff,29638c>{3}\\c[0] Pocket.",
               $Trainer.name,itemname,PokemonBag.pocketNames()[pocket]))
          end
        else
          Kernel.pbMessageBlack(0,false,_INTL("{1} put the <c3=52b5ff,29638c>{2}\\c[0]\r in the <c3=52b5ff,29638c>{3}\\c[0] Pocket.",
             $Trainer.name,itemname,PokemonBag.pocketNames()[pocket]))
        end
        return true
      else   # Can't add the item
        Kernel.pbMessage("Item was not added!")
        return false
      end
    end

    I call it through this
    Code:
    Kernel.pbReceiveCard(:POKEBALL,5)


    This is the error I get

    Code:
    Exception: RuntimeError
    Message: Script error within event 29, map 77 (Red's house):
    Exception: NameError
    Message: Section100:2340:in `pbReceiveCard'uninitialized constant PBCards
    ***Full script:
    Kernel.pbReceiveCard(:POKEBALL,5)
    
    Interpreter:243:in `pbExecuteScript'
    (eval):1:in `pbExecuteScript'
    Interpreter:1606:in `eval'
    Interpreter:243:in `pbExecuteScript'
    Interpreter:1606:in `command_355'
    Interpreter:494:in `execute_command'
    Interpreter:193:in `update'
    Interpreter:106:in `loop'
    Interpreter:198:in `update'
    Scene_Map:104:in `update'
    
    Interpreter:276:in `pbExecuteScript'
    Interpreter:1606:in `command_355'
    Interpreter:494:in `execute_command'
    Interpreter:193:in `update'
    Interpreter:106:in `loop'
    Interpreter:198:in `update'
    Scene_Map:104:in `update'
    Scene_Map:102:in `loop'
    Scene_Map:115:in `update'
    BW_MapClock:66:in `main'

    I don't know how to 'initialize' the constant I created.  Everything else seems in order though.
    How would I solve this?

    BTW, I'm using Klein's BW kit and Luka SJ Battle system if that makes any impact whatsoever.

    Thanks!
     
    9
    Posts
    8
    Years
    • Seen Mar 2, 2016
    I think it might be worth looking at this guy in PItem_Items:
    Code:
    class ItemHandlerHash < HandlerHash
      def initialize
        super(:PBItems)
      end
    end
    I can't find PBItems being initialized anywhere prior to this, so if you make an equivalent for your PBCards it might work. No guarantees though since I'm also still getting to grips with using ruby.
     

    Maruno

    Lead Dev of Pokémon Essentials
    5,286
    Posts
    16
    Years
    • Seen May 3, 2024
    The HandlerHash is for effects, not defining the objects themselves. That wouldn't make a difference.
     
    3
    Posts
    8
    Years
    • Seen May 17, 2016
    I am not quite sure, what is PBItems? How is it different from $ItemData? Why are these constants' code written at run-time? Where do they get their data from?

    Well, I guess the constant gets its data from this function here pbCompilerEachCommentedLine("PBS/items.txt")

    and stores it here
    writeSerialRecords("Data/cards.dat",records)

    I don't have a "cards.dat" file. If I make one, will that make a difference?

    If anyone could answer any of these questions, that would be helpful.
     
    Back
    Top