Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Variable adding with a few scripts.

Hey everyone I haven't been around in a while but I got back into working on my game and I've gotten stuck. I'm using two different script systems for some of the 'professions' the player can do. One for gathering herbs and one for forging armor or weapons. I had these two 'professions' handled by two event systems but finally found some scripts that work alot better. What I'd like to do is when the player has successfully created or gathered an item that it adds a point to a variable.

Herb Gathering system
Part 1
Code:
#==============================================================================

#     Window_Plant

#------------------------------------------------------------------------------

#     The Window with the plant in it

#     By: Tiberius

#==============================================================================

 

  

 

class Window_Plant < Window_Base

  attr_reader :done

  attr_reader :rm

  attr_accessor :time_window

  

  #--------------------------------------------------------------------------

  #    Initialize

  #--------------------------------------------------------------------------

  def initialize(plant_id, time, size, amount)

    super(112, 64, 416, 416)

    self.contents = Bitmap.new(width - 32, height - 32)

    @amount = amount

    @rm = false

    @x = 0

    @y = -1

    @time = time * 40

    @id = plant_id

    @name = $data_items[plant_id].name

    @stuff = []

    for x in 0..15

      @stuff.push([])

      for y in 0..14

        @stuff[x][y] = 1

      end

    end

    cx = rand(2) + 7

    pbs = []

    lastm = 0

    for y in 0..size

      @stuff[cx][y] = 2

      case rand(lastm)

      when 4, 6, 8, 10, 12, 14, 16, 18

        if rand(3) > 0

          cx += 1

          @stuff[cx][y] = 2

          lastm = 0

        end

        if rand(3) > 0 and y < 12

          @stuff[cx + 1][y] = 2

          @stuff[cx + 2][y] = 2

          @stuff[cx + 2][y + 1] = 2

          @stuff[cx + 2][y + 2] = 2

          pbs.push([cx + 2, y + 1, 0])

          lastm = 0

        end

      when 5, 7, 9, 11, 13, 15, 17, 19

        if rand(3) > 0

          cx -= 1

          @stuff[cx][y] = 2

          lastm = 0

        end

        if rand(3) > 0 and y < 12

          @stuff[cx - 1][y] = 2

          @stuff[cx - 2][y] = 2

          @stuff[cx - 2][y + 1] = 2

          @stuff[cx - 2][y + 2] = 2

          pbs.push([cx - 2, y + 1, 1])

          lastm = 0

        end

        pbts = []

        for i in pbs

          if i[1] < 11 and rand(2) > 0

            pbts.push(i)

            y = i[1] + rand(2)

            case i[2]

            when 0

              @stuff[i[0] + 1][y] = 2

              @stuff[i[0] + 2][y] = 2

              @stuff[i[0] + 2][y + 1] = 2

              @stuff[i[0] + 2][y + 2] = 2

            when 1

              @stuff[i[0] - 1][y] = 2

              @stuff[i[0] - 2][y] = 2

              @stuff[i[0] - 2][y + 1] = 2

              @stuff[i[0] - 2][y + 2] = 2

            end

          end

        end

        for i in pbts

          pbs.delete(i)

        end

      end

      lastm += 2

    end

    dirt = dto

    for x in 0..15

      for y in 0..14

        if @stuff[x][y] == 1 and dirt.include?([x, y]) == false

          @rm = true

        end

      end

    end

    refresh

  end

  #--------------------------------------------------------------------------

  #    Done?

  #--------------------------------------------------------------------------

  def done?

    done = true

    for x in 0..15

      for y in 0..14

        if @stuff[x][y] == 2

          if x > 0

            done = false if @stuff[x - 1][y] == 1

          end

          if x < 15

            done = false if @stuff[x + 1][y] == 1

          end

          if y > 0

            done = false if @stuff[x][y - 1] == 1

          end

          if y < 14

            done = false if @stuff[x][y + 1] == 1

          end

        end

      end

    end

    return done

  end

  #--------------------------------------------------------------------------

  #    Refresh

  #--------------------------------------------------------------------------

  def refresh

    self.contents.clear

    types = ["DDirt", "Dirt", "DDirt"]

    for x in 0..15

      for y in 0..14

        bitmap = RPG::Cache.icon(types[@stuff[x][y]])

        self.contents.blt(x * 24, y * 24 + 24, bitmap, Rect.new(0, 0, 24, 24))

        if @stuff[x][y] == 2

          if y == 0

            bitmap = RPG::Cache.icon("above")

            self.contents.blt(x * 24, y * 24 + 24, bitmap, Rect.new(0, 0, 24, 24))

            bitmap = RPG::Cache.icon($data_items[@id].icon_name)

            self.contents.blt(x * 24, 0, bitmap, Rect.new(0, 0, 24, 24))

          end

          if @stuff[x][y + 1] == 2

            bitmap = RPG::Cache.icon("below")

            self.contents.blt(x * 24, y * 24 + 24, bitmap, Rect.new(0, 0, 24, 24))

          end

          if @stuff[x][y - 1] == 2

            bitmap = RPG::Cache.icon("above")

            self.contents.blt(x * 24, y * 24 + 24, bitmap, Rect.new(0, 0, 24, 24))

          end

          if @stuff[x - 1][y] == 2

            bitmap = RPG::Cache.icon("left")

            self.contents.blt(x * 24, y * 24 + 24, bitmap, Rect.new(0, 0, 24, 24))

          end

          if @stuff[x + 1][y] == 2

            bitmap = RPG::Cache.icon("right")

            self.contents.blt(x * 24, y * 24 + 24, bitmap, Rect.new(0, 0, 24, 24))

          end          

        end

      end

    end

    bitmap = RPG::Cache.icon("We_Sword_13")

    self.contents.blt(@x * 24, @y * 24 + 24, bitmap, Rect.new(0, 0, 24, 24))

  end

  #--------------------------------------------------------------------------

  #    Update

  #--------------------------------------------------------------------------

  def update

    super

    @time_window.set_text("Digging up " + @name + ". Time Remaining: " + ((@time + 10) / 40).round.to_s, 1)

    moved = false

    if Input.repeat?(Input::DOWN)

      if @y < 14

        @y += 1

        moved = true

      end

    end

    if Input.repeat?(Input::UP)

      if @y > 0

        @y -= 1

        moved = true

      end

    end

    if Input.repeat?(Input::RIGHT)

      if @x < 15

        @x += 1

        moved = true

      end

    end

    if Input.repeat?(Input::LEFT)

      if @x > 0

        @x -= 1

        moved = true

      end

    end

    if @y > -1

      @time -= 1

      if @time < 0 or @stuff[@x][@y] == 2

        refresh

        Audio.se_play("Audio/SE/057-Wrong01")

        @time_window.set_text("You cut the root! Press action key to continue...", 1)

        @time_window.update

        @done = true

        return

      end

    end

    if moved

      if @y > -1

        @stuff[@x][@y] = 0

        Audio.se_play("Audio/SE/129-Earth01")

      else

        $game_system.se_play($data_system.cursor_se)

      end

      refresh

      if done?

        $game_system.me_play($game_system.battle_end_me)

        $game_party.gain_item(@id, 1)

        $game_variables[104] + 1

        extra = ""

        if @amount > 1

          extra = "(" + @amount.to_s + ")"

        end

        @time_window.set_text("Gained " + @name + extra + "! Press action key to continue...", 1)

        @time_window.update

        @done = true

      end

    end

  end

  #--------------------------------------------------------------------------

  #    Dirt touching origin

  #--------------------------------------------------------------------------

  def dto

    dt = [[0, 0]]

    checked = []

    for x in 0..15

      checked.push([])

      for y in 0..14

        checked[x].push(false)

      end

    end

    for i in 0..50

      for x in 0..15

        for y in 0..14

          if dt.include?([x, y]) and !checked[x][y]

            checked[x][y] = true

            if x > 0

              if 1 == @stuff[x - 1][y] and !dt.include?([x - 1, y])

                dt.push([x - 1, y])

              end

            end

            if x < 15

              if 1 == @stuff[x + 1][y] and !dt.include?([x + 1, y])

                dt.push([x + 1, y])

              end

            end

            if y > 0

             if 1 == @stuff[x][y - 1] and !dt.include?([x, y - 1])

               dt.push([x, y - 1])

              end

            end

            if y < 14

              if 1 == @stuff[x][y + 1] and !dt.include?([x, y + 1])

                dt.push([x, y + 1])

              end

            end

          end

        end

      end

    end

    return dt

  end

end

Part 2
Code:
#==============================================================================

#     Scene_Herb

#------------------------------------------------------------------------------

#     The scene with herbalism

#     By: Tiberius

#==============================================================================

 

class Scene_Herb

  #--------------------------------------------------------------------------

  #    Initialize

  #--------------------------------------------------------------------------

  def initialize(plant_id, time = 8, size = 7, amount = 1)

    @plant_id = plant_id

    @time = time

    @size = size

    @size = 13 if @size > 13

    @size = 1 if @size < 1

    @amount = amount

    @amount = 1 if @amount < 1

  end

  #--------------------------------------------------------------------------

  #    Main

  #--------------------------------------------------------------------------

  def main

    @plant_window = Window_Plant.new(@plant_id, @time, @size, @amount)

    while @plant_window.rm

      @plant_window.dispose

      @plant_window = Window_Plant.new(@plant_id, @time, @size, @amount)

    end

    @time_window = Window_Help.new

    @plant_window.time_window = @time_window

    Graphics.transition

    loop do

      Graphics.update

      Input.update

      update

      if $scene != self

        break

      end

    end

    Graphics.freeze

    @plant_window.dispose

    @time_window.dispose

  end

  #--------------------------------------------------------------------------

  #    Update

  #--------------------------------------------------------------------------

  def update

    if !@plant_window.done

      @plant_window.update

      @time_window.update

    else

      if Input.trigger?(Input::C)

        $scene = Scene_Map.new

      end

    end

    if Input.trigger?(Input::B)

      $game_system.se_play($data_system.cancel_se)

      $scene = Scene_Map.new

    end

  end

end

I thought I had it with plugging " $game_variables[104] + 1" into it but that didn't work I also tried " $game_variables[104] =+1" but that just set the variable to '1' instead of adding '1'.

I'm also using Arev's Item Crafting system to create the weapons and armor but I'd also like to have it add to a variable each time an item is created successfully.

Item Crafting System
Code:
#==============================================================================

# ** Item Forgeing System

#    version 1.00

#    created by arev (rmxp.pl)

#    released 28-12-2009

#==============================================================================

# Installation & Compatibility:

#

# Copy this tab and paste it above 'Main' in your project.

# The system is compatible with default RMXP system. 

# It doesn't override any methods, so it's probably compatible

# with lots of other things.

#

#

#

# Usage:

#

# Below this introduction you'll find an array called RECIPES.

# New recipes are added with new arrays like the following:

# ['i12', 'i78', 'w3', 'a45', 'a45', 'z', 'i-99']

# i stands for items, w for weapons and a for armors.

# Acording to this recipe - to make an item of ID 99 you need

# one item with ID 12, one item with ID 78, one weapon with ID 3,

# two armors with ID 45. There is a maximum number of six components.

# If you want to make a recipe consisting of smaller number of

# components just fill the remaining fields with 'z'.

# The last ithem in the array is the recipe result. The first character

# works just like before, then there's a dash required, followed by ID

# of an item.

# 

# To enter the forgeing scene call a script like this:

# $scene = Scene_Forge.new

#

# During the forgeing the order of items in the component window

# is not important - they are sorted within the script.

#

#

#

# License:

#

# You are free to use this script in your non-commercial project.

# You can redistribute this script (free of charge) only if you provide 

# information about its original author.

# If you want to use this script in a commercial project please contact me

# via [url=http://www.rmxp.pl]http://www.rmxp.pl[/url] or [url=http://www.arpgmaker.com]http://www.arpgmaker.com[/url] private message system.

#==============================================================================

 

RECIPES = [

          ["i42", "i46", "z", "z", "z", "z", "i-52"],

          ["i42", "i43", "i44", "i46", "z", "z", "i-54"],

          ["i42", "i43", "i44", "i46", "z", "z", "i-53"],

          ["i43", "i46", "z", "z", "z", "z", "i-50"],

          ["i43", "i46", "i46", "z", "z", "z", "i-51"],

          ["i33", "i45", "z", "z", "z", "z", "i-55"],

          ]

#------------------------------------------------------------------------------

class Window_Forge_Components < Window_Selectable

  

  attr_accessor :data

  

  def initialize

    super(0, 64, 320, 224)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.index = 0

    self.active = true

    @data = [nil, nil, nil, nil, nil, nil]

    @item_max = 6

    refresh

  end

  

  def item

    return @data[self.index]

  end

  

  def refresh

    self.contents.clear

    for i in 0..5

      x = 0

      y = i * 32

      if @data[i] != nil

        item = @data[i]

        bitmap = RPG::Cache.icon(item.icon_name)

        self.contents.blt(4 + x, y + 4, bitmap, Rect.new(0, 0, 24, 24))

        self.contents.font.color = normal_color

        self.contents.draw_text(x + 32, y, 160, 32, item.name, 0)

      else

        self.contents.font.color = disabled_color

        self.contents.draw_text(4 + x, y, 160, 32, "Empty", 0)

      end

    end

  end

  

  def update_cursor_rect

    self.cursor_rect.set(0, self.index * 32, 288, 32)

  end

  

  def nullify

    @data = [nil, nil, nil, nil, nil, nil]

  end

  

  def transmutation

    temp = []

    for i in 0..5

      if @data[i] == nil

        temp << "0"

      else

        case @data[i]

        when RPG::Item

          temp << "i" + @data[i].id

        when RPG::Weapon

          temp << "w" + @data[i].id

        when RPG::Armor

          temp << "a" + @data[i].id

        end

      end

    end

    temp = temp.sort

    return temp

  end

  

end

#------------------------------------------------------------------------------

class Window_Forge_Result < Window_Base

  

  attr_accessor :item

  

  def initialize

    super(320,64,320,224)

    self.contents = Bitmap.new(width - 32, height - 32)

    @item = nil

    @item_code = ""

    refresh

  end

  

  def refresh

    self.contents.clear

    if @item == nil

      self.contents.font.color = normal_color

      self.contents.draw_text(4,0,288,32, "Select ingredients.", 0)

    else

      bitmap = RPG::Cache.icon(@item.icon_name)

      self.contents.font.color = text_color(3)

      self.contents.draw_text(4,0,288,32, "Spectacular success!", 0)

      self.contents.blt(4, 65, bitmap, Rect.new(0, 0, 24, 24))

      self.contents.font.color = normal_color

      self.contents.draw_text(32, 65, 288, 32, @item.name, 0)

      self.contents.font.color = disabled_color

      self.contents.draw_text(4,128,288,32, "Press the 'Z' button to finalize", 0)

      self.contents.draw_text(4,160,288,32, "the forgeing process!", 0)

    end

  end

  

  def make_item(text="")

    @item_code = text

    if @item_code.include?("-")

      temp = @item_code.split("-")

      @item = (temp[0] == "i" ? $data_items : "w" ? $data_weapons : $data_armors)[temp[1].to_i]

      refresh

    end

  end

  

  def nullify

    @item = nil

    refresh

  end

  

end

#------------------------------------------------------------------------------

class Window_Forge_Item < Window_Selectable

 

  def initialize

    super(0, 288, 640, 192)

    @column_max = 2

    refresh

    self.active = false

    self.index = -1

  end

 

  def item

    return @data[self.index]

  end

 

  def refresh

    if self.contents != nil

      self.contents.dispose

      self.contents = nil

    end

    @data = []

    for i in 1...$data_items.size

      if $game_party.item_number(i) > 0

        @data.push($data_items[i])

      end

    end

    unless $game_temp.in_battle

      for i in 1...$data_weapons.size

        if $game_party.weapon_number(i) > 0

          @data.push($data_weapons[i])

        end

      end

      for i in 1...$data_armors.size

        if $game_party.armor_number(i) > 0

          @data.push($data_armors[i])

        end

      end

    end

    @data.push(nil)

    @item_max = @data.size

    self.contents = Bitmap.new(width - 32, row_max * 32)

    for i in 0...@item_max-1

      draw_item(i)

    end

  end

 

  def draw_item(index)

    item = @data[index]

    x = 4 + index % @column_max * 320

    y = index / @column_max * 32

    case item

    when RPG::Item

      number = $game_party.item_number(item.id)

    when RPG::Weapon

      number = $game_party.weapon_number(item.id)

    when RPG::Armor

      number = $game_party.armor_number(item.id)

    end

    bitmap = RPG::Cache.icon(item.icon_name)

    self.contents.blt(x, y + 4, bitmap, Rect.new(0, 0, 24, 24))

    self.contents.font.color = normal_color

    self.contents.draw_text(x + 28, y, 212, 32, item.name, 0)

    self.contents.draw_text(x + 240, y, 16, 32, ":", 1)

    self.contents.draw_text(x + 256, y, 24, 32, number.to_s, 2)

  end

 

  def update_help

    @help_window.set_text(self.item == nil ? "" : self.item.description)

  end

 

  def update_cursor_rect

    if @index < 0

      self.cursor_rect.empty

      return

    end

    row = @index / @column_max

    if row < self.top_row

      self.top_row = row

    end

    if row > self.top_row + (self.page_row_max - 1)

      self.top_row = row - (self.page_row_max - 1)

    end

    x = index % @column_max * 320

    y = index / @column_max * 32 - self.oy

    self.cursor_rect.set(x, y, 288, 32)

  end

  

end

#------------------------------------------------------------------------------

class Scene_Forge

  

  def main

    @components = Window_Forge_Components.new

    @result = Window_Forge_Result.new

    @item_window = Window_Forge_Item.new

    @help_window = Window_Help.new

    @help_window.set_text("Item Forgeing System", 1)

    Graphics.transition

    loop do

      Input.update

      Graphics.update

      update

      if $scene != self

        break

      end

    end

    Graphics.freeze

    @components.dispose

    @result.dispose

    @item_window.dispose

    @help_window.dispose

  end

  

  def update

    @item_window.update

    @components.update    

    if @components.active

      update_comp

      return

    end

    if @item_window.active

      update_item

      return

    end

  end

  

  def update_comp

    if Input.trigger?(Input::A)

      rock_baby

      return

    end

    if Input.trigger?(Input::C)

      $game_system.se_play($data_system.decision_se)

      @components.active = false

      @item_window.active = true

      @item_window.index = 0

    end

    if Input.trigger?(Input::B)

      for i in 0..5

        item = @components.data[i]

        if item != nil

          case item

          when RPG::Item

            $game_party.gain_item(item.id, 1)

          when RPG::Weapon

            $game_party.gain_weapon(item.id, 1)

          when RPG::Armor

            $game_party.gain_armor(item.id, 1)

          end

        end

      end          

      $game_system.se_play($data_system.cancel_se)

      $scene = Scene_Map.new

    end

  end

  

  def update_item

    if Input.trigger?(Input::C)

      $game_system.se_play($data_system.decision_se)

      component = @components.data[@components.index]

      if component != nil

        case component

        when RPG::Item

          $game_party.gain_item(component.id, 1)

        when RPG::Weapon

          $game_party.gain_weapon(component.id, 1)

        when RPG::Armor

          $game_party.gain_armor(component.id, 1)

        end

      end

      item = @item_window.item

      @components.data[@components.index] = item

      case item

      when RPG::Item

        $game_party.lose_item(item.id, 1)

      when RPG::Weapon

        $game_party.lose_weapon(item.id, 1)

      when RPG::Armor

        $game_party.lose_armor(item.id, 1)

      end

      @item_window.refresh

      @components.refresh

      check_recipes

      

      @item_window.active = false

      @item_window.index = -1

      @components.active = true

    end

    if Input.trigger?(Input::B)

      $game_system.se_play($data_system.cancel_se)

      @item_window.active = false

      @item_window.index = -1

      @components.active = true

    end

  end

  

  def check_recipes

    things_in_the_pot = []

    for i in 0..5

      case @components.data[i]

      when nil

        things_in_the_pot << "z"

      when RPG::Item

        things_in_the_pot << "i#{@components.data[i].id}"

      when RPG::Weapon

        things_in_the_pot << "w#{@components.data[i].id}"

      when RPG::Armor

        things_in_the_pot << "a#{@components.data[i].id}"

      end

    end

    

    things_in_the_pot = things_in_the_pot.sort

    for j in 0...RECIPES.size

      temp = RECIPES[j].dup

      temp[6] = nil

      if things_in_the_pot.size == 6

        things_in_the_pot << nil

      else

        things_in_the_pot[6] = nil

      end

      if things_in_the_pot == temp

        @result.make_item(RECIPES[j][6])

        return

      else

        @result.nullify

      end

    end

  end

  

  def rock_baby

    case @result.item

    when nil

      $game_system.se_play($data_system.buzzer_se)

    when RPG::Item

      $game_party.gain_item(@result.item.id, 1)

      $game_variables[101] =+1

      finalize_forgeing

    when RPG::Weapon

      $game_party.gain_weapon(@result.item.id, 1)

      finalize_forgeing

    when RPG::Armor

      $game_party.gain_armor(@result.item.id, 1)

      finalize_forgeing

    end

  end

  

  def finalize_forgeing

    $game_system.se_play($data_system.save_se)

    @components.nullify

    @components.refresh

    @item_window.refresh

    @result.nullify

  end

  

end

I noticed if I added in the " $game_variables[101] =+1" it would set it two '1' as it did in the Herb gathering system. I'm not a scripter but I try to figure things out by looking at the scripts I do have and seeing how they work. Now something with the Item Crafting System is you can make more then one item at a and the user chooses when to close the scene so I would need it to add '1' to the variable '101' each time an item was successfully created.

Hopefully my request isn't that confusing and shouldn't be that hard to do. I'm probably just not writing the " $game_variables[101] =+1" bit right.
 
The correct syntax would be:

$game_variables[101] += 1


Same applies for subtraction, multiplication, etc.: the operator goes in front of the '=' sign.
Also remember to use
Code:
 tags, they make long scripts much easier to read!
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top