RMRK is retiring.
Registration is disabled. The site will remain online, but eventually become a read-only archive. More information.

RMRK.net has nothing to do with Blockchains, Cryptocurrency or NFTs. We have been around since the early 2000s, but there is a new group using the RMRK name that deals with those things. We have nothing to do with them.
NFTs are a scam, and if somebody is trying to persuade you to buy or invest in crypto/blockchain/NFT content, please turn them down and save your money. See this video for more information.
Useful Scriptlets

0 Members and 1 Guest are viewing this topic.

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
This is a public list for scripts that are so small that the author feels that they should not bother giving them their own topic. Anybody can submit a script to this list and it will be added to the table.

List of Scriptlets:

Script Name
Change Leader
Game Over to Common Event
Stop Animation Actors
A little module addon
Author
|modern algebra
|modern algebra
|modern algebra
|Zeriab
Description
|Scroll through party members to choose leader (on map)
|Runs a common event at gameover, rather then going straight to gameover screen
|Allows you to have actors with stop animation if they are the leader
|Allows for you to set a default value to a public instance variable in it's definition


Change Leader
by modern algebra


It's a pretty common request, so I figured I'd post it. Basically, this script allows you to switch your lead character on the map by using the L and R buttons (To see what keys each button is attached to, press F1 while Test Playing and go to Page 3 in the little menu that pops up. By default, L and R are Q and W.)

Paste it into it's own script page in the editor, just above main.

Spoiler for Script:
Code: [Select]
class Scene_Map
 
  alias modern_algebra_change_leader_modification update
  def update
    modern_algebra_change_leader_modification
    if Input.trigger? (Input::R)
      # Remove the Lead Actor
      old_lead = $game_party.actors.shift
      # Add the old leader back into the party
      $game_party.add_actor (old_lead.id)
    end
    if Input.trigger? (Input::L)
      # Remove the last actor in the party
      new_lead = $game_party.actors.pop
      # Insert him as the lead actor
      $game_party.actors.unshift (new_lead)
      # Refresh $game_player to reflect new leader
      $game_player.refresh
    end
  end
end


Game Over Common Event
by modern algebra


Requested by zzzdude, I figured I would post it in case anyone else wanted it. Instead of calling up the GameOver scene when you die in battle, it calls a common event you specify. This can be used for an epilogue, or any kind of evented Game Over cutscene. Bear in mind that you do have to call the Game Over screen at the end, or else arrange an alternate game over. You will need to set everything in the common event. The script does not do anything a regular Game Over scene does.

Paste it into it's own script page in the editor, just above main.

Spoiler for Script:
Code: [Select]
class Scene_Battle
 
  # Aliases update to run before everything else in the method
  alias gameover_to_common_event update
  def update
    if $game_temp.gameover
      # Editable region. This number below is the ID of the common event you
      # wish to run. It is 1 by default, change it to suit your game.
      $game_temp.common_event_id = 1
      $scene = Scene_Map.new
      $game_temp.gameover = false
      return
    end
    gameover_to_common_event
  end
end

Stop Animation Actors
by modern algebra


Requested by Forcystus, this script allows you to set certain actors to have stop animation when they are the leader (thus on the game screen). Stop animation can be useful for flying units for instance, as it doesn't make any sense for them not to be flapping their wings at any time. It only effects the party member who is the leader. It has no influence on Caterpillar scripts.

Spoiler for Script:
Code: [Select]
#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
#  This class handles the player. Its functions include event starting
#  determinants and map scrolling. Refer to "$game_player" for the one
#  instance of this class.
#==============================================================================

class Game_Player
 
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  #  Place the IDs of the characters who should have stop animation active,
  #  like so:
  #
  #    STOP_ANIMATION_ACTORS = [1, 3]
  #
  #  That would make it so actors 1 and 3 have stop animation. By default,
  #  that would mean Aluxes and Cyrus would animate even when stopped
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
  STOP_ANIMATION_ACTORS = []
 
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Refresh
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  alias ma_player_stop_animation_refresh refresh
  def refresh
    ma_player_stop_animation_refresh
    @step_anime = STOP_ANIMATION_ACTORS.include? ($game_party.actors[0].id)
  end
end

A Little Module Addon
by Zeriab


Here's a litte addition to the module I have made ^^
Spoiler for Code:
Code: [Select]
class Module
  def attr_sec_accessor(sym, *args, &block)
    if block.nil? # default actions
      args = [0] if args.size == 0 #default = 0
      if args.size < 2 # One pair
        attr_writer sym
        attr_sec_reader sym, *args
      else # loads of methods followed by a default value.
        default = args[-1]
        syms = [sym].concat(args)
        syms.pop
        for sym in syms
          attr_writer sym
          attr_sec_reader sym, default
        end
      end
    else # when a block is given
      # currently just pair sematics
      args.unshift(sym)
      i = 0
      while i < args.size
        attr_writer args[i]
        attr_sec_reader args[i], args[i+1]
        i += 2
      end
    end
  end
 
  def attr_sec_reader(sym, default = 0)
    sym = sym.id2name
    string = "def #{sym};" +
             "  @#{sym} = #{default}  if @#{sym}.nil?;" +
             "  @#{sym};" +
             "end;"
    module_eval(string)
  end
end

Spoiler for Simple Version:
Code: [Select]
class Module
  def attr_sec_accessor(sym, default = 0)
    attr_writer sym
    attr_sec_reader sym, default
  end
 
  def attr_sec_reader(sym, default = 0)
    sym = sym.id2name
    string = "def #{sym};" +
             "  @#{sym} = #{default}  if @#{sym}.nil?;" +
             "  @#{sym};" +
             "end;"
    module_eval(string)
  end
end

You can use it like this:
Code: [Select]
attr_sec_accessor :method, default
It works pretty much like the attr_accessor except that you can only do method creation per call.
method is the method name.
default is the default value the method returns. (If it has not been written to yet.)

An usage example:
Code: [Select]
class Foo
  attr_sec_accessor :visible, false
  attr_sec_accessor :data, 'Data_Foo.new'
end

This way you'll have that the default variable of visible is false.
If it is called before you write to the variable you will get 'false' rather than 'nil'
One thing to note is that lazy instantiation is used.
Let us assume that Data_Foo is a heavy object. It is only created if you try to read what the data attribute contains before you have written to it. More specifically. If the value of data attribute is nil then it is change to be what the default value is.

The code in the example is converted to this:
Code: [Select]
class Foo
  def visible=(value)
    @visible = value
  end
  def visible
    @visible = false  if @visible.nil?
    @visible
  end
 
  def data=(value)
    @data = value
  end
  def data
    @data = Data_Foo.new  if @data.nil?
    @data
  end
end
One thing to notice is that you must put '' around Data_Foo.new to make it a string. Otherwise you will get an error in almost all cases.

The greatest use will probably be if you want to add information to hidden classes. Let's for an example take RPG::Actor.
You can use its initialize method to set the default value by default because it won't be called automatically.
Other than that it will hopefully make the coder shorter and easier to understand.

Naturally. Only use this functionality where it is needed or where it can have a positive use ^_^

*hugs*
 ~ Zeriab

« Last Edit: February 19, 2008, 06:24:04 PM by modern algebra »

**
Rep:
Level 87
thankyou so much.... ive been looking for a script like this for ages!

pokeball :)OfflineMale
********
Cheese
Rep:
Level 95
?
such a useful script should be in the database.  ;D
Watch out for: HaloOfTheSun

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
I'm thinking of making this topic into a scriptlet thread. Whenever I write minor scripts I will post them here. Added Common Event Game Over.

Is this a good idea? It would kind of be like Blizzard's Tons of Addons, I guess, except it would have much less and lower quality work :P

I don't really want to make a separate thread for every 20 line script I write.
« Last Edit: August 16, 2007, 05:08:49 AM by modern algebra »

******
Revolution is not a bed of roses.
Rep:
Level 91
Project of the Month winner for July 2009
Nice simple scripts, well done, keep up the good work :)

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
Updated to include items which can teach skills to targeted actors.

« Last Edit: November 02, 2007, 08:19:45 PM by modern algebra »

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
Here's a litte addition to the module I have made ^^
Spoiler for Code:
Code: [Select]
class Module
  def attr_sec_accessor(sym, *args, &block)
    if block.nil? # default actions
      args = [0] if args.size == 0 #default = 0
      if args.size < 2 # One pair
        attr_writer sym
        attr_sec_reader sym, *args
      else # loads of methods followed by a default value.
        default = args[-1]
        syms = [sym].concat(args)
        syms.pop
        for sym in syms
          attr_writer sym
          attr_sec_reader sym, default
        end
      end
    else # when a block is given
      # currently just pair sematics
      args.unshift(sym)
      i = 0
      while i < args.size
        attr_writer args[i]
        attr_sec_reader args[i], args[i+1]
        i += 2
      end
    end
  end
 
  def attr_sec_reader(sym, default = 0)
    sym = sym.id2name
    string = "def #{sym};" +
             "  @#{sym} = #{default}  if @#{sym}.nil?;" +
             "  @#{sym};" +
             "end;"
    module_eval(string)
  end
end

Spoiler for Simple Version:
Code: [Select]
class Module
  def attr_sec_accessor(sym, default = 0)
    attr_writer sym
    attr_sec_reader sym, default
  end
 
  def attr_sec_reader(sym, default = 0)
    sym = sym.id2name
    string = "def #{sym};" +
             "  @#{sym} = #{default}  if @#{sym}.nil?;" +
             "  @#{sym};" +
             "end;"
    module_eval(string)
  end
end

You can use it like this:
Code: [Select]
attr_sec_accessor :method, default
It works pretty much like the attr_accessor except that you can only do method creation per call.
method is the method name.
default is the default value the method returns. (If it has not been written to yet.)

An usage example:
Code: [Select]
class Foo
  attr_sec_accessor :visible, false
  attr_sec_accessor :data, 'Data_Foo.new'
end

This way you'll have that the default variable of visible is false.
If it is called before you write to the variable you will get 'false' rather than 'nil'
One thing to note is that lazy instantiation is used.
Let us assume that Data_Foo is a heavy object. It is only created if you try to read what the data attribute contains before you have written to it. More specifically. If the value of data attribute is nil then it is change to be what the default value is.

The code in the example is converted to this:
Code: [Select]
class Foo
  def visible=(value)
    @visible = value
  end
  def visible
    @visible = false  if @visible.nil?
    @visible
  end
 
  def data=(value)
    @data = value
  end
  def data
    @data = Data_Foo.new  if @data.nil?
    @data
  end
end
One thing to notice is that you must put '' around Data_Foo.new to make it a string. Otherwise you will get an error in almost all cases.

The greatest use will probably be if you want to add information to hidden classes. Let's for an example take RPG::Actor.
You can use its initialize method to set the default value by default because it won't be called automatically.
Other than that it will hopefully make the coder shorter and easier to understand.

Naturally. Only use this functionality where it is needed or where it can have a positive use ^_^

*hugs*
 ~ Zeriab
« Last Edit: October 16, 2007, 07:49:45 AM by Zeriab »

*
A Random Custom Title
Rep:
Level 96
wah
SO CONFUSED ;___;

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
Nah, it seems pretty good. If I understand, it initializes a class variable immediately, which means:

Code: [Select]
attr_sec_reader   :wait, 10

is equivalent to:

Code: [Select]
attr_reader   :wait
alias modern_algebra_possibly_wrong_initialize initialize
def initialize
    modern_algebra_possibly_wrong_initialize
    @wait = 10
end

Of course, it is possible that I misread that, and that's not what it does at all :'(

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
Hey modern. You are unfortunately wrong.
Code: [Select]
attr_sec_reader   :wait, 10
is equivalent to
Code: [Select]
def wait
  @wait = 10  if @wait.nil?
  @wait
end

And we have that
Code: [Select]
attr_sec_accessor :wait, 10
is equivalent to
Code: [Select]
def wait=(value)
  @wait=value
end

def wait
  @wait = 10  if @wait.nil?
  @wait
end


@moo: This is intended for intermediate scripters and up ^_^

*
A Random Custom Title
Rep:
Level 96
wah
Well, why not make me an intermediate scripter, then? >:(

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
 :'(

Ahh, well: lazy initialization then. It has the same effect. :P
So close, yet so far away...

*
A Random Custom Title
Rep:
Level 96
wah
Manipulating Zeriab to make me at least an intermediate scripter: So close but he's so far away. ;9

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
There is one major difference modern.
It is the fact that the default object is only created if you try to read the attribute before you write to it.
While this doesn't really matter if you just have an integer as default value it could easily have an effect if the default value is an object which is costly to make. You won't automatically create it. You just create it when it is needed and you may not have to create it at all.

There is another bonus as well. If you want to add attributes to the hidden classes. Let's for example take RPG::Actor. You can just add them and know that they will have the default value you specify.
If you put the defaults in an initialize method then you will have to call that method on each object because it is not called automatically. (The RPG data classes are saved into files. So if you want the values to be in the files you will either have to hack the RMXP editor or hack the files.)

@moo:
You can try experimenting with it and see if you can understand it that way ^^
For your convenience I'll keep the simple version along with a more advanced version.


With the new version you can for example do this:
Code: [Select]
class Foo
  attr_sec_accessor :hp, :sp, :str, :spr, :spd, :dex, 0
end
The last argument is the default value. Each argument before is an attribute which will have the specified default argument.
Just a quick way to set several methods to the same default value ^_^

Oh, and you can also do this:
Code: [Select]
class Foo
  attr_sec_accessor (:visible, false, :data, Data_Foo.new, :message,  "I am Foo") {'pairs'}
end

In this case you will specify them as pairs. All you have to do is to give a block. While I have put 'pairs' in my example this is not needed. You just have to give a block and the arguments will be assumed to be in pairs of (method, default_value)

Use them when applicable
« Last Edit: October 14, 2007, 06:37:57 AM by Zeriab »

******
Revolution is not a bed of roses.
Rep:
Level 91
Project of the Month winner for July 2009
* Falcon pretends to understand

Sweet. I dunno if this should be in the database, so I'll put it in.

*
A Random Custom Title
Rep:
Level 96
wah
Omfg if Falcon can't understand the only person this is useful for is only Zeriab and Blizzard.

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
Blizzard's comment on this:
Quote from: Blizzard
<3

I guess I haven't explained it enough. Or maybe it's more difficult to understand than I though :/
The thing is. You don't really have to understand how it works. I just have to understand how to use it ^_^
There is another thing which you have used loads while you probably don't understand how it works, but you understand how to use it.
That is hashes.

I updated the first post.
@Falcon: Tell me if you still can't understand how to use it.

I am not really sure whether it should be in the database or not. It's on the edge, but I guess it's fine.

******
Revolution is not a bed of roses.
Rep:
Level 91
Project of the Month winner for July 2009
Oh, you wrote it in English now :D

Correct me if I'm wrong, but this can auto set values, right?

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
I am unsure what you mean with auto setting values :/
So I can't really say whether it can or not :P

Btw. here's an explanation I wrote on a different forum
Quote
Do you know the attr_accessor?
It is used to specify attributes of a class.
You could for example have attr_accessor :data
Now you can read and write to the data attribute.
You can read using object.data and write using object.data = value.
That is how the attr_accessor works. There are loads of example in the default scripts of this.

The thing is. The default value is always nil.
My attr_sec_accessor allow you to specify the default value.
Let us for example have attr_sec_accessor :data, 2.
You can read from and write to the attribute just like with attr_accessor. The difference is now that the default value is 2 instead of nil.

It is just much quicker using it than to write it all out.
You can say it generates most of the recurrent code for you.
Also, having less code makes it easier to debug.

Then there is the stuff about it using lazy instantiation, but you don't need to understand what this means to use it.

Edit:
Hmm.... A quick survey. How would you go about making a string as a default value?
« Last Edit: October 17, 2007, 10:58:22 AM by Zeriab »

******
Revolution is not a bed of roses.
Rep:
Level 91
Project of the Month winner for July 2009
By auto set value, I meant it specifies the default value.

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
Ah. Yes. That is indeed the purpose XD
The purpose of having attributes where you can specify the default value. (So that it is something else than nil)

*
A Random Custom Title
Rep:
Level 96
wah
Edit:
Hmm.... A quick survey. How would you go about making a string as a default value?
'Insert string here' ? ?_?

*
Rep:
Level 97
2014 Most Unsung Member2014 Best RPG Maker User - Engine2013 Best RPG Maker User (Scripting)2012 Most Mature Member2012 Favorite Staff Member2012 Best RPG Maker User (Scripting)2012 Best MemberSecret Santa 2012 ParticipantProject of the Month winner for July 20092011 Best Use of Avatar and Signature Space2011 Best RPG Maker User (Scripting)2011 Most Mature Member2011 Favourite Staff Member2011 Best Veteran2010 Most Mature Member2010 Favourite Staff Member
I'd imagine it would be something like:

attr_sec_reader :test, "'string'"

*
? ? ? ? ? ? ? ? ? The nice kind of alien~
Rep:
Level 92
Martian - Occasionally kind
@moo: You misunderstood ^_^

@modern: Yup, you are indeed right. Thanks ^^

Here's another version.
attr_sec_reader :test, '\'string\''

It should be slightly more efficient, but the difference should be insignificant.
Btw. note the difference between
attr_sec_reader :test, ' "string" '
and
attr_sec_reader :test, " 'string' "

I have placed spaces so you can better see the difference. They are not necessary. Also notice that you can do stuff like this:
attr_sec_reader :test, ' "test no #{$game_variables[7]}" '

*
Full Metal Mod - He will pillage your women!
Rep:
Level 93
The RGSS Dude
It's shiney. O_o

I like. ^_^
"The wonderful thing about Tiggers
Is Tiggers are wonderful things
Their tops are made out of rubber
Their bottoms are made out of springs

They’re bouncy, trouncy, flouncy, pouncy
Fun, fun, fun, fun, fun!
But the most wonderful thing about Tiggers
Is I’m the only one, I’m the only one."