• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Marin's Scripting Utilities

971
Posts
7
Years
  • Age 21
  • 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(n): Breaks self into subarrays every n elements.
breakup!(n): Replaces self with the result of breakup(n).
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(n): 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:
  • 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.
bmp=(path): Alternative to bmp(path). Usage: bmp = "Graphics/Pictures/test"
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.
xyz: Returns the x, y, and z coordinates in the xyz=(args) format. (e.g. Sprite#xyz #=> [10,0,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).
 
162
Posts
7
Years
  • Age 35
  • Seen today
Hi, I am using this script, but unfortunately I have an error when I use slot machines.

Can you help me :C?

ERROR.png
 
971
Posts
7
Years
  • Age 21
  • Seen Nov 28, 2022
Hi, I am using this script, but unfortunately I have an error when I use slot machines.

Can you help me :C?

ERROR.png

Around line 161, you should see def shuffle. Replace that section, all the way until the end about 13 lines below def shuffle, with this:
Code:
  # Shuffles the order of the array
  def shuffle
    indexes = []
    new = []
    while new.size != self.size
      # Weird way of calling rand for compatibility
      # with Luka's scripting utilities
      i = Object.method(:rand).call(self.size)
      if !indexes.include?(i)
        indexes << i
        new << self[i]
      end
    end
    return new
  end

Line 157 should be return self[rand(self.size)]. Replace that with this:
Code:
return self[Object.method(:rand).call(self.size)]

Alternatively, you can redownload the script as I've just fixed this in the download.
 
1
Posts
4
Years
  • Age 18
  • Seen Apr 17, 2020
hey i could use some help understanding this coding thing i'm new so can you help me with it, i don't mind if it is only one tip just please help me out. Thank you :)
 

DarrylBD99

Content Creator and Game Developer
321
Posts
3
Years
When I addded the scripting utilities into my game (for the DPPT Pause Menu), it caused my game to lag really badly whenever transferring to another map and brings up the "Script is taking to long to load" error.
 
971
Posts
7
Years
  • Age 21
  • Seen Nov 28, 2022
When I addded the scripting utilities into my game (for the DPPT Pause Menu), it caused my game to lag really badly whenever transferring to another map and brings up the "Script is taking to long to load" error.

That has nothing to do with this resource. And if it does, an error message would go a long way.
 

NightLullaby

Pokémon Epoch Co-Director
2
Posts
4
Years
Every time I try running my game after installing this, it works fine until I press continue, then the game screen goes black without ever giving an error message.
 

FL

Pokémon Island Creator
2,443
Posts
13
Years
  • Seen Apr 16, 2024
Good script! You have some neat methods here!

But I didn't like your "rand()" (without parameters). The ruby one:

If called without an argument, or if max.to_i.abs == 0, rand returns a pseudo-random floating point number between 0.0 and 1.0, including 0.0 and excluding 1.0.
You override the ruby one to only return integers 0 or 1.

Besides this, great job!
 
Last edited:

FL

Pokémon Island Creator
2,443
Posts
13
Years
  • Seen Apr 16, 2024
I found two harmless issues: NilClass.numeric? is defined twice. I believe than "return" in
Code:
  if $Rescue
    raise
    return
  end
does nothing.

I personally didn't like that you added "empty?" method to all classes (yeah, for json but you can do other way). This surpress some expected crashes.

Some questions:

1. Why you redefine Array.empty? ?
2. Why you cloned only the second arg on Array.swap (besides the array clone)?
3. Why you cloned or replace the array on Array.swap? Seems that a Bubblesort on a big array would have a good time gain using swap.
 

FL

Pokémon Island Creator
2,443
Posts
13
Years
  • Seen Apr 16, 2024
Your get_variables crashes when I have global a variable that isn't attr_reader/attr_accessor.

I change it to
Code:
    return instance_variables.map do |attribute|
      { attribute => self.instance_variable_get(attribute) }
    end
 
2
Posts
2
Years
  • Age 23
  • Seen Jun 18, 2022
do you know how to fix this error
[Pokémon Essentials version 18]
[Generation 8 Project v1.1.0]

Exception: TypeError
Message: no implicit conversion of String into Integer

Backtrace:
376:Marin's Scripting Utilities:190:in `swap'
008:PluginManager:593:in `block (2 levels) in sortLoadOrder'
008:PluginManager:578:in `each'
008:PluginManager:578:in `block in sortLoadOrder'
008:PluginManager:575:in `each'
008:PluginManager:575:in `sortLoadOrder'
008:PluginManager:625:in `getPluginOrder'
008:PluginManager:683:in `runPlugins'
377:Main:28:in `mainFunctionDebug'
377:Main:18:in `block in mainFunction'
 
Back
Top