Chaos Project

RPG Maker => RPG Maker Scripts => Topic started by: Seox on June 17, 2009, 09:17:29 pm

Title: Favorite scripting features?
Post by: Seox on June 17, 2009, 09:17:29 pm
So, what're some of your favorite features/constructs in ruby?

As simple as it is, I use case statements A LAWT, and the include? method of class Array. I also tend to like the stategy of going:

frog = array.include?('urine')
frog = frog + ETC.....



Where you define over the SAME temporary variable in order to save space, memory, organization, and stuff.



What are some of yours?
Title: Re: Favorite scripting features?
Post by: G_G on June 17, 2009, 09:40:16 pm
I like the features of using methods in modules so I can easily make it where instead of using long lines of code like this
$game_variables[5] += $points
I can shorten it up using this
Gv.addval(5, $points)

I made a module and added methods in it so it shortens up code to make it not so long.

EDIT:
And I do it for other things. Use this
module A
  def self.ls(id, iid)
    $game_actors[id].learn_skill(iid)
  end
end


Then use A.ls(actor_id, skill_id)
what this does is instead of using
$game_actors[actor id].learn_skill(skill id)
you can use this instead
A.ls(actor id, skill id)
Title: Re: Favorite scripting features?
Post by: Seox on June 17, 2009, 09:42:59 pm
Quote from: game_guy on June 17, 2009, 09:40:16 pm
I like the features of using methods in modules so I can easily make it where instead of using long lines of code like this
$game_variables[5] += $points
I can shorten it up using this
Gv.addval(5, $points)

I made a module and added methods in it so it shortens up code to make it not so long.


So you have sort of a "common events" type module, metaphorically?

Neat! That's a good idea ^_^
Title: Re: Favorite scripting features?
Post by: G_G on June 17, 2009, 09:44:05 pm
In case you didnt see the rest of the above post....

And I do it for other things. Use this
module A
 def self.ls(id, iid)
   $game_actors[id].learn_skill(iid)
 end
end


Then use A.ls(actor_id, skill_id)
what this does is instead of using
$game_actors[actor id].learn_skill(skill id)
you can use this instead
A.ls(actor id, skill id)
Title: Re: Favorite scripting features?
Post by: Seox on June 17, 2009, 09:56:23 pm
Quote from: game_guy on June 17, 2009, 09:44:05 pm
In case you didnt see the rest of the above post....

And I do it for other things. Use this
module A
 def self.ls(id, iid)
   $game_actors[id].learn_skill(iid)
 end
end


Then use A.ls(actor_id, skill_id)
what this does is instead of using
$game_actors[actor id].learn_skill(skill id)
you can use this instead
A.ls(actor id, skill id)



O_o

That's nifty. Thanks! ^_^



I'm trying to think of something that may be useful to people....
Title: Re: Favorite scripting features?
Post by: Blizzard on June 18, 2009, 04:23:29 am
.each .any? .all? .find_all <3

Definitely saving results of time consuming actions and proper class hierarchy to localize functionality and responsibilities of the different classes.
Title: Re: Favorite scripting features?
Post by: fugibo on June 18, 2009, 01:25:50 pm
Quote from: Blizzard on June 18, 2009, 04:23:29 am
.each .any? .all? .find_all <3

Definitely saving results of time consuming actions and proper class hierarchy to localize functionality and responsibilities of the different classes.


HOLY CRAP, I <3 Array#find_all!!!!! I HAD NO IDEA THAT EXISTED!!! *levels blizz up*

My favorite feature is the dynamic typing. Horribly, horribly slow, but amazing useful, especially in RGSS.

Also: GG, BAD BAD BAD!!!! You know that writing methods to take the place of single lines of code is wrong! Stopitrightthisinstant!
Title: Re: Favorite scripting features?
Post by: Blizzard on June 18, 2009, 01:40:59 pm
Depends on how far you want to go with object oriented design. But in RGSS you shouldn't use it unless it simplifies your code a lot. And that piece of code is obviously pointless. xD *slaps G_G*
Title: Re: Favorite scripting features?
Post by: Ryex on June 18, 2009, 02:26:52 pm
wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters
Title: Re: Favorite scripting features?
Post by: fugibo on June 18, 2009, 03:05:03 pm
Quote from: Ryexander on June 18, 2009, 02:26:52 pm
wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters


Here's an example that should clarify:

array = [1, 2, 3, 4, 5]
array2 = array.find_all { |n| n > 2 }
array3 = array.find_all { |n| n < 4 }

print array; print array2; print array3;
Title: Re: Favorite scripting features?
Post by: Seox on June 18, 2009, 03:08:00 pm
Still, I think the underlying point is noteworthy - that you can create a module with common and long methods, and then use them.

Dynamic typing?

Quote from: Blizzard on June 18, 2009, 04:23:29 am
.each .any? .all? .find_all <3

Definitely saving results of time consuming actions and proper class hierarchy to localize functionality and responsibilities of the different classes.


Aren't those of module Enumerable? Does that have to be mixed in/can it be mixed in to RGSS stuffs? I just looked em up, and they ALL look useful, especially .any? and .all?
Title: Re: Favorite scripting features?
Post by: Ryex on June 18, 2009, 04:22:12 pm
Quote from: Longfellow on June 18, 2009, 03:05:03 pm
Quote from: Ryexander on June 18, 2009, 02:26:52 pm
wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters


Here's an example that should clarify:

array = [1, 2, 3, 4, 5]
array2 = array.find_all { |n| n > 2 }
array3 = array.find_all { |n| n < 4 }

print array; print array2; print array3;



WHOOO! so it finds n where n > 2 and returns an array with all entries? COOL! THAT'S SO USEFUL!

so could you do this?

array = ['hi', 'book', 'name', 'book', 'hello', 'book']
array2 = array.find_all { |n| n == 'book' }
Title: Re: Favorite scripting features?
Post by: Seox on June 18, 2009, 04:28:11 pm
Quote from: Ryexander on June 18, 2009, 04:22:12 pm
Quote from: Longfellow on June 18, 2009, 03:05:03 pm
Quote from: Ryexander on June 18, 2009, 02:26:52 pm
wait .find_all what dose that do? and ya G_G the code is pointless, you increase processing time and add four lines of code to shorten one line of code by six characters


Here's an example that should clarify:

array = [1, 2, 3, 4, 5]
array2 = array.find_all { |n| n > 2 }
array3 = array.find_all { |n| n < 4 }

print array; print array2; print array3;



WHOOO! so it finds n where n > 2 and returns an array with all entries? COOL! THAT'S SO USEFUL!

so could you do this?

array = ['hi', 'book', 'name', 'book', 'hello', 'book']
array2 = array.find_all { |n| n == 'book' }



I would think so. Hold up, I'll try it in IRB.

Yeah, it works, returning:
# =>  array2 = ['book', 'book', 'book']
Title: Re: Favorite scripting features?
Post by: fugibo on June 18, 2009, 04:48:02 pm
Another feature you guys might find useful is the RegExp comparison operator (=~), which checks a string against a Regular Expression (Zeriab posted a tutorial on that somewhere around here).


STRINGS = [ "Hello, Calintz! What's up? Haven't seen you in IRC in a while, lol. Hey, have you read the Necronomicon? BEST BOOK EVAR!", "You know something? My car was just stolen.", "HEY OLD MAN, DOWN IN FRONT!", "Man, that chick is HAWT." ]

book_references = STRINGS.find_all? {|s| s =~ /book/i} # The "i" at the end means case-insenstive.
man_references = STRINGS.find_all? {|s| s =~ /man/i}

print book_references; print man_references;


Also, *levels up for installing full ruby*

EDIT:
Oops, missed the "Dynamic Typing" thing. Googling it should yield decent results, but in summary it's the nature of a language that allows everything to change at any given moment, for example how you can redefine methods and classes. It especially refers to languages where this can be done at run-time.

EDIT2: Ah, no-one will ever know of his poor car :(
Title: Re: Favorite scripting features?
Post by: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?
Title: Re: Favorite scripting features?
Post by: fugibo on June 18, 2009, 07:17:29 pm
Quote from: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?


Once I used to cheat on a really crappy assignment in Health class. You see, we were supposed to complete a daily journal of our health habbits (or something) for one month. Except, I didn't do it. I also didn't feel like typing up 31 pages of crap I did. So, I made one text file that looked kinda like this:

[month] [day], 2005

6:00: Woke up, [verb1], [verb2] [noun1] for breakfast.
<etc etc for the entire day>


Then came up with some random verbs/nouns and made a Ruby script that detected []'s and filled them in from a hash. Took a tenth of the time doing it manually would've taken, and it was more fun XD
Title: Re: Favorite scripting features?
Post by: Seox on June 18, 2009, 09:40:22 pm
Quote from: Longfellow on June 18, 2009, 07:17:29 pm
Quote from: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?


Once I used to cheat on a really crappy assignment in Health class. You see, we were supposed to complete a daily journal of our health habbits (or something) for one month. Except, I didn't do it. I also didn't feel like typing up 31 pages of crap I did. So, I made one text file that looked kinda like this:

[month] [day], 2005

6:00: Woke up, [verb1], [verb2] [noun1] for breakfast.
<etc etc for the entire day>


Then came up with some random verbs/nouns and made a Ruby script that detected []'s and filled them in from a hash. Took a tenth of the time doing it manually would've taken, and it was more fun XD


Oh, that's EPIC. I totally have to try that, XD. I wrote a ruby program a while back that acts as flash cards, which I'll use when school starts again. probably never. I can't/don't study. Regardless, pretty cool stuff. It stores missed problems in an array, and then tells you which you missed at the end, too.
Title: Re: Favorite scripting features?
Post by: Blizzard on June 19, 2009, 04:52:18 am
I use regular expressions in RMX-OS to check for messages from the clients.

	case message
when /REG(.+),(.+)/ # register request
result = self.try_register($1, $2)
# if registering was successful
if result == RESULT_SUCCESS
# log in as well
self.try_login($1, $2)
# send username and user ID
self.send("USR#{@username}")
self.send("UID#{@user_id}")
end
self.send("REG#{result}")
return true
end


And yes, .any?, .all?, etc. are from Enumerable. For them to work, .each needs to be defined in a class and Enumerable needs to be included I think.
Title: Re: Favorite scripting features?
Post by: Seox on June 19, 2009, 11:22:14 am
^_^
Title: Re: Favorite scripting features?
Post by: Zeriab on June 20, 2009, 12:23:51 pm
Enumerable and Procs <3
Title: Re: Favorite scripting features?
Post by: G_G on June 20, 2009, 12:58:04 pm
Again I like how modules can make things simpler like so.
module Gv
  def self.getval(id)
    return $game_variables[id]
  end
  def self.setval(id, val)
    $game_variables[id] = val
  end
  def self.addval(id, val)
    $game_variables[id] += val
  end
  def self.subval(id, val)
    $game_variables[id] -= val
  end
  def self.multval(id, val)
    $game_variables[id] *= val
  end
  def self.divval(id, val)
    $game_variables[id] / val
  end
end


Ok instead of using something like this
$points = $game_variables[id]
you can use this instead
$points = Gv.getval(id)

To me it makes things alot quicker. Here are the different operatoins you can use

Gv.getval(id) # Get the value of the game variable
Gv.setval(id, val) # Set the value of the game variable
Gv.addval(id, val) # Add value to the game variable
Gv.subval(id, val) # Subtract value from the game variable
Gv.multval(id, val) # Multiply value to the game variable
Gv.divval(id, val) # Divide value by the game variable

id = variable id
val = the value you want to set/add/subtract/multiply/divide the variable to

I set modules to do something shorter for me when I know I'm going to be using alot of that command line. I also made on for play sounds so I can use very few letters to make it play a sound
Title: Re: Favorite scripting features?
Post by: Blizzard on June 20, 2009, 01:01:49 pm
You are overcomplicating this. It's completely unnecessary.
Title: Re: Favorite scripting features?
Post by: G_G on June 20, 2009, 01:05:40 pm
Well I really dont like typing out $game_variables[id] += or whatever so I shortened it.
Title: Re: Favorite scripting features?
Post by: Ryex on June 20, 2009, 01:21:46 pm
but your INCREASING process time AND memory usage ADDING FOUR lines of code to get rid of six letters in one line! that's just stupid!
Title: Re: Favorite scripting features?
Post by: fugibo on June 20, 2009, 01:24:11 pm
Quote from: Seox on June 18, 2009, 09:40:22 pm
Quote from: Longfellow on June 18, 2009, 07:17:29 pm
Quote from: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?


Once I used to cheat on a really crappy assignment in Health class. You see, we were supposed to complete a daily journal of our health habbits (or something) for one month. Except, I didn't do it. I also didn't feel like typing up 31 pages of crap I did. So, I made one text file that looked kinda like this:

[month] [day], 2005

6:00: Woke up, [verb1], [verb2] [noun1] for breakfast.
<etc etc for the entire day>


Then came up with some random verbs/nouns and made a Ruby script that detected []'s and filled them in from a hash. Took a tenth of the time doing it manually would've taken, and it was more fun XD


Oh, that's EPIC. I totally have to try that, XD. I wrote a ruby program a while back that acts as flash cards, which I'll use when school starts again. probably never. I can't/don't study. Regardless, pretty cool stuff. It stores missed problems in an array, and then tells you which you missed at the end, too.


I started working on a fairly complex version of that back at the beginning of the year, using Ruby-GNOME2 and REXML (Ruby Electric Extensible Markup Language, part of the standard library). I HATE rote memorization, but we had a ton of that, so...
Title: Re: Favorite scripting features?
Post by: Seox on June 20, 2009, 02:14:59 pm
Quote from: Longfellow on June 20, 2009, 01:24:11 pm
Quote from: Seox on June 18, 2009, 09:40:22 pm
Quote from: Longfellow on June 18, 2009, 07:17:29 pm
Quote from: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?


Once I used to cheat on a really crappy assignment in Health class. You see, we were supposed to complete a daily journal of our health habbits (or something) for one month. Except, I didn't do it. I also didn't feel like typing up 31 pages of crap I did. So, I made one text file that looked kinda like this:

[month] [day], 2005

6:00: Woke up, [verb1], [verb2] [noun1] for breakfast.
<etc etc for the entire day>


Then came up with some random verbs/nouns and made a Ruby script that detected []'s and filled them in from a hash. Took a tenth of the time doing it manually would've taken, and it was more fun XD


Oh, that's EPIC. I totally have to try that, XD. I wrote a ruby program a while back that acts as flash cards, which I'll use when school starts again. probably never. I can't/don't study. Regardless, pretty cool stuff. It stores missed problems in an array, and then tells you which you missed at the end, too.


I started working on a fairly complex version of that back at the beginning of the year, using Ruby-GNOME2 and REXML (Ruby Electric Extensible Markup Language, part of the standard library). I HATE rote memorization, but we had a ton of that, so...


*prepares to be slapped*
What is rote memorization?
Title: Re: Favorite scripting features?
Post by: fugibo on June 20, 2009, 02:19:49 pm
Quote from: Seox on June 20, 2009, 02:14:59 pm
Quote from: Longfellow on June 20, 2009, 01:24:11 pm
Quote from: Seox on June 18, 2009, 09:40:22 pm
Quote from: Longfellow on June 18, 2009, 07:17:29 pm
Quote from: Seox on June 18, 2009, 06:20:05 pm
XD


Thank you for the level ^_^

I can see =~ being useful in ruby proper, but not nearly as much in RMXP. What do you use it for, specifically?


Once I used to cheat on a really crappy assignment in Health class. You see, we were supposed to complete a daily journal of our health habbits (or something) for one month. Except, I didn't do it. I also didn't feel like typing up 31 pages of crap I did. So, I made one text file that looked kinda like this:

[month] [day], 2005

6:00: Woke up, [verb1], [verb2] [noun1] for breakfast.
<etc etc for the entire day>


Then came up with some random verbs/nouns and made a Ruby script that detected []'s and filled them in from a hash. Took a tenth of the time doing it manually would've taken, and it was more fun XD


Oh, that's EPIC. I totally have to try that, XD. I wrote a ruby program a while back that acts as flash cards, which I'll use when school starts again. probably never. I can't/don't study. Regardless, pretty cool stuff. It stores missed problems in an array, and then tells you which you missed at the end, too.


I started working on a fairly complex version of that back at the beginning of the year, using Ruby-GNOME2 and REXML (Ruby Electric Extensible Markup Language, part of the standard library). I HATE rote memorization, but we had a ton of that, so...


*prepares to be slapped*
What is rote memorization?


You know how sometimes teachers will give you a list of stuff, tell you to memorize it, and call it "teaching?" That's Rote learning. The memorization part is the Rote memorization.
Title: Re: Favorite scripting features?
Post by: Seox on June 20, 2009, 02:50:19 pm
Nice explanation, thank you ^_^

Yeah, I HATE that crap! It's usually the really dumb teachers, too. And then you're all, WTF? I WANTED TO LEARN THIS CRAP!

/end rant
Title: Re: Favorite scripting features?
Post by: Blizzard on June 21, 2009, 06:16:51 am
G_G, you can providing an interface for something that needs none.
Title: Re: Favorite scripting features?
Post by: fugibo on June 21, 2009, 09:29:54 am
Also, there are assignment operators besides =, GG.


a = 1     # a is One.
a += 2    # a is 3.
a -= 4    # a is -1.
a *= 42   # a is -42.
a /= 1764 # a is -1/42.
a *= -42  # a is 1.
a += 2    # a is 3
a **= 3   # a is 27.
a %= 4    # a is 3.
Title: Re: Favorite scripting features?
Post by: Seox on June 21, 2009, 12:24:34 pm
Quote from: Longfellow on June 21, 2009, 09:29:54 am
Also, there are assignment operators besides =, GG.

a **= 3   # a is 27. 
a %= 4    # a is 3.




Those last two are "a to the third power", and "a modulo 4", right?

And, aren't _= shortcuts for
a = a _ b
?

Thanks, Longfellow ^_^

(And where did that name and WcW come from, btw? Just curious. They seem random. But so does Seox, so meh.)
Title: Re: Favorite scripting features?
Post by: fugibo on June 21, 2009, 01:49:38 pm
Sí. Now learn

a = 255
a &= 0xf # 15
a |= 0xff # 255
a &= ~0xffffffff # 0
Title: Re: Favorite scripting features?
Post by: Seox on June 21, 2009, 03:31:17 pm
Quote from: Longfellow on June 21, 2009, 01:49:38 pm
Sí. Now learn

a = 255
a &= 0xf # 15
a |= 0xff # 255
a &= ~0xffffffff # 0



Aren't those unicode, or character binary or something? I'm sorry, and I know that you'll slap me for asking such a blatantly obvious question, but I don't know my way around when it comes to that topic.
Title: Re: Favorite scripting features?
Post by: fugibo on June 21, 2009, 04:22:41 pm
Actually, a leading 0x denotes base-16, or hexadecimal (http://en.wikipedia.org/wiki/hexadecimal). Binary (base 2) (http://en.wikipedia.org/wiki/Radix) can't be typed in most programming languages, and Unicode/ASCII are character encodings (http://en.wikipedia.org/wiki/Character_encoding).

I'll give you a while longer to figure out the operators yourself ;)