Confused about Update

Hello again, fellas, you guys are quite helpful for my learning, I'm a little slow, but I think I'm getting there.

I have made a script that displays an item icon and the amount received at the players location.

I doubt it's optimized but I'm just trying to get a feel for how things work.

I had thought that update was called all the time, so if I was to move a sprite in the update method it would continue until
I told it to stop, but I can't seem to get my sprite to continually stay at the players x position.

Could you look at this script and offer some advice, please?

Code:
class S_Pickup_Display < Scene_Map
 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#  
#~  SookyLala's item pickup display  ~#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#  
#~             SETTINGS:             ~#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#


  $gold_icon = 262 # Icon Index Number, found in the bottem left corner
                   # when choosing an icon for items/skills
   
                   
  $pickup_sound = 'Audio/SE/Item3'                 
   
   
  def initialize(itemID, itemAmount)
 
   
   
    $itemDisplayTime = 0
 
 
   if itemID == "g"
     
       @itemID = "g"
     
        else
     
      @itemName = $data_items[itemID]
   
   end
 
    @itemAmount = itemAmount
    @xAmount = "x" + itemAmount.to_s
   
   
    start
   
   
    Audio.se_play($pickup_sound ,100,100)
   
   

    end
 
   
    def start
   
    display_item
    give_item
    update

   
   
  end
 
  def display_item
   
   
   
     if @itemID == "g"
       
       itemName = $gold_icon
       
         else
       
       itemName = @itemName.icon_index
       
     end
     
   
     
    rect = Rect.new(itemName % 16 * 24, itemName / 16 * 24, 24, 24)
   
    $itemDisplay = Sprite.new
    $itemDisplay.bitmap = Bitmap.new(rect.width + 80 ,24)
    bitmap = Cache.system("Iconset")
    $itemDisplay.bitmap.draw_text($itemDisplay.bitmap.rect, @xAmount, 1)
   
    $itemDisplay.bitmap.blt(0, 0, bitmap, rect)
   
   
    $itemDisplay.x = $game_player.screen_x - 20
    $itemDisplay.y = $game_player.screen_y - 10
   

    end
 
   
    def give_item
   
    if @itemID == "g"
     
    $game_party.gain_gold(@itemAmount)
   
  else
   
    $game_party.gain_item(@itemName, @itemAmount)
   
    end
 
 
      end
   

 
      def update

      $itemDisplay.x = $game_player.screen_x - 20
     
    end
   

end
First, your script indentation is quite mess. Mind if you clean up it first?

Second, why would you want this?
Code:
class S_Pickup_Display < Scene_Map

Third, You just want a sprite (displaying item gain) to follow the player character, right?
EDIT:

I've changed the indentation, I hope that it's easy enough to read now.

I have also removed "< Scene_Map" I used it as a test, which didn't change anything and I forgot to remove it.

And well, yes I would like it to follow the player, but eventually I would like it to follow the players X and also lower it's Y value to slowly raise up towards the top of the screen until it is disposed.
I'd give you a hint instead for the sprite position. Generally, you might want to make a "formula"
You created position_x formula and position_y formula. For example, position_y would be something like this
Code:
def pos_y
  $game_player.screen_y - @shift_y
end
And in update method, you would make
Code:
def update
  .....
  @shift_y += 1
  sprite.y = pos_y
end
So every frame, you add 1 to variable @shift_y, so that the sprite would move upwards every frame while still following the player character
@TheoAllen

Well that I understand, but the update method seems to only call once and that's it, I added a msgbox_p command in there and it only shows once.
If you just want to know how to call a method consecutively on update method, then try this and set console ON
Code:
class Scene_Map
  alias old_update update
  def update
    old_update
    puts "Hello World"
  end
end
You probably don't know how to use "alias" I guess?

Well, no, I haven't used it before, but I do know that alias uses the old method as well as the method you have made without overwriting the original method.

Are you saying I would need to alias the Scene_Maps update to update the position of the sprite rather then using a new update method inside of the"S_Item_Display" class?
Are you saying I would need to alias the Scene_Maps update to update the position of the sprite rather then using a new update method inside of the"S_Item_Display" class?
More or less, yes

I don't know what makes you write the script they way you did, but if it were me, I'd write it like this
Code:
class ItemDisplay
   # basically stuff here 
   def update
     ........ (this will be update method for sprite position)
   end
end

class Scene_Map
  alias old_update update
  def update
   old_update
   @item_display.update
  end
end
@item_display would be the instance variable of ItemDisplay class
Thank you for sticking with me and explaining it, I'm really quite new at scripting, I'm not the fastest learner but I do learn.

when you say @item_display.update, that is the instance variable I call in a script call right? So when I want to add a new item this way I would write; @item_display = S_Item_Display.new(1, 1), that is my instance variable?

So when I add @item_display.update to the aliased method, it returns an error that says 'undefined method for update'. I assume that is because @item_display isn't created until I call the script? but I don't run the script all the time?
when you say @item_display.update, that is the instance variable I call in a script call right? So when I want to add a new item this way I would write; @item_display = S_Item_Display.new(1, 1), that is my instance variable?
Not script call. It's in start method on scene
Code:
class Scene_Map
  def start
    ......
    @item_display = S_Item_Display.new
  end
end

So when I add @item_display.update to the aliased method, it returns an error that says 'undefined method for update'. I assume that is because @item_display isn't created until I call the script? but I don't run the script all the time?
When you reporting an error, put full error message and better with screenshot
@TheoAllen

I tried this but the global variable $iDisplay gets overwritten constantly.

Code:
  alias old_update update
 
  def update
    old_update
   
        if $iDisplay != nil
         
          $iDisplay.update

        end


I'm so confused.

if I add @iDisplay.new to Oldupdate I have to also send arguments through (itemID, itemAmount) which gives me the item as soon as the map updates.

if I don't add @iDisplay.new to the Oldupdate method and I try to do @iDisplay.update it returns the error that @iDisplay.update method doesn't exist, because obviously it's not created yet.

if I do that method with the global variable [if $iDisplay != nil $iDisplay.update end]
it sort of works, but if I pick up another item quickly after, $iDisplay is overwritten and the first item picture stops getting updated.

if I use @iDisplay instead of $iDisplay, nothing happens except the original script without the aliased update.
Not related to the actual discussion. But what text editor you use that your code indentation has so much messed format and indentation?
Instead of doing this
Code:
  alias old_update update

  def update
   old_update
   
       if $iDisplay != nil
       
         $iDisplay.update

       end
why not this?
Code:
  alias old_update update
  def update
   old_update
   if $iDisplay != nil
      $iDisplay.update
   end
What makes you write the way you do? It's hard to read, you know

---------
As for in topic discussion. You're trying to do many things you don't know (you don't even know when or why you would use $ or @, or even use "< Scene_Map", which is kinda ridiculous). Why not make it step by step learning?
Do you manage to display a sprite above player (without displaying item gain first)? That would be your first step.

Since my hint doesn't even hit the mark, I'm gonna give you the example code instead.
Spoiler
Code:
class PlayerName < Sprite
 
  def initialize
    super
    self.bitmap = Bitmap.new(130,24)
    self.bitmap.fill_rect(bitmap.rect, Color.new(0,0,0,120))
    self.bitmap.draw_text(bitmap.rect, "This is player")
    self.z = 100
  end
 
  def pos_x
    $game_player.screen_x
  end
 
  def pos_y
    $game_player.screen_y - 32
  end
 
  def update
    super
    self.x = pos_x
    self.y = pos_y
  end
 
end

class Scene_Map
 
  alias old_start start
  def start
    old_start
    @name = PlayerName.new
  end
 
  alias old_update update
  def update
    old_update
    @name.update
  end
 
end
Try this, and forget your script for a moment
If you can explain every single line, and why I structured it like this, we can proceed on to next step
Thank you for your assistance thus far.

As for why I indent the code like that, I had thought it was easier to read, but I will set it out more like the way you had it to make it better.

I had tried to use < Scene_Map because I too also thought that because my new script was being drawn on the map that if I inherited from it
this new script could also use the update feature, but I couldn't get it to work.

$ = A global variable that can be accessed from all scripts
@ = An instance variable that exists in instance of an object.

That script I wrote does manage to show the sprite, I thought I was close because everything was working bar the updating.

Code:
class PlayerName < Sprite
 
  def initialize # this runs as soon as this script is called
    super        # This calls 'Sprites' initialize method
    self.bitmap = Bitmap.new(130,24) # This creates a new Bitmap for the PlayerName class
    self.bitmap.fill_rect(bitmap.rect, Color.new(0,0,0,120)) # This changes the bitmaps color
    self.bitmap.draw_text(bitmap.rect, "This is player") # This Draws text onto said bitmap
    self.z = 100 # This determines the position of the bitmap relative to layer priority
  end
 
  def pos_x
    $game_player.screen_x # This returns the players x position
  end
 
  def pos_y
    $game_player.screen_y - 32 # This returns the players y position - 32 pixels
  end
 
  def update
    super
    self.x = pos_x # This updates the bitmaps position by calling the pos_x method
    self.y = pos_y # This updates the bitmaps position by calling the pos_y method
  end
 
end

class Scene_Map
 
  alias old_start start # This aliases the start method so the original isn't overwritten, for compatibility
  def start
    old_start # This calls Scene_Maps original start method
    @name = PlayerName.new # This sets the the variable to a new instance of the PlayerName script
  end
 
  alias old_update update
  def update
    old_update
    @name.update # This updates the recently created instance of PlayerName
  end
 
end

I understand what those things do, I'm having trouble wrapping my head around my original question.
I had tried to use < Scene_Map because I too also thought that because my new script was being drawn on the map that if I inherited from it
this new script could also use the update feature, but I couldn't get it to work.
You have the reason, but this is not the case. You will break things a lot if you do it including why it could not work.
All the default scripts refers to "Scene_Map", not the child of the class you newly created it, so you better just modify the Scene_Map instead.

$ = A global variable that can be accessed from all scripts
@ = An instance variable that exists in instance of an object.
True, and generally just use @ for variable. You've used too much $ global variable you don't even seem to need.
Use global variable when it's really need to be accessable across all classes such as $data_item or $game_player

You seem to know how my code work. So here is the question
Say, I have a script call (in event) to change the PlayerName text, like this
Code:
change_text("Ralph")
The empty script call method
Code:
class Game_Interpreter
 
  def change_text(text)
   
  end
 
end
How would you change the code so that the "This is player" text would change to "Ralph"?
@TheoAllen

I'm embarrassed to say, but I don't know.

Game_Interpreter can't access @Name. How do I reference the already created new instance of PlayerName if I can't call @Name?
Right, it looks hard isn't. You can try to modify existing class to store the text, for example
Code:
class Game_Temp
  attr_accessor :player_text
 
  alias old_init initialize
  def initialize
    old_init
    @player_text = ''
  end
 
end
And we modify the interpreter
Code:
class Game_Interpreter
 
  def change_text(text)
    $game_temp.player_text = text
  end
 
end
Then we put it on the update method
Code:
  def update
    super
    self.x = pos_x
    self.y = pos_y
    unless $game_temp.player_text.empty?
      self.bitmap.clear
      self.bitmap.fill_rect(bitmap.rect, Color.new(0,0,0,120))
      self.bitmap.draw_text(bitmap.rect, $game_temp.player_text)
      $game_temp.player_text = ''
    end
  end
So every time the $game_temp.player_text is not empty, it will refresh on whatever inside it
@TheoAllen

I feel like you've taught me something profound, something that's making me look at this script differently. It's getting late, but I'm excited to
practice more tomorrow, thank you very much, I appreciate the constant help you've given me, I'll need time to process how to use this information.

You're a good teacher.
Next time when u want to do stuff especially on the field you are not familiar with. Break it into smaller part like this. It might be derailed from your original question, but you can't just go proceed on to larger module if you don't even know the small part of it especially in scripting.

Next thing to do would be changing the text based on latest item gained.
So it will change to something like "Potion +1" every time you use change item on event
Do you think you can do that?
@TheoAllen
Well. I'm not sure if this is what you expect, but using what you did before, I came up with this.
This will change the text every time the "Change Items' event command is used.


I changed the Game_Temp

Code:
class Game_Temp
  attr_accessor :item
  attr_accessor :amount
 
  alias old_init initialize
  def initialize
    old_init
    @item = ""
    @amount = ""
 
  end
 
end

Then I changed Game_Interpreter

Code:
class Game_Interpreter
 
  def change_text
    
    if $game_party.item == nil
       $game_temp.item = ""
    else   
       $game_temp.item = $game_party.item.name
    end

 
    if $game_party.amount <= 0
      $game_temp.amount = ""
    else
      $game_temp.amount = $game_party.amount.to_s
    end
    
  end
 
end

I changed Game_Party to save the variables so I can use them.

Code:
class Game_Party < Game_Unit
  attr_accessor :item
  attr_accessor :amount
 
  alias old_gain_item gain_item
 
  def gain_item(item, amount, include_equip = false)
    old_gain_item(item, amount, include_equip = false)
    @item = item
    @amount = amount
  end
 
end

And then, I changed the Update Method

Code:
  def update
    super
    self.x = pos_x
    self.y = pos_y
    unless $game_temp.item.empty? && $game_temp.amount.empty?
      self.bitmap.clear
      self.bitmap.fill_rect(bitmap.rect, Color.new(0,0,0,120))
      self.bitmap.draw_text(bitmap.rect, $game_temp.item + " " + $game_temp.amount)
    end
  end

I also changed Scene_Map so that it's called automatically and I don't have to call it in the script every item change

Code:
class Scene_Map
 
  alias old_start start 
  def start
    old_start # This calls Scene_Maps original start method
    @interp = Game_Interpreter.new
    @name = PlayerName.new
  end
 
  alias old_update update
  def update
    old_update
    @name.update 
    @interp.change_text
  end
 
end
Good, but if you do it your way, you don't need Game_Interpreter. You can just simply reuse $game_temp without creating a new instance variable for Game_Interpreter, because it's unnecessary

Think of this
Code:
class Scene_Map

  alias old_start start
  def start
   old_start # This calls Scene_Maps original start method
   @name = PlayerName.new
  end

  alias old_update update
  def update
   old_update
   $game_temp.change_text
   @name.update
  end

end
Get the hint?

Original thread: https://forums.rpgmakerweb.com/threads/confused-about-update.92615/