Ruby Scripting
#4
Numbers
Those Boring Integers and Floats

Nope, I am not talking about a weird TV show here. Laughing Instead I want to talk about how Ruby deals with all kinds of numbers. First of all, they aren't really some primitive types as in C or C++ and many other languages. They do posses methods and you can use a dot and a method name to get some different value at any time! Shocked 

First of all, you got to know they all do not belong to a Number class, even if that would have been quite useful. Its actual name is the Numeric class. Internally Ruby offers you the Integer which is divided in Fixnum and Bignum without any way to let you ever change that. Still you can add or subtract numbers like you have always done in school. There is Float as well, which include all real numbers, those pesky values like 1.0 or 3.14159 and so on. There is no other number class. You can still enter binary or hexadecimal values but Ruby will internally print an integer instead. If you do not believe me, go try the IRB alias the Ruby Ruby Interactive Shell Console. Laughing + Tongue sticking out 

+ lets you sum up whatever you need to calculate, - is subtraction, * is multiplication and / lets you divide a number by another. % provides you with its modulo value (4 % 3 = 1 as its modulo). You can also combine them with the = equal sign at will. You can compare them with operators (mathematical symbols) like < or > or >= or <= or <=> or == or !=. <=> is the sort operator, while != is the unequal or not equal symbol.

In Ruby you normally get Integers as a result unless both of them are Floats or you explicitly define the last argument as a Float with to_f method. you can convert a Float to an Integer with the to_i method. Actually you can even convert a character like 'A' to an Integer with to_i or hex or even ord depending on your version of the Ruby interpreter.

Even so numbers in Ruby are kind of constant so you can assign 1 to 100 variables that whenever you change the value to one of them, the rest will not get affected at all, not even if you alter the original variable that was storing that number! Shocked 

Hello World!
The Most Boring Way to Start Programming

Yeah, it was inevitable! Laughing We had to come to this point some day and that day or night has finally come! Laughing + Tongue sticking out 

How do you say that in Ruby!?

That is quite easy in Ruby and Cristal, you only need to do this:

puts "Hello world!"

And it will get printed in the Console console or terminal window. Really, there is nothing else you need to do. Well, you can substitute puts with print, but you would need to add another argument, namely the return carriage symbol "\n" or your next line will be printed right at its side without any space in between. puts does that for you from the very beginning. Shocked

How do I do the same in RM series?

You would need to create a Window and its self.contents or a Sprite and its self.bitmap to do that. This is how you do that with Sprites.

Code:
@sprite = Sprite.new
@sprite.bitmap = Bitmap.new(240, 26)
@sprite.bitmap.draw_text(0, 0, 240, 24, "Hello world!")

Here I did not use self.bitmap because I was already using a variable storing that @sprite. It is practically the same with a Window but you usually use one of its subclasses like Window_Base or Window_Command or a custom window class instead. You replace bitmap with contents. draw_text, guess what? It draws text on screen for you! Laughing + Tongue sticking out For this kind of stuff you better use @instance variables, never use local variables that use no symbol prefix because they would disappear right after your method or script ends. They get killed in no time! Shocked Do not make them suffer without need! Laughing

Explanation on Bitmap and Window_Base's draw_text method

Code:
@sprite.bitmap.draw_text(0, 0, 240, 24, "Hello world!")

draw_text(x_coord, y_coord, max_width, max_line_height, text, optional_alignment)

The optional alignment there can be either non explicit which is the same as its default value 0 (left).
1 would mean it will be centered taking in consideration the lines max_width.
2 means its right hand side will end at the max_width.

puts "Happy Non Disposed Bitmaps!"
Laughing
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9

Maranatha!

The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

My Original Stories (available in English and Spanish)

List of Compiled Binary Executables I have published...
HiddenChest & Roole

Give me a free copy of your completed game if you include at least 3 of my scripts! Laughing + Tongue sticking out

Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Reply }


Messages In This Thread
Ruby Scripting - by kyonides - 08-29-2019, 04:51 AM
RE: Ruby Scripting - by kyonides - 08-30-2019, 05:47 AM
RE: Ruby Scripting - by kyonides - 09-03-2019, 07:24 AM
RE: Ruby Scripting - by kyonides - 09-06-2019, 05:46 AM
RE: Ruby Scripting - by kyonides - 09-09-2019, 05:00 AM
RE: Ruby Scripting - by kyonides - 06-05-2021, 08:20 PM
RE: Ruby Scripting - by kyonides - 11-28-2021, 03:31 AM
RE: Ruby Scripting - by kyonides - 05-02-2022, 02:43 AM
RE: Ruby Scripting - by kyonides - 01-27-2023, 08:01 AM
RE: Ruby Scripting - by kyonides - 02-03-2023, 02:42 AM
RE: Ruby Scripting - by DerVVulfman - 02-03-2023, 03:52 AM
RE: Ruby Scripting - by kyonides - 02-03-2023, 04:33 AM
RE: Ruby Scripting - by kyonides - 04-19-2023, 12:47 AM
RE: Ruby Scripting - by kyonides - 04-19-2023, 01:12 AM
RE: Ruby Scripting - by kyonides - 06-08-2023, 09:24 PM
RE: Ruby Scripting - by kyonides - 06-08-2023, 09:46 PM
RE: Ruby Scripting - by kyonides - 06-12-2023, 06:29 AM
RE: Ruby Scripting - by kyonides - 07-14-2023, 10:37 PM
RE: Ruby Scripting - by kyonides - 07-15-2023, 07:32 AM
RE: Ruby Scripting - by kyonides - 07-16-2023, 06:05 AM
RE: Ruby Scripting - by kyonides - 07-17-2023, 04:01 AM
RE: Ruby Scripting - by kyonides - 08-22-2023, 08:59 AM
RE: Ruby Scripting - by kyonides - 08-28-2023, 03:34 AM
RE: Ruby Scripting - by kyonides - 11-18-2023, 09:02 AM
RE: Ruby Scripting - by kyonides - 04-16-2024, 06:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Ruby - Behind the Scenes DerVVulfman 0 1,159 07-15-2023, 05:52 PM
Last Post: DerVVulfman
Information  Assorted Ruby chm documents. hanetzer 10 35,449 08-17-2020, 04:19 AM
Last Post: kyonides
Brick  Learn Ruby! greenraven 2 7,651 05-16-2014, 12:25 PM
Last Post: greenraven
   Creating Your Own Scripting System DerVVulfman 3 7,507 10-12-2009, 03:37 PM
Last Post: Alpha-Mad
   How to make interesting NPC's without scripting Third333Strike 0 3,857 12-06-2008, 04:59 PM
Last Post: Third333Strike



Users browsing this thread: 3 Guest(s)