- 971
- Posts
- 8
- Years
- Age 22
- Seen Nov 28, 2022
Unlike the first resource I called "Utility Scripts", this one is actually useful. It adds loads of methods and functionality to arrays, strings, hashes, numerics, booleans, sprites, and so forth. They're primarily used by my resources, but you may very well find some methods to be useful yourself (if you're a coder).
Installation
Add this in a new script section above Main (and ideally above all of my resources that need this script!):
Marin_Utilities
Object:
get_variables: Gets all instance variables of an object and returns them in an array.
set_variables(vars): Sets all variables in the vars object to their values.
Class:
to_sym: Simply a shorter way of writing .to_s.to_sym().
NilClass:
empty?: Always returns true for compatibility with to_json().
Numeric:
format: Formats the numbers with a separator (e.g. 99999.format('.') #=> "99.999")
to_digits(n=3): Converts self to a string and ensures it has at least n digits (e.g. 13.to_digits(3) #=> "013").
root(n=2): Performs the n root on self.
fact: Performs factorial on self.
ncr(k): Combinations (selfCk).
npr(k): Permutations (selfPk).
to_b: Returns the binary representation of self.
empty?: Same as NilClass#empty?.
numeric?: Compatibility with String#numeric?.
Float:
round(n=0): Rounds self to n decimals (always returns as Float, even if a whole number).
TrueClass:
include Boolean: Includes the (empty) Boolean module. This so you can call true.is_a?(Boolean).
to_i: Returns 1 to be able to use true as an array index.
FalseClass:
include Boolean: Includes the (empty) Boolean module. This so you can call true.is_a?(Boolean).
to_i: Returns 0 to be able to use true as an array index.
Enumerable:
fullsize: Gets the full size of self, also counting string size, array size (recursively). Used for a certain resource.
Array:
random: Returns a random array element.
shuffle: Shuffles all elements of self to a random order.
shuffle!: Replaces self with the result of shuffle().
breakup
: Breaks self into subarrays every n elements.
breakup!
: Replaces self with the result of breakup
.
swap(index1,index2): Swaps the positions of the two passed indexes.
swap!(index1,index2): Replaces self with the result of swap(index1,index2)
starts_with?(e): Returns whether or not the first element in the array is equal to e.
ends_with?(e): Returns whether or not the last elemtn in the array is e qual to e.
+(var): Does the same as <<(var), but if var is an array, it concatenates it into the main array.
-(var): Deletes every instance of var in self.
to_hash(delete_nil=false): Tries to convert self to a Hash. Deletes nil empties if passed argument is true.
mid(round_up=true): Returns the middle element of self. If even, it'll round up or down depending on the passed argument.
average: Gets the average of all elements in the array (provided they're all Numeric)
to_json: Converts self to a JSON representation. Even though this method accepts arguments, do not pass any.
self.make_table(range=1..10,&proc): Makes an array table with range. For every element in range, it'll call proc with the current iteration as parameter. Example:
Hash:
to_array(delete_nil=false): Tries to convert self to an Array as best as possible. If passed argument is true, it'll ignore nil entries.
compact: Deletes all nil entries.
compact!: Replaces self with the result of compact().
to_json: Convert self to a JSON representation. Even though this method accepts arguments, do not pass any.
String:
starts_with?(str): Returns whether or not self starts with str.
ends_with?(str): Returns whether or not self ends with str.
to_b: Converts self to binary.
to_b!: Replaces self with the result of to_b().
from_b: Converts self from binary.
from_b!: Replaces self with the result of from_b().
first(n=1): Returns the first n characters.
last(n=1): Returns the last n characters.
random: Returns a random character.
shuffle: Shuffles all characters in self.
breakup
: Splits self every n characters and returns an array.
empty?: Returns whether or not the string is empty.
numeric?: Returns whether or not self could be converted to a valid Numeric.
deflate: Deflates self with Zlib.
deflate!: Replaces self with the result of deflate().
inflate: Inflates self with Zlib.
inflate!: Replaces self with the result of inflate().
File:
self.copy(source,destination): Copies the file at source to destination.
self.move(source,destination): Executes self::Copy(source,destination) and deletes source.
self.rename(source,destination): Alias of self.move.
self.inflate(file): Opens file, inflates its data and writes it back.
self.deflate(file): Opens file, deflates its data and writes it back.
self.create(path,data=nil): Creates a new file at path. If any of the subdirectories don't exist, they will be made.
Dir:
self.get_all(path,recursive=true): Gets all files/directories in the specified directory. If the second parameter is true, it will also return all files/directories in subdirectories.
self.get_files(path,recursive=true): Gets all files in the specified directory. If the second parameter is true, it will also return all files in subdirectories.
self.get_dirs(path,recursive=true): Gets all directories in the specified directory. If the second parameter is true, it will also return directories in subdirectories.
self.delete(path,non_empty=true): Deletes the specified directory. If the second parameter is true, it will delete all files inside first. You cannot delete non-empty directories.
Sprite:
bmp(arg1=nil,arg2=nil): Does a number of things. Example usages:
xyz=(args): Sets the x, y, and z coordinates. Usage:
center_origins: Centers a sprite by setting the origin points to half the width/height.
fullwidth: Returns the actual width of the sprite, taking zoom_x into account.
fullheight: Returns the actual height of the sprite, taking zoom_y into account.
TextSprite:
It's a Sprite, but contains some neat methods to make drawing text easier and faster.
initialize(viewport=nil,text=nil,width=-1,height=-1): Calls Sprite#initialize(viewport). Creates a new bitmap based on the passed width and height (by calling bmp(width,height)).
If text is a normal, valid array as you'd normally use with pbDrawTextPositions ([text,x,y,align,basecolor,shadowcolor]), it'll draw that afterward. Note that this doesn't have to be an array of arrays; if you have just one thing you want to draw, just one array is enough. This applies to all draw functions too.
clear: Clears the bitmap of any text that was on it.
draw(text,clear=false): Draws text to the bitmap. If clear is true, it'll clear the bitmap beforehand.
draw_outline(text,clear=false): Draws text to the bitmap with an outline. If clear is true, it'll clear the bitmap beforehand.
draw_ex(text,clear=false). Draws text to the bitmap. The align parameter is now the width one line may take up. If it exceeds that width, it'll continue drawing on a new line.
dispose: Disposes the Sprite and clears the bitmap.
SpriteHash:
This is your typical @sprites hash, but as a class. It contains a lot of useful functionality, such as being able to increase/decrease values such as x, y, z, opacity, and such -- relative. If you set the SpriteHash's x to 0 for example, all sprites would have a 0 increase, meaning they still have their own x value.
Worth noting is that you can have SpriteHashes inside SpriteHashes -- In fact, I use this, because it helps keeping things tidy and organized, and allows for quick and easy manipulating of sprites.
If possible, all keys are converted to symbols. Getting or setting them by key will automatically convert them, so you won't notice that.
[]: Returns the sprite in the specified key.
[]=: Sets the sprite in the specified key.
raw: Returns the internal hash in its raw form.
keys: Returns all keys of the hash.
size/length/count: Returns the size of the hash.
clone: Clones the hash.
add(key,value): Alias for []=.
delete(key): Deletes an entry by key.
each: Iterates over all entries and yields the values (not the key-values!).
update: Calls update() on each entry in the hash (if they respond to update()).
dispose: Disposes all sprites in the hash.
disposed?: Compatibility.
x=(value): Manipulates the x value.
y=(value): Manipulates the y value.
z=(value): Manipulates the z value.
visible=(value): Manipulates the visible value.
opacity=(value): Manipulates the opacity value.
hide(frames=16): Fades out all sprites (by decreasing their opacity) over frames frames.
show(frames=16): Fades in all sprites (by increasing their opacity) over frames frames.
Input:
self.any?: Returns true if any of the listed buttons are pressed (or mouse, if defined).
Stand-alone Methods (not part of a class/module):
showBlk: Shows a black overlay over a time frame of 17 frames.
hideBlk: Hides and disposes a black overlay over a time frame of 17 frames.
pbGetExpPercentage(pokemon): Returns the percentage EXP a Pok?mon has compared to the next level.
rand: Extends the functionality and support of the random number generator:
Installation
Add this in a new script section above Main (and ideally above all of my resources that need this script!):
Marin_Utilities
Object:
get_variables: Gets all instance variables of an object and returns them in an array.
set_variables(vars): Sets all variables in the vars object to their values.
Class:
to_sym: Simply a shorter way of writing .to_s.to_sym().
NilClass:
empty?: Always returns true for compatibility with to_json().
Numeric:
format: Formats the numbers with a separator (e.g. 99999.format('.') #=> "99.999")
to_digits(n=3): Converts self to a string and ensures it has at least n digits (e.g. 13.to_digits(3) #=> "013").
root(n=2): Performs the n root on self.
fact: Performs factorial on self.
ncr(k): Combinations (selfCk).
npr(k): Permutations (selfPk).
to_b: Returns the binary representation of self.
empty?: Same as NilClass#empty?.
numeric?: Compatibility with String#numeric?.
Float:
round(n=0): Rounds self to n decimals (always returns as Float, even if a whole number).
TrueClass:
include Boolean: Includes the (empty) Boolean module. This so you can call true.is_a?(Boolean).
to_i: Returns 1 to be able to use true as an array index.
FalseClass:
include Boolean: Includes the (empty) Boolean module. This so you can call true.is_a?(Boolean).
to_i: Returns 0 to be able to use true as an array index.
Enumerable:
fullsize: Gets the full size of self, also counting string size, array size (recursively). Used for a certain resource.
Array:
random: Returns a random array element.
shuffle: Shuffles all elements of self to a random order.
shuffle!: Replaces self with the result of shuffle().
breakup
breakup!
swap(index1,index2): Swaps the positions of the two passed indexes.
swap!(index1,index2): Replaces self with the result of swap(index1,index2)
starts_with?(e): Returns whether or not the first element in the array is equal to e.
ends_with?(e): Returns whether or not the last elemtn in the array is e qual to e.
+(var): Does the same as <<(var), but if var is an array, it concatenates it into the main array.
-(var): Deletes every instance of var in self.
to_hash(delete_nil=false): Tries to convert self to a Hash. Deletes nil empties if passed argument is true.
mid(round_up=true): Returns the middle element of self. If even, it'll round up or down depending on the passed argument.
average: Gets the average of all elements in the array (provided they're all Numeric)
to_json: Converts self to a JSON representation. Even though this method accepts arguments, do not pass any.
self.make_table(range=1..10,&proc): Makes an array table with range. For every element in range, it'll call proc with the current iteration as parameter. Example:
Code:
# Since no _range_ parameter was passed, it'll default to 1..10 (from 1 to 10, inclusive)
# This block performs the power of 2 on whatever number is passed
Array.make_table { |i| i ** 2 }
#=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Hash:
to_array(delete_nil=false): Tries to convert self to an Array as best as possible. If passed argument is true, it'll ignore nil entries.
compact: Deletes all nil entries.
compact!: Replaces self with the result of compact().
to_json: Convert self to a JSON representation. Even though this method accepts arguments, do not pass any.
String:
starts_with?(str): Returns whether or not self starts with str.
ends_with?(str): Returns whether or not self ends with str.
to_b: Converts self to binary.
to_b!: Replaces self with the result of to_b().
from_b: Converts self from binary.
from_b!: Replaces self with the result of from_b().
first(n=1): Returns the first n characters.
last(n=1): Returns the last n characters.
random: Returns a random character.
shuffle: Shuffles all characters in self.
breakup
empty?: Returns whether or not the string is empty.
numeric?: Returns whether or not self could be converted to a valid Numeric.
deflate: Deflates self with Zlib.
deflate!: Replaces self with the result of deflate().
inflate: Inflates self with Zlib.
inflate!: Replaces self with the result of inflate().
File:
self.copy(source,destination): Copies the file at source to destination.
self.move(source,destination): Executes self::Copy(source,destination) and deletes source.
self.rename(source,destination): Alias of self.move.
self.inflate(file): Opens file, inflates its data and writes it back.
self.deflate(file): Opens file, deflates its data and writes it back.
self.create(path,data=nil): Creates a new file at path. If any of the subdirectories don't exist, they will be made.
Dir:
self.get_all(path,recursive=true): Gets all files/directories in the specified directory. If the second parameter is true, it will also return all files/directories in subdirectories.
self.get_files(path,recursive=true): Gets all files in the specified directory. If the second parameter is true, it will also return all files in subdirectories.
self.get_dirs(path,recursive=true): Gets all directories in the specified directory. If the second parameter is true, it will also return directories in subdirectories.
self.delete(path,non_empty=true): Deletes the specified directory. If the second parameter is true, it will delete all files inside first. You cannot delete non-empty directories.
Sprite:
bmp(arg1=nil,arg2=nil): Does a number of things. Example usages:
- bmp("Graphics/Pictures/test"): Loads the file in the path.
- bmp(32,32): Creates an empty 32x32 bitmap.
- bmp(-1,-1): Creates an empty width a width/height equal to the screen resolution.
- bmp(some_other_bitmap): Clones the passed bitmap and assigns it to self.
- bmp: Returns the current bitmap.
xyz=(args): Sets the x, y, and z coordinates. Usage:
- xyz = [10,10]: Does x = 10, y = 10, and z remains the same.
- xyz = [10,nil,5]: Does x = 10, y remains the same, and does z = 5.
center_origins: Centers a sprite by setting the origin points to half the width/height.
fullwidth: Returns the actual width of the sprite, taking zoom_x into account.
fullheight: Returns the actual height of the sprite, taking zoom_y into account.
TextSprite:
It's a Sprite, but contains some neat methods to make drawing text easier and faster.
initialize(viewport=nil,text=nil,width=-1,height=-1): Calls Sprite#initialize(viewport). Creates a new bitmap based on the passed width and height (by calling bmp(width,height)).
If text is a normal, valid array as you'd normally use with pbDrawTextPositions ([text,x,y,align,basecolor,shadowcolor]), it'll draw that afterward. Note that this doesn't have to be an array of arrays; if you have just one thing you want to draw, just one array is enough. This applies to all draw functions too.
clear: Clears the bitmap of any text that was on it.
draw(text,clear=false): Draws text to the bitmap. If clear is true, it'll clear the bitmap beforehand.
draw_outline(text,clear=false): Draws text to the bitmap with an outline. If clear is true, it'll clear the bitmap beforehand.
draw_ex(text,clear=false). Draws text to the bitmap. The align parameter is now the width one line may take up. If it exceeds that width, it'll continue drawing on a new line.
dispose: Disposes the Sprite and clears the bitmap.
SpriteHash:
This is your typical @sprites hash, but as a class. It contains a lot of useful functionality, such as being able to increase/decrease values such as x, y, z, opacity, and such -- relative. If you set the SpriteHash's x to 0 for example, all sprites would have a 0 increase, meaning they still have their own x value.
Worth noting is that you can have SpriteHashes inside SpriteHashes -- In fact, I use this, because it helps keeping things tidy and organized, and allows for quick and easy manipulating of sprites.
If possible, all keys are converted to symbols. Getting or setting them by key will automatically convert them, so you won't notice that.
[]: Returns the sprite in the specified key.
[]=: Sets the sprite in the specified key.
raw: Returns the internal hash in its raw form.
keys: Returns all keys of the hash.
size/length/count: Returns the size of the hash.
clone: Clones the hash.
add(key,value): Alias for []=.
delete(key): Deletes an entry by key.
each: Iterates over all entries and yields the values (not the key-values!).
update: Calls update() on each entry in the hash (if they respond to update()).
dispose: Disposes all sprites in the hash.
disposed?: Compatibility.
x=(value): Manipulates the x value.
y=(value): Manipulates the y value.
z=(value): Manipulates the z value.
visible=(value): Manipulates the visible value.
opacity=(value): Manipulates the opacity value.
hide(frames=16): Fades out all sprites (by decreasing their opacity) over frames frames.
show(frames=16): Fades in all sprites (by increasing their opacity) over frames frames.
Input:
self.any?: Returns true if any of the listed buttons are pressed (or mouse, if defined).
Stand-alone Methods (not part of a class/module):
showBlk: Shows a black overlay over a time frame of 17 frames.
hideBlk: Hides and disposes a black overlay over a time frame of 17 frames.
pbGetExpPercentage(pokemon): Returns the percentage EXP a Pok?mon has compared to the next level.
rand: Extends the functionality and support of the random number generator:
- rand(3..6): Generates a random number between 3 and 6, inclusive.
- rand(3...6): Generates a random number between 3 and 6, exclusive.
- rand(3): Generates a random number between 0 and 3, exclusive (0...3, 0..2).
- rand(3, 6): Generates a random number between 3 and 6, exclusive (3...6, 3..5).
- rand: Generates a random number between 0 and 2, exclusive (0...2, 0..1).
- rand(nil): Generates a random number between 0 and 2, exclusive (0...2, 0..1).
- rand(nil, 3): Generates a random number between 0 and 3, exclusive (0...3, 0..2).