Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 RGSS scripting dissections and explinations
#23
DerVVulfman Wrote:I guess to get ALL of the math for 13/5, you would need to calculate 13/5 (to get 2) and 13%5 (to get the remainder '3').
Or get a very specific result by dividing 13/5.0 #=> 2.6
If the last number is a float (a number with decimals), then you get the exact result at once, no need for remainders then. This situation would be useful if you want to calculate stuff like percents, but not that useful if you want to calculate in game item prices because nobody would expect decimals there.

Item ID 1 is a Potion, it's price is set to 50, adding 15% of taxes (yeah, expensive indeed) would make it cost 57.5 but you don't want that because... well, the party has gold pieces only, no silver coins, no copper coins, so you prefer to round it as much as to avoid decimals completely. In that case you would not just do this...

50*1.15

but...

(50*1.15).round

57.5 then becomes 57! round alters the previous algorhythm or calculation surrounded by parenthesis by removing all decimals.

Why did I multiply the price by 1.15? Because 1 represents 100% and 0.15 represents 15% in that calculation, also known as price plus taxes.

In Ruby there are two kind of strings, those enclosed by " " and others by ' '.

" " double quotes accept many special characters including % for substitutions, but single quotes are simple, they need \ to interpreter a third ' single quote as apostrophe or the stupid floating dash that appears in idioms like Siletrea's cat to show other people that such cat belongs to you. (Some memes would tell you otherwise implying the cat owns you according to the cat's own perspective XD) " " double quotes accept one or more ' single quotes inside and treats them as a quote (you're reporting what a neighbor told you last week, i.e. something about how dangerous a street has become since a gang took control of it) like in...

"My neighbor actually told me last week: 'those scumbags have taken over Elm Street and keep attacking clueless people that use it as a shortcut to save a few minutes...' It seems police is afraid of confronting them because the gang still own that street."

String substitutions, meaning you are gonna replace a weird character with an actual value (a number, a name), are defined like this.

CAT_OWNER = "%s's pet cat"
owner_name = 'DerVVulfman'

print sprintf(CAT_OWNER, owner_name) #=> "DerVVulfman's pet cat"

But let's say we had defined CAT_OWNER in a different way...

CAT_OWNER = "%s's pet cat has a weird name, it's %s."
owner_name = 'DerVVulfman'
cat_name = 'Patches'

print sprintf(CAT_OWNER, owner_name, cat_name) #=> "DerVVulfman's pet cat has a weird name, it's Patches."

sprintf means special print format, it's an adaptation of sprintf command found in C so that's why it kept it's weird name in Ruby even if it's not as readable as most commands in Ruby actually are. sprintf is used for string substitutions a lot depending on a scripter's taste. You can substitute as many characters (%s) as you defined in the original string. Keep in mind that the original string was not modified, sprintf made a copy of it with one or two names included.

There are more possibilities to substitute different stuff but I guess that one might help you a lot once you take the grasp of it.

DerVVulfman had said earlier Wrote:& thingie (not listed) ... we think of as the 'and' symbol is called an ampersand. Good for doing if... tests. 'if this & that.'

Why do we use & ampersand? Well, blame ancient Romans that defined the term "and" as et back in the days. Yes, it's an e plus a t mixed in a single symbol.

. Dots - well, they let us access methods or properties or attributes that belong to any object (a class, a module, an array, a hash, a variable, a number, even a string) if they already exist. Otherwise you'd get a NoMethodError message in no time because Ruby could not find it anywhere. (The only class where you can call a none existing method would be Struct, a special kind of class that can be altered at any time, but I think that's too complex for you now and we rarely use it anyway. It's like, err, combining a class with a hash so it lets you call and set any method, really, any method at anytime and returns nil if it just doesn't exist instead of the error message I mentioned above.)

.. Double Dots, ... Triple Dots - they are a different story, they are part of a Range! Range in Ruby means you set an initial value like 13 and a last value like 17 or 18 as the last value. Automatically you'll get every single value inbetween as well! There's a catch you gotta observe, double dots actually includes all those values (always numbers, no exceptions!), but triple dots exclude the last value.

0..18 #=> a Range representing all ages that would make a person be called a minor and get arrested if found drinking alcohol.
0...19 #=> same as above but skips 19

first_value..last_value #=> if first_value is 19 and last_value is 100 it will work like any Range that's defined with numbers.

countdown = 60 #=> let's say it represents 60 minutes here
first_value = 0
last_value = 120

range = first_value..last_value
range.include?(countdown) #=> true it IS included because first_value equals 0 and last_value equals 120.

every minute we substract a minute to our lovely countdown

countdown -= 1 #=> 59 minutes left because countdown - 1 would be 59

But that would be a mere calculation, to make the variable keep the new value we set it like this: countdown = countdown - 1 or countdown -= 1, they mean exactly the same, both will allow us to substract 1 minute from countdown.

if countdown == first_value
print "Siletrea's birthday party begins now!"
else
print "Siletrea should wait patiently..."
end

Yeah, I know, there's a second command named include... well, that's not completely accurate, it's called include? so Ruby thinks it's different from include. How can you be so sure? Well, use it to include a module like RPG and you'll get an error that will shutdown your game after you close the pop up window XD. include? with that question mark ? is a query or something like a question you make while holding a conversation with range. It's like asking range...

"Do you include 60, my dear friend range?"

Instead of getting a response like yes or not, range will tell you it's true or false only.
"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
RE: RGSS scripting dissections and explinations - by kyonides - 09-07-2017, 04:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Help iwth script (RGSS Player crash) Whisper 3 6,529 06-17-2017, 05:03 PM
Last Post: Whisper
  How can I use the cmd of "require" in rgss superegp 2 5,350 11-03-2015, 06:16 AM
Last Post: kyonides
   Scripting in VX vs VX Ace Miharu 5 8,165 02-21-2015, 10:10 AM
Last Post: Taylor
   Combat animations via scripting; How? ZeroSum 2 4,552 09-20-2013, 06:58 PM
Last Post: ZeroSum
Question  RGSS stoped to work Chaos17 5 6,879 02-14-2013, 05:13 PM
Last Post: DerVVulfman
   Ruby, RGSS & General Code Discussion Kain Nobel 6 9,844 12-22-2012, 05:11 AM
Last Post: MechanicalPen
   [Request] Tut. for RGSS Eldur 9 10,549 12-07-2012, 04:27 AM
Last Post: DerVVulfman
   [ASK-RGSS] Behemoth's CBS alike Getsuga_kawaii 0 3,850 04-29-2010, 03:07 PM
Last Post: Getsuga_kawaii
   Scripting I think spazfire 7 8,916 04-12-2010, 03:21 AM
Last Post: DerVVulfman
   Beginner Scripting Tuts? KDawg08 1 3,669 03-31-2010, 11:03 PM
Last Post: Hsia_Nu



Users browsing this thread: