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.

Special Support - A great place to learn to Script

Everyone wants to learn to script, but no one really knows where to start. In my honest opinion, the best place to learn is just by reading the default scripts. I learned to script with the japanese RMXP, so I didn't have English comments at my disposal, so everyone of the new generation has that upperhand. Anyways, what I think the best thing to do to learn to script is read the default scripts, and understand each line.

So what I am offering here is support for anyone trying to learn to script. If there is a line of code you don't understand, ask me here. If there is something you want to know, like where in the script editor does something happen, ask here.
  • Ask about a certain line of code : What it means, does, etc.
  • Ask where the coding is for a certain function
PLEASE DO NOT ASK SOMETHING OTHER THAN EXISTING CODE, OR WHERE IN THE DEFAULT SCRIPTS TO FIND A CERTAIN BLOCK OF CODE. Your post will be deleted.

This is a Trial and Error topic. Hopefully, it can lead to more a use full FAQ for beginners.
 

NL124

Member

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.

Code:
def set_attack_damage_value(attacker)

    case DAMAGE_ALGORITHM_TYPE

    when 0

      atk = [(attacker.atk - self.pdef) / 2, 0].max

      str = [2.5 * attacker.str, 0].max

    when 1

      atk = [attacker.atk - ((attacker.atk * self.pdef) / 1000), 0].max

      str = [20 + attacker.str, 0].max

    when 2

      atk = 20

      str = [(attacker.str * 4) - (self.dex * 2) , 0].max

    when 3

      atk = [(10 + attacker.atk) - (self.pdef / 2), 0].max

      str = [(20 + attacker.str) - (self.dex / 2), 0].max

    end

    self.damage = atk + str

    self.damage = 1 if self.damage == 0 and (rand(100) > 40)

    self.damage *= elements_correct(attacker.element_set)

    self.damage /= 100

  end
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...
 

NL124

Member

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.
 
In the "Main" script there is this block:

Code:
  while $scene != nil

    $scene.main

  end

That is, the "main" method will always be called, unless $scene = nil (i.e. the game is exited)
 
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?


Code:
class Game_Variables

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

  # * Object Initialization

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

  def initialize

    @data = []

  end

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

  # * Get Variable

  #     variable_id : variable ID

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

  def [](variable_id)

    if @data[variable_id] == nil

      return 0

    else

      return @data[variable_id]

    end

  end

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

  # * Set Variable

  #     variable_id : variable ID

  #     value       : the variable's value

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

  def []=(variable_id, value)

    if variable_id <= 5000

      @data[variable_id] = value

    end

  end

end

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.
 

Zeriab

Sponsor

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
 
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:

Code:
 

class Game_Map

  alias en_detect_gm_update update

  def update

     en_detect_gm_update

     #do custom stuff

  end

end

 

Code:
 

class Game_Event

   alias en_det_update update

   def update

      #don't bother calling the original just do our won stuff    

   end

 

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...
Code:
self.contents.draw_text(0, 0, 32, 32, $var)
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:
Code:
super(120, 425, 50, 50)
Anyway, this is what I get with this code:
Code:
class Ventanhabilidad < Window_Base

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

  # * Creando ventanica fondo de habilidades

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

  def initialize

    super($posx, 425, 50, 50)

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

    self.back_opacity = 100

    refresh

  end

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

  # * Refrescamos

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

  def refresh

    self.contents.clear    

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

    self.contents.font.size = 10

    self.contents.draw_text(0, 0, 50, 8, $nombre_habilidad)

  end

end

The scene:
wtf.jpg

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?
 

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