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.
 
This line...

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

...tells the game to draw the contents inside of each window, so there are 16 px on the left and another 16 px on the right hand side, the same happens with the upper and lower side of the window, where you can't see any text or icon, they'd be hidden by the window borders. So you gotta leave it as it is or create a transparent window (opacity = 0) of the same size of the larger window and see if it allows you to place the text message like this...

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

you could also use something like...

self.contents.draw_text(0, 0, 50, -4, $nombre_habilidad)

but no one here would ever recommend to use negative values even if they'd work fine.

BTW, don't declare too many $global_variables, they're the worst you can ever see, even if they allow you to get access to multiple things from anywhere. Use as few as possible, maybe you may create between 1 and 2 of them only. If they are used for very similar purposes, merge them, leave only one of them alive and get rid of the other one.
 
Kain Nobel":2x8cvdu6 said:
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.

There is no difference on using 0x0F or 15. Both will do the same thing, as they are both evaluated as the same value.

Your logic missed the boolean operator. It shouldn't be evaluated as:
@passages[event.tile_id] & 0x0f == 0x0f
First, it should be:
@passages[event.tile_id] & 0x0f == 0x0f

The value of "@passages[event.tile_id]" is a number, but on boolean representation it would be composed of four flags:
abcd
Each of them is a direction. I can't guarantee but I believe that a = up, b = right, c = left and d = down.
Anyway, 0x0f is "1111" on boolean. Therefore, all directions checked. When you do the AND operator, it'll check each flag, or bit, and do the AND calculation. AND will only return 1 (aka. true) if both values are 1.
So:
0110 AND
1100 =
0100

What is a mystery is why they're using the AND operator there. Any value you use there "AND" 0x0f will result on the first value, so I guess it could be:
@passages[event.tile_id] == 0x0f
 

Injury

Awesome Bro

Edit:
Well I changed my mind...

I'm going to use battlebacks for NPC dialouge, essentially going into battle with someone to talk with them...and I want to use battlers to represent each person....

-get battler id
-show battler
-position: L,R,C

is there a way to do this with a basic script command? $sprite.battler? something? idk! help me out please!
 
Good Evening!!
I've a couple of questions about the Bitmap class
and the various command that this class use i'm trying to calculate the time of execution of the Bitmap's methods
the ammount of RAM and CPU that the Methods use for example... is there an easy way to do it? like a table where all of this is contained
for example... what's the difference between these two Line of code in RAM and CPU:
Code:
 

@image = Sprite.new

@image.bitmap = RPG::Cache.picture("image.png") #for an image big n*32 width and m*32 height pxl

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

@image = Sprite.new

@image.bitmap = Bitmap.new(n*32,m*32)

for i in 0..n-1

  for j in 0..m-1

    img = get_img(i,j)

    @image.bitmap.blt(i*32,j*32,img,Rect.new(0,0,@image.width,@image.height))

  end

end
I'm sure that the second way to do it have a bigger impact on the CPU usage...
but... if i try to do other action on this Sprites...
Code:
@image.x = @new_x

@image.y = @new_y

@image.zoom_x = @new_zx

@image.zoom_y = @new_zy

@image.mirror = @boolean_mir
assuming the case where the Sprite called @image is taken from a single PNG file
then the case where the Sprite is created with a merge of more pictures.PNG...
There is here a case where there is a lighter use of the CPU and of the RAM?
or it's the same thing?
there may be the case where n and m (width/32 and height/32 of the @image) are >= 100 for example.

I'll explain what i'm doing (so that you may have a better view of the problem)
I'm rewriting the Tilemap Class ('cause all the version i've seen have a lot of lag or glitch)
I've write a script that create a file .PNG (that will be saved in a directory "Temp") of the first layer of the map, then, when you load that particular map, put the .PNG at the same spot of the not-animated tiles of the first layer (the animated one will be print under the .PNG) the problem of this script is that it takes several seconds to create and save the .PNG (for a map 50x50 it takes 6 seconds) storing the previous Temporary maps, so you don't have to create it everytime, just the first time, and eventually again if you delete the file into the "Temp" directory.

So, does the composed Sprite (with the .bitmap.blt() method) takes the same time to be modified that takes the Sprite created with a single image (also if we're talking about a big image, with a lot of tiles merged together) or not?

==================================================================================

I've another question, always regarding the Bitmaps...
talking about the Animated-Autotiles...
is it easyer for the computer to update the animated graphic of a single tile in a bigger Sprite
or to manage several sprites that will be resetted with the new frame?
Code:
@image = Sprite.new

@image = Bitmap.new(n*32,m*32)

#previous code where i create the bitmap... etc...etc...

def update

  n = @image.width/32

  m = @image.height/32

  for i in 0..n-1

    for j in 0..m-1

      for z in 0..2

      if animated #if an animated autotile is detected in i,j,z coords

      new_anim = get_autotile(i,j,z)

      @image.bitmap.blt(i*32,j*32,new_anim,Rect.new(0,0,@image.width,@image.height)

      end

    end

  end

end

 

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

 

@tiles = {}

n = @map_data.xsize

m = @map_data.ysize

for i in 0..n-1

  for j in 0..m-1

    for z in 0..2

      coords = [i,j,z]

      @tiles[coords] = Sprite.new

      @tiles[coords].bitmap = get_autotile(i,j,z) #or the relative Tile, i can't go into the specifics here XD

      @tiles[coords].x = i*32

      @tiles[coords].y = j*32

    end

  end

end

#then, for the method update

def update

  n = @map_data.xsize

  m = @map_data.ysize

  for i in 0..n-1

    for j in 0..m-1

      for z in 0..2

        coords = [i,j,z]

        if animated(coords)

          new_autotile = get_autotile(i,j,z)

          @tiles[coords].bitmap = new_autotile

        end

      end

    end

  end

end

I know for sure that to handle several Sprites is a bigger issue for the program... but i don't know if to edit a sector of a Bitmap (with .bitmap.blt()) is easy like setup a new image for the Bitmap (with .bitmap = var) do someone know the answer?

So... resuming all of this:
a single big bitmap (created with a picture, a file taken from a directory) called single_bitmap
a big bitmap (created merging several images, with the .bitmap.blt() method) called composed_bitmap
When the 2 images has been created... to edit single_bitmap is like edit the composed_bitmap? the RGSS think at the two of them like 2 simple images? the composed_bitmap is processed as a single image or as a composition of several images (also after the creation)?
and... the .bitmap.blt() method has the same CPU and RAM usage of the command .bitmap = var... or the second one is lighter? (delay of bitmap.blt() >> delay of bitmap = var)
is preferred to handle several Sprites, so that you can modify them one by one... or to handle a single composed Sprite that you have to edit?

Obviously if a composed_bitmap for the RGSS is still a group of several bitmap (and not a single image)... then the answer to the second question is immidiate ^_^
Thanks!! I really hope for an answer!!
 
i Hope this wont be considered a Double Post... since long time has passed from my last post (also if it's still the last of the topic)

i was asking you about an algoritm and a solution to a question of mine... i'm trying to make a Method for Bitmap, that draw a Graduated text
Code:
 

  def draw_gradient_text(x,y,w,h,text,color1,color2,align = 0)

    text_bitmap = Bitmap.new(w,h)

    text_bitmap.font.name = self.font.name

    text_bitmap.font.size = self.font.size

    output_bitmap = Bitmap.new(w,h)

    old_color = color1

    for i in 0..h-1

      new_red = (old_color.red*(h-i-1) + color2.red)/(h-i)

      new_green = (old_color.green*(h-i-1) + color2.green)/(h-i)

      new_blue = (old_color.blue*(h-i-1) + color2.blue)/(h-i)

      new_alpha = (old_color.alpha*(h-i-1) + color2.blue)/(h-i)

      old_color = Color.new(new_red,new_green,new_blue,new_alpha)

      text_bitmap.clear

      text_bitmap.font.color = old_color

      text_bitmap.grad_draw_text(0,0,w,h,text,align)

      output_bitmap.blt(0,i,text_bitmap,Rect.new(0,i,w,1))

    end

    self.blt(x,y,output_bitmap,Rect.new(0,0,w,h))

  end

 
this is the solution that i've found... but i consider it a little bit expensive, also not so good to see... extra bitmaps and all the rest...
do someone know a better way? or maybe a way to clean what i've write here?
probably the solution is in the right use of rect... but i don't know how...
thanks!! :)


aaand... how do i write the syntax for this?
I'm the text of a window message
this \gr[1][2]word\gr is graduated
i know that this:
@text.gsub!(/\\[Gg][Rr]\[([0-9]+|#[0-9A-Fa-f]{6,6})\]/) { "\016[#{$1}]" }
will turn the string \gr[n] into \016, and will save as $1 the value of n
but what if i want to put 2 value in the string between []?
what do i have to write? i've searched around for the string syntax without results... and i've no idea how they work...
i made this work only because i've copied from another script... what i want to do is:
\gr[n][n] (where N could be a color code or an integer) and save both the [n] in 2 var called $1 and $2
i know that it's possible, i simply don't know the syntax for this operation... thanks again!! :D
 
Princess Amy":3lpt9frh said:
Am I right in thinking else is Ruby's equivalent of default in switches?

As in:

case variable
when 1
# do this
when 2
# do that
else
# do something completely different
end
Think of the Ruby switch statement as a "when" statement, it's very different to the computer science standard switch statement.

Switches are a safe replacement of the original goto statement, this would be the equivalent switch;

C:
<div class="c" id="{CB}" style="font-family: monospace;"><ol> 

<span style="color: #b1b100;">switch ( variable ) {

<span style="color: #b1b100;">case <span style="color: #cc66cc;">1:

    // Do this

    <span style="color: #000000; font-weight: bold;">break; // <-- Notice this

<span style="color: #b1b100;">case <span style="color: #cc66cc;">2:

    // Do that

    <span style="color: #000000; font-weight: bold;">break; // <-- Notice this

<span style="color: #b1b100;">case <span style="color: #b1b100;">default:

    // Do something completely different

    <span style="color: #000000; font-weight: bold;">break; // <-- Notice this

}

 

However this switch statement is completely different;
C:
<div class="c" id="{CB}" style="font-family: monospace;"><ol> 

<span style="color: #b1b100;">switch ( variable ) {

<span style="color: #b1b100;">case <span style="color: #cc66cc;">1:

    // Do this

<span style="color: #b1b100;">case <span style="color: #cc66cc;">2:

    // Do that

<span style="color: #b1b100;">case <span style="color: #b1b100;">default:

    // Do something completely different

}

 
In that last case, the default will always get called and case 2 is called after case 1 then default is called after that, this can't be done with Ruby, you'd have to do this to get the equivalent for that last C code;
Ruby:
 

case variable

when 1, 2

    # Do this

    if variable == 2

        # Do that

    end

end

# Always do something completely different

 

So Ruby's version of default is whatever comes after the switch statement, to give this the "break" operation you need to check that nothing else will be called;
Ruby:
 

doDefault = true

case variable

when 1, 2

    doDefault = false

    # Do this

    if variable == 2

        # Do that

    end

end

 

if doDefault == true

    # Do something completely different

end

 




(For the old school) This is what the switch statement replaces;
C:
<div class="c" id="{CB}" style="font-family: monospace;"><ol> 

<span style="color: #b1b100;">if ( variable == <span style="color: #cc66cc;">1 ) {

    <span style="color: #b1b100;">goto CASE_1;

} <span style="color: #b1b100;">else <span style="color: #b1b100;">if ( variable == <span style="color: #cc66cc;">2 ) {

    <span style="color: #b1b100;">goto CASE_2;

}

<span style="color: #b1b100;">goto CASE_DEFAULT;

CASE_1:

<span style="color: #b1b100;">goto CASE_END; // <- This would be the "break" keyword

CASE_2:

CASE_DEFAULT:

// Default always called and called when ( variable == 2 )

CASE_END:

 
In fact you'll find that C compilers will complain for when you declare a variable inside a switch statement because it abuses the stack that the goto statement would ruin (You would miss the declaration, this is why C90 and earlier needed you to declare your variables at the top of the stack), you need to make a new block {} within the statement for any variables you declare.

Edit: Turns out Ruby's when statement is weaker than I thought it was, updated the code where I had "when 1, 2 when 2"
 
Bit of a weird one. Probably an obvious answer (hopefully). Wondering if anyone's come across this.

Basically I'm spawning events, using Game_Event.new. I set it up, based on an existing event, and it does appear... but it has no graphic. I can check it's .character_name, and it returns "001-Fighter01", but it's just transparent as an event.

If I open and close the menu, the event appears.

What do I need to update or refresh that I'm missing do you reckon?

Code:
 

class Game_Map

  def add_event(map_id,id,x,y)

    s = 1

    (1..@events.size + 1).each{|s|break unless @events.keys.include?(s)}

    map = load_data(sprintf("Data/Map%03d.rxdata", map_id))

    ev = map.events[id]

    ev.id = s

    @events[s] = Game_Event.new(@map_id, ev)

    p @events[s].character_name

    @events[s].moveto(3, 3)

    return @events[s].id

  end

end

 

Adds an event and returns it's ID.
 
it needs to get added to the list of events in Spriteset_Map. It's an array of Sprite_Character, one for each event on the map, and it only gets modified on map load (thus, showing up when going to and from menu).
 

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