Quick Assistance with draw_image and $game_variables

Salutations!


To begin with I am just a novice at coding, but have been diligently working on it. Please try to use simple terms.
I need assistance with a bit of coding that is most likely quite simple to fix, but I just can't figure it out after days of searching so I will post what I have and any and all assistance with it would be great.

RPG Maker VX Ace.

What I am trying to do:

  • Create an image that changes based on an in game variable


Spoiler
  def draw_image
    if $game_variables[15] = 1
      bitmap = Bitmap.new("Graphics/Faces/Image0")
      self.contents.blt(x,y,bitmap,Rect.new(0,0,bitmap.width,bitmap.height))
    elsif $game_variables[15] = 1
      bitmap = Bitmap.new("Graphics/Faces/Image1")
      self.contents.blt(x,y,bitmap,Rect.new(0,0,bitmap.width,bitmap.height))
    elsif $game_variables[15] = 2
      bitmap = Bitmap.new("Graphics/Faces/Image2")
      self.contents.blt(x,y,bitmap,Rect.new(0,0,bitmap.width,bitmap.height))
    end
  end



This is the bit of code I have come up with. What happens for me is it draws the image I want if the var = 0, but even upon changing var[15] it stays the same image (even after refresh)

I already have a window which it places itself in fine, disappears when the window does and overall functions exactly as I'd like EXCEPT the image changing depending on a $game_variables[]


If this is in the wrong topic, or more information is needed, please let me know!


Thank you in advance for the help! 


EDIT:

After trying numerous code for the last several months (
Though only days after I posted this), I have made a solution!

Spoiler
#=====================================================================


# Module Piece to edit


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


module Hud_Config


# Picture = "Graphics/Pictures/test.jpg"


  PICTURE = {


    0 => "test0.jpg",


    1 => "test1.jpg",


    2 => "test3.jpg",


    }


# Variable to use to determine the picture to display


  VAR = 1


end


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


# Create the window


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


class Window_Hud < Window_Base


  def initialize


    super(0,0,250,75)


    update_padding


    update_tone


    create_contents


    refresh


  end


  def refresh


    self.contents.clear


    contents.clear


    draw_image


  def draw_image
      bitmap = Cache.picture(Hud_config:😛ICTURE[$game_variables[VAR]])
      self.contents.blt(x,y,bitmap,Rect.new(0,0,bitmap.width,bitmap.height))
  end


end


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


# Scene to keep the HUD on the map


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


class Scene_Map < Scene_Base
  alias start_window start
  alias term_window terminate
  alias update_window update
  
  def start
    start_window
    @winhud = Window_Hud.new
    update
  end
  
  def terminate
    @winhud.dispose
    term_window
  end
  
  def update
    update_window
    @winhud.update
  end
end



I have more code in this for myself, but this is the basics of what I needed to be able to make a window that displayed an image based on a variable. You could probably add more arrays/variables to be able to use it for more than one instance as well, however I only need it for this.

Feel free to use this piece of code and edit it freely for commercial and non-commercial use. If you wanna share it, I'd just appreciate some credit.
I've moved this thread to RGSSx Script Support . Please be sure to post your threads in the correct forum next time. Thank you.
I think the issue with the original code is using a single = symbol in the if lines.


X = 100    #Sets X to 100


X == 100  #Checks if X is 100

Code:
   def draw_image
    if $game_variables[15] == 0
      bitmap = Bitmap.new("Graphics/Faces/Image0")
      self.contents.blt(x,y,bitmap,Rect.new(0,0,bitmap.width,bitmap.height))
    elsif $game_variables[15] == 1
      bitmap = Bitmap.new("Graphics/Faces/Image1")
      self.contents.blt(x,y,bitmap,Rect.new(0,0,bitmap.width,bitmap.height))
    elsif $game_variables[15] == 2
      bitmap = Bitmap.new("Graphics/Faces/Image2")
      self.contents.blt(x,y,bitmap,Rect.new(0,0,bitmap.width,bitmap.height))
    end
  end
There are many issues with the "solution" script...

  1. You forgot to end the refresh method in your window class. You actually typed in the whole code here instead of copy/paste? o.o
  2. This line: 

    bitmap = Cache.picture(Hud_config:😛ICTURE[$game_variables[VAR]])


    Will give an error. It should be:


    bitmap = Cache.picture(Hud_config:😛ICTURE[$game_variables[Hud_config::VAR]])


  3. You don't have to call the update method on windows created in any scene which inherits from Scene_Base. They are updated automatically. If you call update on them, they will be updated twice in each frame.
  4. You don't have to call dispose on windows created in any scene which inherits from Scene_Base either. They are disposed automatically.

Not sure what this script does actually? With just the code you shared, the image shown will not be updated in real time, only when Scene_Map is re-started. If that was the function you wanted, than it works how you want it to work, but if you want real time image changes the moment you change the assigned variable, you will need to make a logical update method for it somewhere in the window's update method.

Original thread: https://forums.rpgmakerweb.com/threads/quick-assistance-with-draw_image-and-game_variables.64269/