[XP][VX][VXA] Fade Events

Started by Heretic86, April 23, 2012, 05:50:00 pm

Previous topic - Next topic

Heretic86

April 23, 2012, 05:50:00 pm Last Edit: April 25, 2012, 04:07:32 pm by Heretic86
Fade Events
Authors: Heretic
Version: 1.0
Type: Fade Events with Duration
Key Term: Misc Add-on



Introduction

This script will allow you to Fade Events in and out gradually.  The Default Options allow you to change an events opacity, but does not allow you to make a gradual transition.  Thus, if you wanted to have an Event that "dissolved", "teleported" or "flickered", you would have to use quite a few Change Opacity commands, then use Wait after each and every Change Opacity command.  Sorry to say, but I think that is a pain in the behind.  This script offers an easier way to accomplish that same effect by using one line of script: fade_event(new_opacity, duration).  Short Fades can be accomplished without this script by adjusting the opacity every frame for a few frames, but if you need rather long Fades that take a lot of frames, then this is the script for you!


Features


  • Saves Time by changing Opacity for you Automagically

  • Super Duper easy to use

  • Only ONE Command to Remember

  • No Configuration Necessary

  • High Degree of Compatability

  • Reported to me to be compatible with VX and VXA as well




Screenshots
Spoiler: ShowHide








Demo

http://www.775.net/~heretic/downloads/rmxp/fade_event/fade_event.exe


Script

Fade Event (with Developer Errors)
(has a little more code, lets you know if you do something wrong)

Spoiler: ShowHide
# Name: Fade Event (With Developer Errors)
# Author: Heretic
# Ver: 1.0
# Date: Yes, please, but with a pretty girl! (yes, that was a joke!)
# Timestamp: Apr 23rd, 2012
#
# FADE EVENT is Super simple to use.  Put above Main somewhere. 
#
# Pretty much anywhere between Scene_Debug and Main should be fine.
#
# In the Editor, in Edit Events, go to Set Move Route, and hit Script
# in that window, enter "fade_event(255, 20)" without the quotes
# and adjust 255 for Not Transparent to 0 for Fully Invisible
# the second number is the number of Frames for the Transition
#
# Note: 1 Second in RMXP is 20 Frames

module Fade_Events #(for including in any new or custom classes)
  #--------------------------------------------------------------------------
  # * Fade Event - (Opacity 0 to 255, Duration in Frames)
  #--------------------------------------------------------------------------
 
  def fade_event(arg_opacity_target, arg_opacity_duration)
    if arg_opacity_target.nil? or arg_opacity_duration.nil?
      print "fade_event(opacity, duration) expects Two Arguments\n"
      return
    elsif not arg_opacity_target.is_a? Numeric
      print "\"", arg_opacity_target, "\" is not an Number!\n",
            "Both Arguments should be Numbers!\n\n",
            "I.E. fade_event(255,20)"
      return
    elsif not arg_opacity_duration.is_a? Numeric
      print "\"", arg_opacity_duration, "\" is not an Number!\n",
            "Both Arguments should be Numbers!\n\n",
            "I.E. fade_event(255,20)"
      return
    elsif arg_opacity_target < 0 or arg_opacity_target > 255
      print "\"", arg_opacity_target, "\" Opacity Out of Range\n",
            "Valid Range: 0 - 255"
    elsif arg_opacity_duration < 0
      print "\"", arg_opacity_target, "\" Duration Out of Range\n",
            "Valid Range: Any Positive Number or 0"
    end
   
    self.opacity_target = arg_opacity_target * 1.0
    self.opacity_duration = arg_opacity_duration.abs
    if self.opacity_duration == 0
      self.opacity = self.opacity_target.clone
    end
  end

#-----------------------------------------------------------------------------
#  * Change Event Opacity
#-----------------------------------------------------------------------------
 
  def change_event_opacity
    # Manage change in Event Opacity Level
    if @opacity_duration >= 1
      d = @opacity_duration
      @opacity = (@opacity * (d - 1) + @opacity_target) / d
      @opacity_duration -= 1
    end
  end

end

#==============================================================================
# ** Game_Character
#------------------------------------------------------------------------------

class Game_Character
 
  attr_accessor     :opacity_target
  attr_accessor     :opacity_duration
  attr_accessor     :original_opacity
 
  unless self.method_defined?('fade_events_initialize')
    alias fade_events_initialize initialize
  end

  def initialize
    # Initilaize Original First
    fade_events_initialize
    # Added Properties
    @opacity_target = 255
    @opacity_duration = 0
    @original_opacity = @opacity
  end

  # IF the method hasn't been defined yet
  unless self.method_defined?('fade_events_update')
    alias fade_events_update update
  end
 
  # Redefine Updater
  def update
    # Run Original First
    fade_events_update
    if @opacity_duration >= 1
      # Manages the Events Current Opacity
      change_event_opacity
    end
  end

  # Check if the Fade_Event is already Defined (prevents errors)
  unless Game_Character.included_modules.include? (Fade_Events)
    # Make the functions inside the Module available to this class
    include Fade_Events
  end
 
end

class Interpreter
  def fade_event(opacity, duration)
    print "Please call \"fade_event\" from\n",
           "\"Edit Event\" -> \"Set Move Route\" -> Scripts Window\n",
           "instead of \n\"Edit Event\" -> Scripts Window"
  end
end 



Fade Event (no Developer Errors)
(has less code, no errors displayed if you do something wrong, so you're on your own)

Spoiler: ShowHide
# Name: Fade Event (No Developer Errors)
# Author: Heretic
# Ver: 1.0
# Date: Yes, please, but with a pretty girl! (yes, that was a joke!)
# Timestamp: Apr 23rd, 2012
#
# FADE EVENT is Super simple to use.  Put above Main somewhere. 
#
# Pretty much anywhere between Scene_Debug and Main should be fine.
#
# In the Editor, in Edit Events, go to Set Move Route, and hit Script
# in that window, enter "fade_event(255, 20)" without the quotes
# and adjust 255 for Not Transparent to 0 for Fully Invisible
# the second number is the number of Frames for the Transition
#
# Note: 1 Second in RMXP is 20 Frames

module Fade_Events #(for including in any new or custom classes)
  #--------------------------------------------------------------------------
  # * Fade Event - (Opacity 0 to 255, Duration in Frames)
  #--------------------------------------------------------------------------
 
  def fade_event(arg_opacity_target, arg_opacity_duration)
    self.opacity_target = arg_opacity_target * 1.0
    self.opacity_duration = arg_opacity_duration.abs
    if self.opacity_duration == 0
      self.opacity = self.opacity_target.clone
    end
  end

#-----------------------------------------------------------------------------
#  * Change Event Opacity
#-----------------------------------------------------------------------------
 
  def change_event_opacity
    # Manage change in Event Opacity Level
    if @opacity_duration >= 1
      d = @opacity_duration
      @opacity = (@opacity * (d - 1) + @opacity_target) / d
      @opacity_duration -= 1
    end
  end

end

#==============================================================================
# ** Game_Character
#------------------------------------------------------------------------------

class Game_Character
 
  attr_accessor     :opacity_target
  attr_accessor     :opacity_duration
  attr_accessor     :original_opacity
 
  unless self.method_defined?('fade_events_initialize')
    alias fade_events_initialize initialize
  end

  def initialize
    # Initilaize Original First
    fade_events_initialize
    # Added Properties
    @opacity_target = 255
    @opacity_duration = 0
    @original_opacity = @opacity
  end

  # IF the method hasn't been defined yet
  unless self.method_defined?('fade_events_update')
    alias fade_events_update update
  end
 
  # Redefine Updater
  def update
    # Run Original First
    fade_events_update
    if @opacity_duration >= 1
      # Manages the Events Current Opacity
      change_event_opacity
    end
  end

  # Check if the Fade_Event is already Defined (prevents errors)
  unless Game_Character.included_modules.include? (Fade_Events)
    # Make the functions inside the Module available to this class
    include Fade_Events
  end
 
end



Instructions

Just put the code above main somewhere...


Compatibility

No Incompatability Issues, as far as I know...


Credits and Thanks


  • I'd like to thank the Academy, Chucky Cheese, and inspiration from Chuck Norris!

  • Thanks to everyone who has been helping me improve my scripting skills!




Author's Notes

Thursday is Garbage Day so don't forget to take out the Trash!  Oh, wait, that isnt really a note related to this Script!  How embarassing!


---

I dont own copies of VX or VXA, but it has been reported to me that this script is compatible with VX and VXA as well!  Please advise if there are any issues and I'll see what I can do for you.
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

Calintz

this was a wonderful idea. +1

LiTTleDRAgo

Quote from: Heretic86 on April 23, 2012, 05:50:00 pm
Actually, brief side note.  I dont have RMVX or VXA so Im not sure if this script would be compatible with those or not, but if someone feels like testing it out and posting results...


and yes, it's compatible with VX - VXA

G_G

April 23, 2012, 10:23:26 pm #3 Last Edit: April 23, 2012, 10:36:14 pm by game_guy
Snippet so you can use the code in Event Commands > Script Call rather than doing it through the Set Move Route command.
class Interpreter
 def fade_event(opacity, duration, event = 0)
   character = get_character(event)
   return true if character == nil
   character.fade_event(opacity, duration)
 end
end


Called in a script call window.
fade_event(opacity, duration, target_event = 0)
target_event = -1 (the player), 0 (event the script was called in), or other_event_id
If target_event is omitted, it'll target the event that the script call was placed in.


And instead of checking to see if the opacity value is less than 0 or greater than 255, its a simple array check like so.
arg_opacity_target = [[arg_opacity_target, 0].max, 255].min

Which clamps the number between 0 and 255.

Also no offense, but those warning messages and parameter checks are a little useless in my opinion. You clearly state in your instructions what to use for your parameters, so if the end-user of a script can't read some simple instructions, its their own fault if they can't get the script to work. I know you're trying to make this user-friendly but your code could be so much cleaner if you got rid of those messages.

One more thing, instead of checking to see if arg_opacity_duration is a positive number, just use the abs method or "absolute" method, which is the placement away from zero. Always returns a positive number.
arg_opacity_duration = arg_opacity_duration.abs

ForeverZer0

You don't need to clamp opacity values, they are already by the underlying library.

Quote from: The RMXP Help Manualopacity
The sprite's opacity (0-255). Values out of range are automatically corrected.


sprite = Sprite.new
sprite.opacity = -34345
p sprite.opacity
sprite.opacity = 63728374328472
p sprite.opacity


I agree with G_G about the unneeded checks. They are nothing more than an extra lines of code to perform.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Heretic86

Okie dokie, I'll add those things in when I get two minutes free.  Dogs are telling me its "take a dump o'clock", so I have to take em for a walk...
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

Heretic86

So I've been thinking about your guys suggestions.  And Im not the most experienced scripter, so I am listening to your advice.  So no offense taken.

In order to make a script both "User Friendly" and "Compact", which appears to be a balance between adding extra code to provide those messages, and make it run fast, what would be the "Official" way to do it?  I mean by the book?  I have no books to reference, just mostly the internet and listening to what you guys have to say.

Here is my therory.  If "User Friendly" has to run extra code and makes a script run like garbage because it causes a bigtime performance hit, the extra User Friendliness code has to go.  Performance and stability is top priority.  Such as doing User Friendly error messages in a repeating loop.  Bad Idea.  I get that.  But for a script that executes only every so often, and printing errors doesnt cause any noticable performance hits (yeah I know they add up quickly), is it better to let the person using the script know they've messed up by not clamping, even if clamping would fix that problem before it is an issue, or to allow them to continue to falsely "think" that 320000000 is within an acceptable range of Opacity?

Admitted, half the code is just there to advise the User on potential errors, and the Interpreter one doesnt run at all unless it is called from the wrong script window.  Now, on the Interpreter error vs just make it work philosophy, would it make more sense to a user to run a script from only one place, or from where ever they want, even though it requires additional arguments (parameters) to be passed to the method?  Im trying to keep it simple for stupid (k.i.s.s.) as I am sure that many of the people that use RM arent scripters, and many when they first start out dont even know how to insert a new script.  Everyone has to start somewhere, which means we were all of us "stupid" at some point.

Help me out with this.  I've got what seems like slightly conflicting advice.  One says get rid of as much extra code as possible, other says auto-fix all user created problems with bits of extra code.  Both arguments are valid.  What is the Balance I should be aiming for?
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)

Blizzard

There are two philosophies on this: The "professional" one and the "amateur" one.

The professional one says that you should make certain errors obvious to the user. Since this is the "professional" one, it's usually used in professional software.

The amateur one says who cares. If the user doesn't properly use the script, it's their fault. This philosophy is basically used all over the RMXP community. And it's ok. If there were that many additional error messages in scripts, things would really become too cluttered. e.g. Your example with opacity. If somebody is stupid enough (no offense meant here for the users) to try to set the opacity to 32000000 (regardless of the fact that RMXP sliently clamps the value within the range), then it's their own fault. You can't expect that something works if you just keep "pressing random buttons".

As for your specific problem, remove all extra code. You should add in the instructions how the script is used and what kind of limitations there are. People who try to use the scripts just like that without reading the instructions (you wouldn't believe how many people don't read them) usually end up giving up on their projects anyway. They are not even committed enough to their projects to read a few sentences. Besides, even if you added error messages and all, they will still post and complain that your script isn't working and that it's giving them errors.

So my personal recommendation: No extra checks unless you think it really, really makes sense. No extra code. Use instructions. If people ask stupid questions, tell them to read the instructions, it's all there. Everybody in the RM community does it that way and it works best from what I have seen in my 6 years in the community.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

LiTTleDRAgo

Quote from: Heretic86 on April 24, 2012, 02:42:44 amIm trying to keep it simple for stupid (k.i.s.s.) as I am sure that many of the people that use RM arent scripters,


it will be simpler to correcting the user simple mistake silently in the shadows,
if the mistake is fatal, that's a different story

enterbrain does it too doesn't it?


  • look at the draw_text in vx / vxa
    even if you typed an integer in draw_text, it will automatically changed to string

  • opacity will automatically corrected if out of range

  • etc, etc, etc



if something trivial triggers an error / message, the user probably will think your script is annoying

Quote from: Blizzard on April 24, 2012, 04:25:17 am
There are two philosophies on this: The "professional" one and the "amateur" one.


I'll probably go with "amateur" one

Blizzard

Everybody in the RM community does and I think it's the best one for something like this. Unless you are being paid, there is not really a point for the professional one.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

I really only see the purpose if you are developing some type of framework or API that users will be compounding upon. Other than, simply telling the user how to make the call is sufficient.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

Yes, a framework or some sort of SDK would be the only exceptions to the rule. Maybe even certain development/scripting tools. e.g. When using a console, there should be proper error handling for syntax errors and such. But this still comes down to simple and generic error handling instead of covering "all possible cases".
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Heretic86

Ok, temporary edit to original post.  I'll update the demo when I've come up with a product that everyone can be happy.

I put two versions up, just temporarily.  One has the errors in it (original), the other has all the extra code pulled out.  Both versions I changed "self.opacity_duration = arg_opacity_duration" to "self.opacity_duration = arg_opacity_duration.abs" to do a quick fix for negative values.  I did not add anything to redirect from Interpreter if the script is called, as it is an extra optional argument, so the game will crash if fade_event(O, D) is called from non Move Route Script window.  It just seems to me that if I add in extra args, one window expects them to be there, one does not, it has too much potential to confuse even pretty sharp users of the script.  So two expected arguments that have to be called from the correct Script Window.  And I think that is the way it is going to stay.  If you use it for yourself, and already know how to script, you can modify it to suit your needs.

Since there werent really any functional differences, excluding .abs addition I didnt think that it warranted a version change, unless you guys advise otherwise.  Oh, and should I put anything in for Legal junk?  Its too small of a script to say "bla bla bla, mention me in the credits, free for commercial use" for me to really care, but what do you guys think about the Legals of it?

Any other suggestions? 
Current Scripts:
Heretic's Moving Platforms

Current Demos:
Collection of Art and 100% Compatible Scripts

(Script Demos are all still available in the Collection link above.  I lost some individual demos due to a server crash.)