RPG Maker XP without Error on str increase with HUD

Started by Memor-X, May 08, 2013, 11:26:39 pm

Previous topic - Next topic

Memor-X

May 08, 2013, 11:26:39 pm Last Edit: May 08, 2013, 11:28:05 pm by Memor-X
i've been trying to fix up the hud for the XGrid for my game for a while, the part i'm trying to fix up now is setting up a hud which displays the player's stats, this is the script


# Xgrid Class (Chaos-Project Stripped)
class XGrid
 
 attr_accessor     :hud
 
 # inialized values
 def initialize

   @hud = nil

 end
end
 

class XGrid_Char < Sprite
 
 SYSTEM_COLOR  = Color.new(192, 224, 255)
 NORMAL_COLOR  = Color.new(255, 255, 255)
 CLEAR         = Color.new(0, 0, 0, 96)
 VIEWPORT_Z    = 10000
 
 # Defines what types of info will be drawn
 TIME         = true
 
 def initialize
   
   viewport = Viewport.new(0, 0, 640, 480)
   viewport.z = VIEWPORT_Z
   super(viewport)
   
   # Initialize the words used for each value.
   self.bitmap = Bitmap.new(640, 480)
   self.bitmap.font.name = 'Arial'
   self.bitmap.font.size = 14
   self.bitmap.font.bold = true
   
   @refresh = [TIME]
   @data = []
   @data[0] = time_info           if @refresh[0]
   
   # Draw all the items.
   @data.each_index {|i|
     if @data[i] != nil
       draw_items(Array.new(@data[i][0].size) {|j| j }, i)
     end
   }
   # Self updating thread.
   @thread = Thread.new { loop { $xgrid.hud.update if $xgrid.hud != nil } }
 end
 
 def dispose
   # Terminate the update thread when the sprite is disposed.
   @thread.kill
   @thread = nil
   super
 end
 
 def get_info(index)
   return case index
   when 0 then time_info
   end
 end
 
 def update
   # Check for change in values. Re-draws the lines that have changed.
   @refresh.each_index {|i|
     if @refresh[i]
       info = get_info(i)
       if @data[i][1] != info[1]
         draw_items(get_indexes(@data[i][1], info[1]), i)
         draw_items(get_indexes(@data[i][2], info[2]), i)
         #draw_items(get_indexes(@data[i][3], info[3]), i)
         #draw_items(get_indexes(@data[i][4], info[4]), i)
         #draw_items(get_indexes(@data[i][5], info[5]), i)
         @data[i] = info
       end
     end
   }
   # Dispose debugger and terminate thread if not Scene_Map
   unless $scene.is_a?(Scene_Map)
     $xgrid.hud.dispose
   end
 end
 
 def draw_items(indexes, group)
   array = case group
   when 0 then [5, 5, 128]
   #when 0 then [16, 304, 128]
   #when 1 then [160, 424, 128]
   #when 2 then [160+128, 424, 128]
   #when 3 then [160, 424, 128]
   #when 4 then [160, 424, 128]
   #when 5 then [160, 424, 128]
   else
     [304+((group-2)*112), 412, 96]
   end
   
   self.bitmap.fill_rect(0, 0, 350, 95, CLEAR)
   indexes.each {|index|
     # Create a rect at the specific line.
     rect = Rect.new(array[0], (index*12)+array[1], array[2], 12)
     # Draw line number INDEX.
     
     self.bitmap.font.color = SYSTEM_COLOR
     self.bitmap.draw_text(rect, @data[group][0][index])
     self.bitmap.font.color = NORMAL_COLOR
     self.bitmap.draw_text(5, (index*12)+5, 100, 12, @data[group][1][index], 2)
#      self.bitmap.draw_text(40, (index*12)+5, 128, 12, @data[group][2][index], 2)
#     self.bitmap.draw_text(100, (index*12)+5, 128, 12, @data[group][3][index], 2)
 #    self.bitmap.draw_text(165, (index*12)+5, 128, 12, @data[group][4][index], 2)
  #   self.bitmap.draw_text(215, (index*12)+5, 128, 12, @data[group][5][index], 2)
   }
   
 end
 
 def get_indexes(array1, array2)
   indexes = []
   array1.each_index {|i| indexes.push(i) if array1[i] != array2[i] }
   return indexes
 end
 
 def time_info
   # Return strings releated to the time.
   return [
   ['  Character Stats', 'HP ', 'MP ', 'STR ', 'DEX ', 'AGI ', 'INT '],
   ['', $game_actors[1].maxhp.to_s, $game_actors[1].maxsp.to_s, $game_actors[1].str.to_s, $game_actors[1].dex.to_s, $game_actors[1].agi.to_s, $game_actors[1].int.to_s]
   
   ]
 end
 
end


and this is the Call Script to make the HUD Appear (use in an event)

$xgrid = XGrid.new
$xgrid.hud = XGrid_Char.new


the purpose of the HUD is to show Actor 1's stats and update when it's changed so i have an event set up with this Call Script

$game_actors[1].str += 1


but when i tigger the event RPG Maker XP shuts down and i don't get an error, if i trigger the second event to increase str it doesn't crash and the Actor's str stat increases

can anyone help me out with this

NOTE: yes, the XGrid_Char Class is just a copy of ForeverZer0's CCTS's debug script found in the demo of the script with some chnages to change where it's positioned and the content

EDIT: wow, the Orange Juice i am drinking must have drugs in it cause the code looks funky, i like it, too bad the preview when making tyhe post didn't do this

KK20

Syntax highlighting has been around for a bit now. Have you really been gone for that long? :P

The problem is with the Thread you created. I haven't worked with those before, as I have no practical reason why I should yet, so I can't explain it in detail. Basically all I can suggest is that you call the update method elsewhere, like in Scene_Map.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Memor-X

Quote from: KK20 on May 09, 2013, 12:10:00 am
Syntax highlighting has been around for a bit now. Have you really been gone for that long? :P


yes i have

Quote from: KK20 on May 09, 2013, 12:10:00 am
The problem is with the Thread you created. I haven't worked with those before, as I have no practical reason why I should yet, so I can't explain it in detail. Basically all I can suggest is that you call the update method elsewhere, like in Scene_Map.


:facepalm: :facepalm: thank you, it seems that while the debug windows are in CCTS the extra script ForeverZer0 made to allow them to be displayed when you press shift is in the Scene_Map Class......might explain why when i go into the game menu is doesn't dispose when the logic is there

KK20

I also found out about this

if @data[i][1] != info[1]
         draw_items(get_indexes(@data[i][1], info[1]), i)
         draw_items(get_indexes(@data[i][2], info[2]), i)
...

@data[i][2] and info[2] are returning nil. So when you call 'get_indexes', you are attempting to call nil.each_index, which should throw the error 'undefined method each_index for Nil class'. And I found this online
QuoteIf an exception is raised in the main thread, and is not handled anywhere, the Ruby interpreter prints a message and exits. In threads other than the main thread, unhandled exceptions cause the thread to stop running.

So that's probably why it's quitting without an error message.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Memor-X

Quote from: KK20 on May 09, 2013, 12:32:36 am

@data[i][2] and info[2] are returning nil. So when you call 'get_indexes', you are attempting to call nil.each_index, which should throw the error 'undefined method each_index for Nil class'. And I found this online
QuoteIf an exception is raised in the main thread, and is not handled anywhere, the Ruby interpreter prints a message and exits. In threads other than the main thread, unhandled exceptions cause the thread to stop running.

So that's probably why it's quitting without an error message.


yeh, that's fixed things, the STR display is now workingbeen working with xpath's at work and when looking at this code i forgot than the array started at 0 not 1 and that @data[i][0] and info[0] aren't included in the update as they are normally just headings

now everything else disappears when i trigger the second event, i think i know why, Zer0 had the initialization done in Scene_Map's update and looking at the rest of the XGrid code which i was working on months ago (before C.C's k key f*#$@#& up) i had members set up to do the exact same but forgot their purpose

kinda annoying that it doesn't give me an error, would have seen this sooner had i got something like "undefined function each_index for nil object"