Special Support - A great place to learn to Script

I ran into some trouble adding some debugging to equipping items onto actors within the default scripts, which has led me to some confusion on Events.

I was attempting to just go through some of the code and put in some prints to see how it functions when I ran into what appeared to be odd behavior from my perspective. Here's the code I changed in Game_Actor.

  1.  
  2.   def equip(equip_type, id)
  3.      case equip_type
  4.      when 0   # Weapon
  5.         if id == 0 or $game_party.weapon_number(id) > 0
  6.            $game_party.gain_weapon(@weapon_id, 1)
  7.            # TEST DEBUGGING
  8.            print "New Weapon ID: %d" %id
  9.            print "Old Weapon ID: %d" %@weapon_id
  10.            # END TESTING
  11.            @weapon_id = id
  12.            $game_party.lose_weapon(id, 1)
  13.         end
  14.  
Expand to see the code.


The problem is, it keeps going and going and going. It will say "New Weapon ID: 1" "Old Weapon ID: 2" "New Weapon ID: 2" "Old Weapon ID: 1" over and over and over, never stopping. I have to Alt+F4 it. I tracked this back all the way to Scene_Equip line 20: def main. It looks like it is just an infinite loop in which it refreshes the page forever. That means it constantly calls equip repeatedly (@actor.equip line 106 of Scene_Equip). That would explain the behavior I saw when trying to watch the code quoted above.

So, my question is why is it done this way? I would have thought that it would have waited for a user fired Event based on a keypress at which time it would run Game_Actor.equip. Ruby is an O-O language, and the likes of Observer Patters and Events are fairly ubiquitous (I think, at least). Is this a programming choice? Is there a reason for it? Is this a Ruby thing? Is this what I can expect throughout the default scripts?
You are correct. They are calling 'refresh' with every update. It should only be called when the individual windows need to be refreshed.

Try changing the update method in Scene_Equip to

  1.  
  2.   def update
  3.     # Update windows
  4.     @left_window.update
  5.     @right_window.update
  6.     @item_window.update
  7.     ## Only refresh when the right window cursor (index) moves
  8.     if @equip_index != @right_window.index
  9.       refresh
  10.       @equip_index = @right_window.index
  11.     end
  12.     # If right window is active: call update_right
  13.     if @right_window.active
  14.       update_right
  15.       return
  16.     end
  17.     # If item window is active: call update_item
  18.     if @item_window.active
  19.       update_item
  20.       return
  21.     end
  22.   end
  23.  
Expand to see the code.
I just need feedback on this script I started working on. Its an Item Rarity script.

Image

I need feedback on it and anything I could possible do to it.
Quote:
I just need feedback on this script I started working on.

You missed the point of this topic. No feedback is given here.
If you need help with the default scripts, this is the place. But don't even bother posting here if you want people to download and comment your stuff.


SEE YA!!!!!
Curious, why might be I getting this error

Script 'Window_Base' line 172: NoMethodError occured.
undefined method 'name' for nil:NilClass

for this line in the window_base class.

self.draw_shadow(x, y, 120, 32, actor.name)

I don't get why it's not working personally cause actor.name is a variable of the Game_Actor class and actor is being sent to it but ya..

Trying to basically recreate the menu system so it's not making a new scene for the options of the menus (Item works fine but status/equip/skills of course require the send of actors so it must be something with that). If you need me to post more code just let me know.

Edit: nvm, I figured it out XD lol took me a bit though
I have a cms in my game where the faceset shown relies on actor id
however in my game I have character actor's graphic changing often
thus this system doesn't work for me,
rather I want it to rely on character graphic (the walking one seen on the map) filename
but I don't know which variable to use...

it uses @actor to determine which faceset to use and
@actor = actor

Which variable should I use instead of actor
(which according to Game_Actor refers to the actor id)
so that it refers to character graphic filename?
Hello :)

Wow, I haven't had to ask anything in a long time, but I was recently messing and experimenting with $game_map.passable? method and I've always been curious what this value is exactly and how it works, why its used, etc...

0x0f

When I use 'p' or 'print' to view the value, it says '15' so... I don't understand why that number? I mean, passability is based off of 8 directions, right? So what does the 0x0f or 15 come from?

When I look at this line, it confuses me what its purpose is...

# If obstacle bit is set in all directions
elsif @passages[event.tile_id] & 0x0f == 0x0f


That basically translates to me like...

# If obstacle bit is set in all directions
elsif @passages[event.tile_id] & 15 == 15


0x0f == 0x0f and 15 == 15 will always return true, right? Or does this 'bianary' number change or what? Thanks ahead of time for anybody who can clear that up for me.

@Velocir_X : This topic is for general Ruby/RGSS and RGSS2 questions, ie questions about default scripts and not custom scripts, if I'm not mistaken. Unless you're talking about VX, you might want to make your own support topic with the code for the CMS and especially the parts that you made to deal with facesets. If I have time I'll drop in and try to help you out.

If you want, I have a scriptlette (which needs to be updated) from my CMS which I use to handle face graphics, with a settings module so you can define things like what to look for in the filename when referencing a faceset.

Most people who do their own 'faceset' script for XP would reference it like @actor.character_name + "_face", ie Fighter 01's faceset would be "001-Fighter01_face" for the filename. If it doesn't exist, then use a rescue statement so it doesn't crash the game... please make topic, I'll try to help you more if I have time today.
Quote:
@Velocir_X : This topic is for general Ruby/RGSS and RGSS2 questions, ie questions about default scripts and not custom scripts, if I'm not mistaken. Unless you're talking about VX, you might want to make your own support topic with the code for the CMS and especially the parts that you made to deal with facesets. If I have time I'll drop in and try to help you out.

If you want, I have a scriptlette (which needs to be updated) from my CMS which I use to handle face graphics, with a settings module so you can define things like what to look for in the filename when referencing a faceset.

Most people who do their own 'faceset' script for XP would reference it like @actor.character_name + "_face", ie Fighter 01's faceset would be "001-Fighter01_face" for the filename. If it doesn't exist, then use a rescue statement so it doesn't crash the game... please make topic, I'll try to help you more if I have time today.

Actually it is a custom script - Caldaron's Animated 1 Hero CMS
viewtopic.php?t=10326.msg111971

And I tried actor.character_name, it doesn't work
I get a random 10 digit variable name that is required ?????

The part of the script in question
class Menu_Graphic < Window_Base
#--------------------------------------------------------------------------
def initialize(actor)
@actor = actor
super(240, -256, 160, 224, 128)
self.contents = Bitmap.new(width - 32, height - 32)
refresh
end
#--------------------------------------------------------------------------
def refresh
self.contents.clear
draw_actor_menu_graphic(@actor, 64, 96)
end
#--------------------------------------------------------------------------
end

I would of put this on the cms's thread but that would be necroposting :(

EDIT: Nvm I was looking at the wrong place should of looked at the draw_actor_menu_graphic method :P
I'm having a problem editing original RGSS2:

  1. #--------------------------------------------------------------------------
  2.   # * Get Character Graphic
  3.   #     filename : Filename
  4.   #--------------------------------------------------------------------------
  5.   def self.character(filename)
  6.     load_bitmap("Graphics/Characters/", filename)
  7.   end
Expand to see the code.


When I add 'hue' at the end of the array, the hue bar does not show up in the editor... Why is that? The commands above it has the 'hue' in their arrays above this segment... so I thought maybe the hue bar would show up, allowing me to edit hue values...
quote Spoiler
Kain Nobel wrote:
Hello :)

Wow, I haven't had to ask anything in a long time, but I was recently messing and experimenting with $game_map.passable? method and I've always been curious what this value is exactly and how it works, why its used, etc...

0x0f

When I use 'p' or 'print' to view the value, it says '15' so... I don't understand why that number? I mean, passability is based off of 8 directions, right? So what does the 0x0f or 15 come from?

When I look at this line, it confuses me what its purpose is...

# If obstacle bit is set in all directions
elsif @passages[event.tile_id] & 0x0f == 0x0f


That basically translates to me like...

# If obstacle bit is set in all directions
elsif @passages[event.tile_id] & 15 == 15


0x0f == 0x0f and 15 == 15 will always return true, right? Or does this 'bianary' number change or what? Thanks ahead of time for anybody who can clear that up for me.


I can't answer fully, but I can help some and maybe you can figure it out from there/someone else can help with the rest. Anyway.

0xNUMBER means it's a hexadecimal number. 15 in hex is F. It's written with the 0x in front so it can be differentiated from decimal numbers (0F is clearly a hex number but, for example, 0x300 in decimal is 768; if you just wrote 300, how would it know which you meant?). I don't know the reason 0x is what's used, but whatever.
And as far as I can tell, for the code you were looking at
  1. # If obstacle bit is set in all directions
  2. elsif @passages[event.tile_id] & 0x0f == 0x0f
Expand to see the code.

the order of operation here is
  1. # If obstacle bit is set in all directions
  2. elsif ((@passages[event.tile_id] & 0x0f) == 0x0f)
Expand to see the code.


However, I don't know how the binary and operator works, so I'm not sure what it's checking. :/

EDIT

I'm thinking... the @passages[event.tile_id] gets the passability for the tile, which is 4 bits for each direction. So if they're all set to 1, that's 1111, which is 0x0F/15 in binary. I'm guessing the "& 0x0F" actually would return the exact same thing as the original number, except converted to hex instead of in decimal or whatever it starts as. I don't see why they would do it that way, but it kinda makes sense.
Note : Using RMXP, if it helps at all...

Currently I'm using a sideview battle system with its own battle calculations, taking on something almost the same as the ones used in game. I've changed the battle calculations accordingly as they don't work when changed in the normal place. My issue lies in that enemies are hitting decimals on me. I've looked for a bit around to find the solution, and the only things I found didn't work.

  1. def set_attack_damage_value(attacker)
  2.     case DAMAGE_ALGORITHM_TYPE
  3.     when 0
  4.       atk = [(attacker.atk - self.pdef) / 2, 0].max
  5.       str = [2.5 * attacker.str, 0].max
  6.     when 1
  7.       atk = [attacker.atk - ((attacker.atk * self.pdef) / 1000), 0].max
  8.       str = [20 + attacker.str, 0].max
  9.     when 2
  10.       atk = 20
  11.       str = [(attacker.str * 4) - (self.dex * 2) , 0].max
  12.     when 3
  13.       atk = [(10 + attacker.atk) - (self.pdef / 2), 0].max
  14.       str = [(20 + attacker.str) - (self.dex / 2), 0].max
  15.     end
  16.     self.damage = atk + str
  17.     self.damage = 1 if self.damage == 0 and (rand(100) > 40)
  18.     self.damage *= elements_correct(attacker.element_set)
  19.     self.damage /= 100
  20.   end
Expand to see the code.

The system uses 1 of 4 different damage algorithms. The one in question is "0".

The issue would be the fact that the attack is added to 2.5x the attacker's str. On even numbers, and the one I used on my characters(101, oddly enough) for testing all work fine and dandy. Unfortunate coincidence, I guess.

I'm unsure exactly how to round the number to an integer, though I've looked around and found a few solutions... But they don't seem to work, regardless of where I place the coding. I'm just learning the scripting thing, and this is something I can't seem to find anywhere. Perhaps I didn't search hard enough, but I've been unable to find what I've needed...

Any help appreciated, and sorry if this is simple, but I'm new to it all and the RMXP help file is sorta confusing...
str = ([2.5 * attacker.str, 0].max).to_i
Ahh, I see. Didn't think about the brackets like that. Much appreciated...

Edit : However, it doesn't seem to have worked. I'll read through the rest of that section and see if I missed anything, which I most likely did. Thank you.

Edit2 : Restarted RMXP(since it seems to help most people's problems from what I've read) and it seems to work now just fine.
What does it mean to define main? Sometimes I see:

  1. def main
Expand to see the code.


but I don't understand why it's used.
In the "Main" script there is this block:

  1.  while $scene != nil
  2.     $scene.main
  3.   end
Expand to see the code.


That is, the "main" method will always be called, unless $scene = nil (i.e. the game is exited)
EDIT: Moved
Hmm... all right, I'm basically trying to explore through Ruby here at this moment. I'm trying to figure out what's what and where's where. Anyhow, I want to slowly develop a battle system. Which is rather complicated, but if I take it slowly, I'll probably at least learn a couple of stuff along the way. I can figure out how to make a menu and stuff, and how to locate it on the screen so that's all right.

Right now, though, I'm trying to focus on the background. How do I make it so that the background changes into a "battleback"? I can't really spot where that line of code is that changes the background. >_< It's somewhere in Scene_Battle1, 2 and 3. Though, I can't find it. I figured it would be like a bitmap image (like the title/game over screen) though, I'm starting to think that isn't the case?

Much appreciated.

EDIT: Oh scratch that. I got it and figured it out. Idk why I didn't click the "Help" section earlier. xD Anyhow, this feels pretty cool. :)
SephirothSpawn please help me to understand this piece of code! Can you help me to understand what is going on here?


  1. class Game_Variables
  2.   #--------------------------------------------------------------------------
  3.   # * Object Initialization
  4.   #--------------------------------------------------------------------------
  5.   def initialize
  6.     @data = []
  7.   end
  8.   #--------------------------------------------------------------------------
  9.   # * Get Variable
  10.   #     variable_id : variable ID
  11.   #--------------------------------------------------------------------------
  12.   def [](variable_id)
  13.     if @data[variable_id] == nil
  14.       return 0
  15.     else
  16.       return @data[variable_id]
  17.     end
  18.   end
  19.   #--------------------------------------------------------------------------
  20.   # * Set Variable
  21.   #     variable_id : variable ID
  22.   #     value       : the variable's value
  23.   #--------------------------------------------------------------------------
  24.   def []=(variable_id, value)
  25.     if variable_id <= 5000
  26.       @data[variable_id] = value
  27.     end
  28.   end
  29. end
Expand to see the code.


I am especially confused with the [] part. I know it's an array, but there is no variable name preceding it.

Besides, what happening with this snippet?
def []=(variable_id, value)

Thank you! :)
Well I'm no Sepiroth Spawn (sadly he is gone) nor am I a Ruby expert in anyway
but imo it looks like the script is leaving an empty array so that parameters can be added later,
including the variable name as the variable is invoked within the editor

...which makes sense as the variable class is a wrapper for the array so that the editor can use t
(something like that :huh: )

so your best bet would be to look for further invocations of the variables/methods in question
I'm not seph, but perhaps I can help.

Game_Variables as a class, is pretty much just a fancy array that stores all the info on the variables. In Scene_Title, after you start a new game, you'll notice it creates an instance of this class:
$game_variables = Game_Variables.new

The function you're referring to allows you to go:
$game_variables[variable_id] = value
or with numbers
$game_variables[5] = 10
which sets variable 5 to 10.
Or in other words, it defines the syntax to create and read arrays within class Game_Variables ;)
Quote:
def []=(variable_id, value)
(...)
end


That is a method declaration. It's a way to add support some syntactic sugar. You can have method calls of this style:
$game_variables[variable_id] = value

Similarly allows the def [](variable) declaration method calls of this style:
$game_variables[variable_id]

*hugs*
In my game there are a few areas where I need to use a different font, and only for numbers. What I'm wondering is, if I don't want the player to have to install a font, which would be less intensive on memory:
drawing rectangles to make these small (7x5px) characters, or using individual bitmaps?
I would use a single bitmap (7 x 50) with all 10 digits, then in the section that draws the numbers use the bitmap "blt" method to draw each digit. For example, if the numbers in the bitmap are 0-9, and you need a 3 at position(120,40)

src_rect = Rect.new(3 * 5, 0, 5, 7) # 3 being the digit you want
self.contents.blt(120, 40, numbers, src_rect) #numbers being the bitmap with the 10 digits
That's what I figured would be the best bet. Thanks for the help Brewmeister. :)
Hi, I'm trying to learn to script rather than blindly copy existing scripts. I've tried searching before asking honest :)

So I was looking at setting up enemies that move around the map and chase the player. I noticed these lines in a script:

  1.  
  2. class Game_Map
  3.   alias en_detect_gm_update update
  4.   def update
  5.      en_detect_gm_update
  6.      #do custom stuff
  7.   end
  8. end
  9.  
Expand to see the code.


  1.  
  2. class Game_Event
  3.    alias en_det_update update
  4.    def update
  5.       #don't bother calling the original just do our won stuff    
  6.    end
  7.  
Expand to see the code.


Now I can see in the first listing that this allows the "custom stuff" to run every frame and is piggy backing onto the Game_Map class and extending the "en_detect_gm_update" function to do it. However, I can't find "en_detect_gm_update" in the default scripts or the help file. Seems like there are functions built-in that aren't explained?

What does "en_detect_gm_update" and "en_det_update" do? And is there a list of all these functions somewhere?
They are defined in the alias statements above the def statements.

en_detect_gm_update calls the original 'update' method from the Game_Map class.
en_det_update calls the original 'update' method from the Game_Event class.

alias nickname name means, essentially....

rename the original method (name) to nickname, and let me define a new 'name' method.
then in the new method, I use the nickname to call the original method with the same name.

make sense?
Hello peoples! This is my first support-requesting post and also my 3rd day with RGSS , so I would appreciate some patience :D
I have some doubts about X and Y in draw_text'ing inside a window. Is it possible to draw text beginning in coords x=0 and y=0 of the window? How can I do it? Writing this do not fix it...
  1. self.contents.draw_text(0, 0, 32, 32, $var)
Expand to see the code.

Thanks for all guys!
Is the window located in the upper left corner or not? Because that text would be drawn just in the upper left corner / side of the same window where you call it... And if the window is not located there...
Do you mean in the upper left corner of the scene? No, the window has this properties:
  1. super(120, 425, 50, 50)
Expand to see the code.

Anyway, this is what I get with this code:
  1. class Ventanhabilidad < Window_Base
  2.   #----------------------------------------------------------------------
  3.   # * Creando ventanica fondo de habilidades
  4.   #----------------------------------------------------------------------
  5.   def initialize
  6.     super($posx, 425, 50, 50)
  7.     self.contents = Bitmap.new(width-32, height-32)
  8.     self.back_opacity = 100
  9.     refresh
  10.   end
  11.   #----------------------------------------------------------------------
  12.   # * Refrescamos
  13.   #----------------------------------------------------------------------
  14.   def refresh
  15.     self.contents.clear    
  16.     self.contents.font.color = text_color(4)
  17.     self.contents.font.size = 10
  18.     self.contents.draw_text(0, 0, 50, 8, $nombre_habilidad)
  19.   end
  20. end
Expand to see the code.


The scene:
Content Hidden Spoiler
Image


And what I want is that the unreadable text appear just in the upper left corner of each small window. Can it be done?
It seems that there is a security area where you cannot write text nor images, isn't it?

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=33070