[RMXP] When two scripts alias one something....

Started by Seox, June 10, 2009, 09:36:12 pm

Previous topic - Next topic

Seox

Hiya.

My CBS and my ammo requirements scripts don't like each other. Ever since I got the former, the latter stopped working.

Now that I know wth I'm doing with scripts, I've realized that both alias:

#----------------------------------------------------------------------------
# Begin Scene_Battle
#----------------------------------------------------------------------------
class Scene_Battle
  # Alias Methods
  alias syn_scene_battle_range make_basic_action_result
  alias syn_scene_battle_skill make_skill_action_result
  #----------------------------------------------------
  # Alias the Attacking method
  #----------------------------------------------------
  def make_basic_action_result
    # Gather the current Ammo Cost
    gather_ammo_cost = Ammo::Range_weapons_id[@active_battler.weapon_id]
    # Gather the Current Ammo
    gather_ammo = Ammo::Range_ammo_id[@active_battler.weapon_id]
    # Check if the Active Battler is attacking and if they are using a ranged weapon
    if @active_battler.current_action.basic == 0 and Ammo::Range_weapons_id.has_key?(@active_battler.weapon_id)
      # Check the Ammo Count
      if $game_party.item_number(gather_ammo) >= gather_ammo_cost
        # Sufficient Ammo, remove item
        $game_party.lose_item(gather_ammo,gather_ammo_cost)
        syn_scene_battle_range
      else
        # Insufficient Ammo
        @help_window.set_text("#{@active_battler.name}'s weapon clicks - Empty! ", 1)
      end
      # Call Default Code
    else
      syn_scene_battle_range
    end
  end


Long story short, is there a way to edit them so that they don't clash, and I can use both at once? To my knowledge, there is not - but I'm sure that someone around here knows of a way.

Please and thank you ^_^
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

Sure, just rename one of the clashing aliases. Keep in mind to search the script to find all places where that alias was called (usually only one place) and change it as well.
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.

Seox

Oh, and that would still work properly? I still don't fully understand aliasing- I know that it renames a method, but not specifically what the result is. My impression is that it allows you to edit the method without killing it, allowing you to retain the old method. However, under that theory, two methods couldn't alias the same thing.

But all I have to do is rename the alias? They don't go by the same name, they just alias the same thing. Therefore, renaming it shouldn't really make a difference, unless I rename the aliased method - but then it doesn't work at all. Meh.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

G_G

aliasing does not rename methods. I'll explain exactly what they do. Well most of it.

When you alias this is what it looks like
alias add_something_later method

What it does is store the old method of "method" and then you can call the old "method" later when needed. So instead of doing something like this

class Scene_Title
 def command_new_game
   # THIS IS A NEW LINE BELOW
   $something = something.new
   # Play decision SE
   $game_system.se_play($data_system.decision_se)
   # Stop BGM
   Audio.bgm_stop
   # Reset frame count for measuring play time
   Graphics.frame_count = 0
   # Make each type of game object
   $game_temp          = Game_Temp.new
   $game_system        = Game_System.new
   $game_switches      = Game_Switches.new
   $game_variables     = Game_Variables.new
   $game_self_switches = Game_SelfSwitches.new
   $game_screen        = Game_Screen.new
   $game_actors        = Game_Actors.new
   $game_party         = Game_Party.new
   $game_troop         = Game_Troop.new
   $game_map           = Game_Map.new
   $game_player        = Game_Player.new
   # Set up initial party
   $game_party.setup_starting_members
   # Set up initial map position
   $game_map.setup($data_system.start_map_id)
   # Move player to initial position
   $game_player.moveto($data_system.start_x, $data_system.start_y)
   # Refresh player
   $game_player.refresh
   # Run automatic change for BGM and BGS set with map
   $game_map.autoplay
   # Update map (run parallel process event)
   $game_map.update
   # Switch to map screen
   $scene = Scene_Map.new
 end
end


You can simply do this

class Scene_Title
 alias add_something_later command_new_game
 def command_new_game
   # THIS IS A NEW LINE BELOW
   $something = something.new
   add_something_later
 end
end


So I'll explain this alias
alias add_something_later command_new_game
the add_something_later is sort of a temporary method thats storing the old method of command_new_game which is the long version of whats posted above.

Then you use add_something_later to call the old method of command_new_game which makes the script still work

This is how I understand aliasing I think I have the jist of it :P not sure though

Blizzard

Simply put: An alias makes a duplicate of the same method with another name. When you redefine the original method, the "aliased" method stays the same. I explained it in my e-book. It's definitely worth a look. :)
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.

Ryex

Quote from: Seox on June 13, 2009, 03:24:36 pm
Oh, and that would still work properly? I still don't fully understand aliasing- I know that it renames a method, but not specifically what the result is. My impression is that it allows you to edit the method without killing it, allowing you to retain the old method. However, under that theory, two methods couldn't alias the same thing.

But all I have to do is rename the alias? They don't go by the same name, they just alias the same thing. Therefore, renaming it shouldn't really make a difference, unless I rename the aliased method - but then it doesn't work at all. Meh.

to sum it up aliasing takes the method as it was defined before the alias method and saves it under a new name, gives it an alias.

def foo(text = 'test')
 p test
end
alias foo_later foo

at this point both foo and foo_later will print 'test'
but if afterward comes

def foo
 p 'hello'
 foo_later('bob')
end

now foo_later will print 'test'
and foo will print 'hello' and then 'bob'.
it allows for the addition of code at the beginning and ends of methods and conditional method calls.

EDIT: darn Blizz you got there first! I was typing this while you posted!
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

LMAO! Don't worry about it. I was referring to my e-book so I was able to make a much shorter post. xD
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.

Seox

Ahhhh...I THINK I get it now. Blizz, I checked your e-book, but I still can't grasp it. Then again, until two weeks ago, I was about the same on attr_'s, but then they just "clicked."

So...long story short, an alias allows you to just add on to the end or beginning of a method, without rewriting, or overwriting a method?

game_guy and rye, those examples DEFINITELY help. Thank you.

And I notice that usually, after alias-ing, when people redefine the original method, they add the name of the new metho-

Ok, it just dawned on me, midsentence. Correct me if I'm wrong, but doesn't that mean that the old method has:
$crappy_extra_variable = :poop
old_method


so that it overwrites the initial method, but then IMMEDIATELY calls the COPY OF ITSELF. IE, it adds it, the short way- just like you said, Rye!

I think I get it, now! *inspiration*

So then, last questions: How does this help compatibility? I know that you mentioned this in the e-book, Blizz, but I didn't really get how/why.
And by
Quote from: Blizzard on June 11, 2009, 05:00:36 am
Sure, just rename one of the clashing aliases. Keep in mind to search the script to find all places where that alias was called (usually only one place) and change it as well.
, which part do you mean to rename?

That should solve my problem, and I'll also walk away understanding aliasing.

Sorry for troubling everyone, but thank you all, VERY MUCH  :^_^':
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Blizzard

Simple. When you have two scripts that rewrite the same method, obviously one will mess up the other script. Aliasing allows you "insert" your code into the method and still execute everything else of all the other scripts.
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.

Seox

Thank you, everyone!

Blizz, that's definitely cleared it up. I may be wrong, but I think that I understand aliasing fully now!

^_^

You mentioned renaming the alias...what are you referring to? The alias name itself seems to not really matter. The old method name is a NECESSITY, given that otherwise, you alias another (nonexistant) method. Which are you referring to?

Thanks again. Enlightenment = pwnage.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Ryex

June 14, 2009, 08:51:52 pm #10 Last Edit: June 14, 2009, 09:04:12 pm by Ryexander
alias foo_later foo
def foo
 do_somthing
 foo_later
end

rename the red, the name of the alias
you only have to do it for one of the scripts though

edit wow i have be giving a lot of help in the rgss section lately, but then again I been asking for a lot too so...
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Seox

#----------------------------------------------------------------------------
# Begin Scene_Battle
#----------------------------------------------------------------------------
class Scene_Battle
  # Alias Methods
  alias syn_scene_battle_range make_basic_action_result
  alias syn_scene_battle_skill make_skill_action_result
  #----------------------------------------------------
  # Alias the Attacking method
  #----------------------------------------------------
  def make_basic_action_result
    # Gather the current Ammo Cost
    gather_ammo_cost = Ammo::Range_weapons_id[@active_battler.weapon_id]
    # Gather the Current Ammo
    gather_ammo = Ammo::Range_ammo_id[@active_battler.weapon_id]
    # Check if the Active Battler is attacking and if they are using a ranged weapon
    if @active_battler.current_action.basic == 0 and Ammo::Range_weapons_id.has_key?(@active_battler.weapon_id)
      # Check the Ammo Count
      if $game_party.item_number(gather_ammo) >= gather_ammo_cost
        # Sufficient Ammo, remove item
        $game_party.lose_item(gather_ammo,gather_ammo_cost)
        syn_scene_battle_range
      else
        # Insufficient Ammo
        @help_window.set_text("#{@active_battler.name}'s weapon clicks - Empty! ", 1)
      end
      # Call Default Code
    else
      syn_scene_battle_range
    end
  end


^ Ammo script snippet


def make_basic_action_result
    if @active_battler.current_action.basic == 0
      execute_action_attack
      return
    end
 


^ CBS.

O_o. It completely defines it, without an alias. I thought it was an alias. Maybe i'm missing it.

I'm guessing that I should alias the second one? Then what, should that work?

Ryex, the only thing that I don't get is the fact that since
Quotealias foo_later foo
def foo
  do_somthing
  foo_later
end


foo_later is only the new alias name, and not the name of a method, and if both WERE aliased, they'd have different alias names, how would renaming it make a difference? It's not like they have THE SAME alias name (foo_later).

Sorry for being so dense...I just don't understand ><
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Ryex

June 14, 2009, 09:48:14 pm #12 Last Edit: June 14, 2009, 09:57:30 pm by Ryexander
Quote from: Seox on June 14, 2009, 09:07:50 pm

foo_later is only the new alias name, and not the name of a method, and if both WERE aliased, they'd have different alias names, how would renaming it make a difference? It's not like they have THE SAME alias name (foo_later).

Sorry for being so dense...I just don't understand ><

but thats what we thought you were saying that... oh never mind have you tried puting your amo script under you cbs?
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Seox

Ahhhh. I'm sorry ><

I meant that they both aliased the same method. Or so I thought >.>

Regardless, I now understand aliasing, thanks to you guys ^_^

Yes, I've tried that. It doesn't return errors, it just doesn't use up ammo like it used to.
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)

Ryex

I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Seox

June 14, 2009, 10:00:26 pm #15 Last Edit: June 14, 2009, 10:04:29 pm by Seox
Not sure.

Trying.

EDIT: No. It works the same both ways. No errors, but no effects.

Here's the script. It's short :

Spoiler: ShowHide
#============================================================================
#                            Syn's Ammo Requirements 
#----------------------------------------------------------------------------
# Written by Synthesize
# Version 1.00
# August 15, 2007
# Tested with SDK 2.1
#============================================================================
#----------------------------------------------------------------------------
# Customization Section
#----------------------------------------------------------------------------
module Ammo
  # Format = {weapon_id => Ammo_cost}
  Range_weapons_id = {1 => 1}
  Range_weapons_id = {2 => 5}
  Range_weapons_id = {4 => 1}
  Range_weapons_id = {5 => 1}
  Range_weapons_id = {6 => 3}
  # Format = {weapon_id => Item_id
  Range_ammo_id = {1 => 33}
  Range_ammo_id = {2 => 34}
  Range_ammo_id = {4 => 36}
  Range_ammo_id = {5 => 36}
  Range_ammo_id = {6 => 37}
  # Format = {skill_id => Ammo_cost}
  Skill_ammo = {81 => 15}
  # Note on Skills: When using Skills the Current Ammo for the equipped
  # weapon will be used. So if Skill 73 is used and Weapon 17 is equipped
  # then Ammo #33 will be used.
end
#----------------------------------------------------------------------------
# Begin Scene_Battle
#----------------------------------------------------------------------------
class Scene_Battle
  # Alias Methods
  alias syn_scene_battle_range make_basic_action_result
  alias syn_scene_battle_skill make_skill_action_result
  #----------------------------------------------------
  # Alias the Attacking method
  #----------------------------------------------------
  def make_basic_action_result
    # Gather the current Ammo Cost
    gather_ammo_cost = Ammo::Range_weapons_id[@active_battler.weapon_id]
    # Gather the Current Ammo
    gather_ammo = Ammo::Range_ammo_id[@active_battler.weapon_id]
    # Check if the Active Battler is attacking and if they are using a ranged weapon
    if @active_battler.current_action.basic == 0 and Ammo::Range_weapons_id.has_key?(@active_battler.weapon_id)
      # Check the Ammo Count
      if $game_party.item_number(gather_ammo) >= gather_ammo_cost
        # Sufficient Ammo, remove item
        $game_party.lose_item(gather_ammo,gather_ammo_cost)
        syn_scene_battle_range
      else
        # Insufficient Ammo
        @help_window.set_text("#{@active_battler.name}'s weapon clicks - Empty! ", 1)
      end
      # Call Default Code
    else
      syn_scene_battle_range
    end
  end
  #----------------------------------------------------
  # Alias the Skill method
  #----------------------------------------------------
  def make_skill_action_result
    # Gather the current Ammo Cost
    gather_ammo_cost = Ammo::Skill_ammo[@active_battler.current_action.skill_id]
    # Gather Ammo
    gather_ammo = Ammo::Range_ammo_id[@active_battler.weapon_id]
    # Check if the Actor is using a defiend skill
    if Ammo::Skill_ammo.has_key?(@active_battler.current_action.skill_id)
      # Check if Ammo is present
      if $game_party.item_number(gather_ammo) >= gather_ammo_cost
        # Sufficient Ammo, remove item
        $game_party.lose_item(gather_ammo,gather_ammo_cost)
        # Call Default Code
        syn_scene_battle_skill
      else
        # Set Window; Do Nothing
        @help_window.set_text("#{@active_battler.name} cannot attack due to insufficient Ammo", 1)
      end
      # Otherwise SKip the check and call default code
    else
      syn_scene_battle_skill
    end
  end
end
#============================================================================
# Written by Synthesize
# Special Thanks: ~Emo~ for the request
#----------------------------------------------------------------------------
#                            Ammo Requirements
#============================================================================
... (<<<<<<<<<<<<<<< TEH DOTS OF DOOM. Hey, kinda catchy. :naughty:)