Ruby/RGSSx questions that don't deserve their own thread

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
no worries.

Its just so that the thread doesnt get derailed with heavily specific issues I think.
 

Neok

Veteran
Veteran
Joined
Jul 31, 2014
Messages
45
Reaction score
15
First Language
English
Primarily Uses
Analyzing KilloZapit's (absolutely amazing!) Word Wrap Script:

http://www.rpgmakervxace.net/topic/6964-word-wrapping-message-boxes/

So these lines here:

if c =~ /[ \t]/if @lastc =~ /[\s\n\f]/Can someone explain what's meant specifically by these lines? It's found on line 77 and 78.

Specifically what I want to know:

What does it mean to have the tilde after equals? As in:

=~

How does the syntax work for the part afterwards? These parts:

/[ \t]/

/[\s\n\f]/

I assume those are escape characters, but how does the program read this? What's /[ ]/ mean? Does having the space in /[ \t]/ mean anything versus no space in /[\s\n\f]/?
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
=~ is a shorthand way to check for regular expression matches. It is identical to doing

if c.match(/[ \t]/)/[ \t]/ and /[\s\n\f]/ are regular expression match patterns. The two slashes, one in front and one at the end, make it clear to Ruby that between them is a regular expression. 

[ \t] means to match either a space ( ), or a tab(\t).

[\s\n\f] means to match any whitespace character (space, tab, etc), a new line (\n), and another whitespace character, though I don't know specifically what \f matches to.
 

cremnophobia

Veteran
Veteran
Joined
Dec 10, 2013
Messages
216
Reaction score
97
Primarily Uses
The \f escape sequence represents the so called form feed. You probably won't use it ever and the meta character \s already matches the escape sequences \n and \f, so the code can be simplified to:

Code:
if @lastc =~ /\s/
The RPG Maker help file contains a regular expression reference. It's just a reference, though. It's definitely worth it to learn them as they are powerful and useful. And many other programming languages and some programs support them too. In a way.
 
Last edited by a moderator:

Neok

Veteran
Veteran
Joined
Jul 31, 2014
Messages
45
Reaction score
15
First Language
English
Primarily Uses
Ah okay, thanks guys. Still a little confused about how the program knows where to separate expressions though.

Say for example, I'm taking text and removing all instances of:

\en

\enc

\enr

replacing them with empty space, using gsub, how would the syntax for that go?

 

My guess was:

text = text.gsub(/\en\enc\enr/, "")

 

which is wrong. What's the right way of doing this?

 

(Thanks for the Rubular website link. I'll be sure to check it out, and the RM help file.)

 

Oh, as a side note, something I noticed... Why do a lot of scripters use 2 modules to assign their constants? Usually it's something like:

 

module Name_of_person_who_made_script

     module Name_of_script

 

Any particular reason to use 2 modules instead of just 1?
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
You would want to do \enc and \enr first, because otherwise your \enc and \enr would be changed to c and r


Because two people could make scripts with similar script names.
 

Zeriab

Huggins!
Veteran
Joined
Mar 20, 2012
Messages
1,269
Reaction score
1,423
First Language
English
Primarily Uses
RMXP
@Neok:

I suggest you read my tutorial on regular expressions: https://sites.google.com/site/zeriabsjunk/tutorials/regexp-in-rgss

While it is written for RGSS most, if not all, should work for RGSS3.

As for your regular expression /\en\enc\enr/ well, you are actually escaping you e characters. I do not know what \e is interpreted as. It's not listed in the syntax list over special characters.

To actually search for the \ character you need to escape that. I.e.  /\\en\\enc\\enr/ would be what you meant. It's still not what you want since it only matches a string that got \en\enc\enr in it. Such as the string 'tee\en\enc\enrso'.

What you need is a regular expression that matches either \en, \enc or \enr

Give it a try. Let me know of your best guess.

*hugs*

 - Zeriab
 

Milena

The woman of many questions
Veteran
Joined
Jan 26, 2014
Messages
1,281
Reaction score
106
First Language
Irish
Primarily Uses
N/A
How can I determine the equipment note of an actor in battle? My code is:

#-------------------------------------------------------------------------- # * Calculate Damage #-------------------------------------------------------------------------- def make_damage_value(user, item) value = item.damage.eval(user, self, $game_variables) value *= item_element_rate(user, item) value *= pdr if item.physical? if item.magical? if user.is_a?(Game_Actor) if $game_party.members[user.id].equips.each do |equip| if equip.note =~ /<kurenai>/ end end end end end value *= rec if item.damage.recover? value = apply_critical(value) if @result.critical value = apply_variance(value, item.damage.variance) value = apply_guard(value) @result.make_damage(value.to_i, item) endbut it says it doesn't know equips, let alone can I check the note.
 

Neok

Veteran
Veteran
Joined
Jul 31, 2014
Messages
45
Reaction score
15
First Language
English
Primarily Uses
Thanks for the article, Zeriab! It's confirmed that regular expressions are even more confusing than I'd hoped.

text = text.gsub(/\en\w*/, "")Here's my best shot. As far as I know, it removes every non-punctuation character beginning with '\en', irrelevant of length.

Something I've noticed. Is it possible to use gsub to sub in quotations?

text = text.gsub(/'/, """)Obviously doesn't work.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
Milena, $game_party.members is indexed by position in the party (starting at 0 for the leader), not by actor id. So if you have 4 party members, $game_party.members will only have 0, 1, 2 and 3. If your 4th member is actor 8, you still have to get to them via $game_party.members[3] - $game_party.members[8] will not give you anything.


It's not equips that is the problem - it's $game_party.members[actor.id]. In fact, the error message probably said something like "equips not defined for nilclass" which indicates that what comes BEFORE equips is the thing that has the problem.


Anyway, you don't need to do this. You're trying to look up an actor object. But the actor object is already passed into the method - you already have it available.


Use user.equips instead.
 

Doodleguy

Villager
Member
Joined
Jul 26, 2014
Messages
8
Reaction score
0
First Language
Filipino
Primarily Uses
Hello, I'm new to these forums and this is actually going to be my first post  :)

Alright, to the point:

This is probably a ridiculous question. How do you add states or other conditions to a specific enemy?

Last time I checked the Event Commands, you can only add states to an enemy index within the troop.

This is what I tried to do:

$data_enemies[enemy_id].add_state(state_id)

It was supposed to add a specified state to an enemy, depending on their ID. This didn't actually work, according to the Game_Interpreter

I also tried changing the brackets to make sure I was doing it right; the game still crashes.

Is there another way to do this? Sorry about my English.

Thanks in advance!
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,108
Reaction score
13,713
First Language
English
Primarily Uses
RMMV
You don't want to change $data_<anything>.


$data_enemies is an array of constants. A Game_Enemy is instantiated when it's needed, and gotten rid of when it's no longer needed. You need to wait until you have a Game_Enemy object (from $game_troop.members), then check its id, and if it's the one you want, add the state to it. This will only add the state to a SINGLE enemy of that kind, not to every one.


If you want ALL enemies of a certain kind to have a state by default, you would probably do it in the Game_Enemy.initialize method - again doing a check to see if the @enemy_id is the one you're after, and adding the state as you've done (but just by calling add_state(state_id) as you don't need the object prefix if you add the command within that method)
 

TheRiotInside

Extra Ordinaire
Veteran
Joined
Sep 3, 2012
Messages
270
Reaction score
123
First Language
English
Primarily Uses
Had a small RMXP question that's been bothering me. I've been getting the hang of script commands like,

self.state?(30)
Code:
self.skill_learn?(9)
and other commands like that, but I was wondering if there was another way to check for database items, using their name instead of the specific ID. Like, instead of a "skill_learn?(4)" I could somehow use something like "skill_learn?("Thunder") instead. Just some way to be able to search for a name instead of an ID. I'm sure I'd have to avoid naming two skills or states the same thing in this case, but I was just curious. The functionality of being able to add these little scripts before having the database all sorted out (or if I have to change around the database later) seems pretty alluring.

Thanks in advance!
 
Last edited by a moderator:

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
Something I've noticed. Is it possible to use gsub to sub in quotations?

text = text.gsub(/'/, """)Obviously doesn't work.
Does the first part work?

Also, for the second (quoted) part, the problem is that you're not escaping the quote you want to insert into the text. Ruby is interpreting that as

text = text.gsub(/'/, """)#set 'text' to 'text' after all instances of ' have been replaced with <invalid string>Ruby sees the first two "s and thinks 'ok, an empty string, which is fine. The third one comes in, however, and opens a new string. This one is never closed, so you will get many a error with invalid syntax, and things just not functioning as you would expect. To escape the quote, simpley replace 

""" with 

"\""The backlash is the escape character in nearly all programming languages. It's used to literally input special characters into a string, such as a quote, which would normally mark the beginning or end of a string.

Notes

With 

text = text.gsub(/'/, "\"")you can simplify this with the gsub! method. When a method has a '!' at the end of a name in Ruby, it will directly modify the object that calls the method. For example,

text = text.gsub(/'/, "\"")would be equivalent, and probably more efficient than

text.gsub!(/'/, "\"") Additionally, if you put a double quote inside a set of single quotes, Ruby will understand to input a double quote. For example,

text = "Apple"puts "The value of text is '#{text}'"would output 

Code:
The value of text is 'Apple'
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
Ruby sees the first two "s and thinks 'ok, an empty string, which is fine. The third one comes in, however, and opens a new string. This one is never closed, so you will get many a error with invalid syntax, and things just not functioning as you would expect. To escape the quote, simpley replace 

""" with 

"\""
Would

'"'Not work equally as well ?Thats what I've always done when having to put the " character into quotations.
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
935
First Language
English
Primarily Uses
N/A
I believe it does, but in my example with #{}, I don't believe single quotes interprets those. I could be wrong, but 

'"'will work fine.
 

??????

Diabolical Codemaster
Veteran
Joined
May 11, 2012
Messages
6,517
Reaction score
3,221
First Language
Binary
Primarily Uses
RMMZ
Oh yea, for the #{} its gotta be in a double quotation or it just wont show. Sure I have tried pulling that one (and failed) in the past :D

Can also do something like this...

Code:
    /#{some_text_variable}/i =~ "Some_text"
 

cremnophobia

Veteran
Veteran
Joined
Dec 10, 2013
Messages
216
Reaction score
97
Primarily Uses
If you want to replace certain characters, you can just use String#tr (see tr on wikipedia):

"'Hello'!".tr("'", '"') # =>  "\"Hello\"!" 
@TheRiotInside: Yes, multiple skills with the same name could be a problem. Or if a skill with the name doesn't exist. Enumerable#find returns the first match.

Code:
skill = $data_skills[1..-1].find { |skill| skill.name == 'Thunder' }fail 'unknown skill' unless skill skill_learn?(skill.id)
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 5)

Latest Threads

Latest Profile Posts

Day 9 of giveaways! 8 prizes today :D
He mad, but he cute :kaopride:

Our latest feature is an interview with... me?!

People4_2 (Capelet off and on) added!

Just beat the last of us 2 last night and starting jedi: fallen order right now, both use unreal engine & when I say i knew 80% of jedi's buttons right away because they were the same buttons as TLOU2 its ridiculous, even the same narrow hallway crawl and barely-made-it jump they do. Unreal Engine is just big budget RPG Maker the way they make games nearly identical at its core lol.

Forum statistics

Threads
106,040
Messages
1,018,479
Members
137,824
Latest member
dobratemporal
Top