Jump to content
Sign in to follow this  
MasterMoes

Script value won't accept a variable value

Recommended Posts

Hello, all yall...

I have this Hime script that lets me move the position of enemies in the middle of battle. I have this set up going on using Hime's Global Common Events script where I have a parallel process common event running during battle. The common event contains script calls that continuously move the enemy's position, creating a sort of floating/flying animation. The problem is, it isn't very smooth looking, so I tried to change the value of "Move_Speed" to be determined by a variable so I can slow down and speed up the movement speed for smooth floating animation.

Example of the common event:

168588082_help1.PNG.ca39a0f0f8551a746df2b17a686a0580.PNG

As you can see in the script below, I set the value of "Move_Speed" to $game_variables[4819], which should work properly. However, every time I test my game, I get this message:

2074255015_help2.PNG.f5d71d53c9bbfec164c71903073cdfa5.PNG

I can't figure out why the Move_Speed value can't be a variable value. Repositioning my script didn't work. I tried doing "==" after Move_Speed but that didn't work either. This variable method has worked for me for other scripts. Does anyone know what the problem is?

Script (with variable modification):

Spoiler

=begin
#===============================================================================
 Title: Enemy Re-position
 Author: Hime
 Date: Feb 15, 2014
--------------------------------------------------------------------------------
 ** Change log
 Feb 15, 2014
   - Checks whether enemy exists before trying to re-position
 Apr 6, 2013
   - Initial release
--------------------------------------------------------------------------------   
 ** Terms of Use
 * Free to use in non-commercial projects
 * Contact me for commercial use
 * No real support. The script is provided as-is
 * Will do bug fixes, but no compatibility patches
 * Features may be requested but no guarantees, especially if it is non-trivial
 * Credits to Hime Works in your project
 * Preserve this header
--------------------------------------------------------------------------------
 ** Description
 
 This script allows you to move your enemy's sprites around in battle using
 script calls.
--------------------------------------------------------------------------------
 ** Installation
 
 Place this script below Materials and above Main
 
--------------------------------------------------------------------------------
 ** Usage

 Make a script call during battle

   position_enemy(enemy_index, x, y)
   move_enemy(enemy_index, x, y)
   
 The enemy_index is the index of the enemy, where 1 is the first enemy,
 2 is the second enemy, and so on.
 
 x, y is the position they will be moved to.
 
 The first call is an absolute position relative to the top-left corner of
 the screen.
 
 The second call is relative to the sprite's current position. So if you say
 x = 100, then it will shift it to the right 100 pixels.
 
#===============================================================================
=end
$imported = {} if $imported.nil?
$imported["TH_Enemy_Reposition"] = true
#===============================================================================
# ** Configuration
#===============================================================================
module TH
  module Enemy_Reposition
    
    # how fast the sprite moves around the screen
    Move_Speed = $game_variables[4819]
  end
end
#===============================================================================
# ** Rest of script
#===============================================================================
class Game_Interpreter
  
  #-----------------------------------------------------------------------------
  # Specify absolute position where the enemy should be placed
  #-----------------------------------------------------------------------------
  def position_enemy(index, x, y)
    enemy = $game_troop.members[index-1]
    return unless enemy
    enemy.set_new_position(x, y)
    
  end
  
  #-----------------------------------------------------------------------------
  # Specify relative position where the enemy should be placed.
  #-----------------------------------------------------------------------------
  def move_enemy(index, x, y)
    enemy = $game_troop.members[index-1]
    return unless enemy
    enemy.set_new_position(enemy.screen_x + x, enemy.screen_y + y)
  end
end

class Game_Enemy < Game_Battler
  
  attr_accessor :new_screen_x
  attr_accessor :new_screen_y
  attr_accessor :position_changing
  
  def position_changing?
    @position_changing
  end
  
  def set_new_position(x, y)
    @new_screen_x = x
    @new_screen_y = y
    @position_changing = true
  end
end

class Game_Troop < Game_Unit
  
  alias :th_enemy_reposition_setup :setup
  def setup(troop_id)
    th_enemy_reposition_setup(troop_id)
    setup_initial_positions
  end
  
  def setup_initial_positions
    @enemies.each do |enemy|
      enemy.new_screen_x = enemy.screen_x
      enemy.new_screen_y = enemy.screen_y
    end
  end
end

class Sprite_Battler < Sprite_Base
  
  alias :th_enemy_reposition_update :update
  def update
    th_enemy_reposition_update
    if @battler && @battler.enemy?
      update_move_position if @battler.position_changing?
    end
  end
  
  #-----------------------------------------------------------------------------
  # How fast the sprite's position is changed in pixels
  #-----------------------------------------------------------------------------
  def update_move_speed
    TH::Enemy_Reposition::Move_Speed
  end
  
  #-----------------------------------------------------------------------------
  # Move the sprite towards its new position
  #-----------------------------------------------------------------------------
  def update_move_position
    if @battler.screen_x != @battler.new_screen_x
      if @battler.screen_x < @battler.new_screen_x
        self.x = [@battler.screen_x + update_move_speed, @battler.new_screen_x].min
      else
        self.x = [@battler.screen_x - update_move_speed, @battler.new_screen_x].max
      end
      @battler.screen_x = self.x
    end
    
    if @battler.screen_y != @battler.new_screen_y
      if @battler.screen_x < @battler.new_screen_x
        self.y = [@battler.screen_y + update_move_speed, @battler.new_screen_y].min
      else
        self.y = [@battler.screen_y - update_move_speed, @battler.new_screen_y].max
      end
      @battler.screen_y = self.y
    end
    
    if @battler.screen_x == @battler.new_screen_x && @battler.screen_y == @battler.new_screen_y
      @battler.position_changing = false
    end
    self.z = @battler.screen_z
  end
end

 

 

Share this post


Link to post
Share on other sites
Posted (edited)

Simply replace these parts of the code, and it should work normally. There's a caveat, you can't use a global variable that has not been initialized at that point. Thus you gotta pick any valid number instead.

CODE UPDATED

module TH
  module Enemy_Reposition
    @move_speed = 2 # $game_variables[4819]
    extend self
    attr_accessor :move_speed
  end
end
...
  def update_move_speed
    TH::Enemy_Reposition.move_speed
  end

The update_move_speed method can be found inside the Sprite_Battler class. Just go replace it at once.

Edited by kyonides

Share this post


Link to post
Share on other sites

In addition, I don't believe that values above 6 will work.

Share this post


Link to post
Share on other sites
On 4/19/2023 at 8:58 PM, kyonides said:

Simply replace these parts of the code, and it should work normally. There's a caveat, you can't use a global variable that has not been initialized at that point. Thus you gotta pick any valid number instead.


module TH
  module Enemy_Reposition
    # how fast the sprite moves around the screen
    @move_speed = 2 # $game_variables[4819]
    attr_accessor :move_speed
  end
end
...
  def update_move_speed
    TH::Enemy_Reposition.move_speed
  end

The update_move_speed method can be found inside the Sprite_Battler class. Just go replace it at once.

If these script changes don't let me change the value of @move_speed using a variable, then what's the purpose of these changes exactly? The script functions properly when using a simple number value, but that's not what I need. I'm trying to get it to work with a variable. I also don't know what you mean when you say "You can't use a global variable that has not been initialized at that point". All variable values start at 0, don't they? If a variable hasn't been set to anything, it should default to 0, thus setting the @move_speed value to 0 until otherwise changed. Sorry, but these script changes don't solve my problem at all. Also, update_move_speed does not exist in the Sprite_Battler class...

7 hours ago, PhoenixSoul said:

In addition, I don't believe that values above 6 will work.

Good to know!

Share this post


Link to post
Share on other sites
Posted (edited)

That's the problem of dealing with a newcomer that thinks he already knows how the code works...

First of all, I wanna tell you I'm not forced to tell you anything, yet, I'll do it only because my teacher's side has emerged. Even so I'll make sure I won't repeat myself. Thus you better pay close attention to this issue.

NO GLOBAL VARIABLES exist at the moment the game is loading.

This means that it will fail all checks at the moment it reads the global variable's contents expecting to get an array that has NEVER been created at that point.

Never forget that! Never! I said NEVER!

Fail to remember that and it will keep throwing weird error messages at your face.

WARNING:

If you're thinking that it doesn't matter and you'll directly create the variable by setting it to an Array for any reason, it will still fail because it'll be reset when the game starts a new game session.

Since you only care about using your favorite game variable, you have 2 options.

Using the updated version of my proposed changes, stop using that game variable and start setting the value of TH::Enemy_Reposition.move_speed = 2 manually. This means, you'll be using that script call as many times as needed.

Anyway, keep in mind it changes the speed of ALL ENEMIES.

The other way is to simply forget about the starting value and make your favorite game variable the return value of the update_move_speed method.

End of the Scripting Class

Edited by kyonides

Share this post


Link to post
Share on other sites
22 minutes ago, kyonides said:

That's the problem of dealing with a newcomer that thinks he already knows how the code works...

I never claimed I knew how the code works. If I DID know how the code works I wouldn't be asking for help...

24 minutes ago, kyonides said:

The other way is to simply forget about the starting value and make your favorite game variable the return value of the update_move_speed method.

Anyway, because I'm no expert scripter, my question is: how can I make the value of the update_move_speed method equal to a variable?

Share this post


Link to post
Share on other sites
Posted (edited)

Go to the method I mentioned and replace its contents with your favorite game variable ONLY, just like you did it on your first post.

And don't expect me to answer any more questions. If you can follow the instructions without questioning, fine. I'll ignore future inquiries on this topic, anyway.

Edited by kyonides

Share this post


Link to post
Share on other sites

Putting aside your attitude and your over-expectations of someone of my newbie scripting capabilities, I got it working, so for THAT, I'm just going to say thank you and walk away.

For anyone needing the finished modified script, here you go:

Spoiler

=begin
#===============================================================================
 Title: Enemy Re-position
 Author: Hime
 Date: Feb 15, 2014
--------------------------------------------------------------------------------
 ** Change log
 Feb 15, 2014
   - Checks whether enemy exists before trying to re-position
 Apr 6, 2013
   - Initial release
--------------------------------------------------------------------------------   
 ** Terms of Use
 * Free to use in non-commercial projects
 * Contact me for commercial use
 * No real support. The script is provided as-is
 * Will do bug fixes, but no compatibility patches
 * Features may be requested but no guarantees, especially if it is non-trivial
 * Credits to Hime Works in your project
 * Preserve this header
--------------------------------------------------------------------------------
 ** Description
 
 This script allows you to move your enemy's sprites around in battle using
 script calls.
--------------------------------------------------------------------------------
 ** Installation
 
 Place this script below Materials and above Main
 
--------------------------------------------------------------------------------
 ** Usage

 Make a script call during battle

   position_enemy(enemy_index, x, y)
   move_enemy(enemy_index, x, y)
   
 The enemy_index is the index of the enemy, where 1 is the first enemy,
 2 is the second enemy, and so on.
 
 x, y is the position they will be moved to.
 
 The first call is an absolute position relative to the top-left corner of
 the screen.
 
 The second call is relative to the sprite's current position. So if you say
 x = 100, then it will shift it to the right 100 pixels.
 
#===============================================================================
=end
$imported = {} if $imported.nil?
$imported["TH_Enemy_Reposition"] = true
#===============================================================================
# ** Configuration
#===============================================================================
module TH
  module Enemy_Reposition
    
    # how fast the sprite moves around the screen. This value can be changed
    # through the value number on line 137 of this script (default= variable 100)
    Move_Speed = 12
  end
end
#===============================================================================
# ** Rest of script
#===============================================================================
class Game_Interpreter
  
  #-----------------------------------------------------------------------------
  # Specify absolute position where the enemy should be placed
  #-----------------------------------------------------------------------------
  def position_enemy(index, x, y)
    enemy = $game_troop.members[index-1]
    return unless enemy
    enemy.set_new_position(x, y)
    
  end
  
  #-----------------------------------------------------------------------------
  # Specify relative position where the enemy should be placed.
  #-----------------------------------------------------------------------------
  def move_enemy(index, x, y)
    enemy = $game_troop.members[index-1]
    return unless enemy
    enemy.set_new_position(enemy.screen_x + x, enemy.screen_y + y)
  end
end

class Game_Enemy < Game_Battler
  
  attr_accessor :new_screen_x
  attr_accessor :new_screen_y
  attr_accessor :position_changing
  
  def position_changing?
    @position_changing
  end
  
  def set_new_position(x, y)
    @new_screen_x = x
    @new_screen_y = y
    @position_changing = true
  end
end

class Game_Troop < Game_Unit
  
  alias :th_enemy_reposition_setup :setup
  def setup(troop_id)
    th_enemy_reposition_setup(troop_id)
    setup_initial_positions
  end
  
  def setup_initial_positions
    @enemies.each do |enemy|
      enemy.new_screen_x = enemy.screen_x
      enemy.new_screen_y = enemy.screen_y
    end
  end
end

class Sprite_Battler < Sprite_Base
  
  alias :th_enemy_reposition_update :update
  def update
    th_enemy_reposition_update
    if @battler && @battler.enemy?
      update_move_position if @battler.position_changing?
    end
  end
  
  #-----------------------------------------------------------------------------
  # How fast the sprite's position is changed in pixels
  #-----------------------------------------------------------------------------
  def update_move_speed
    $game_variables[100]
  end
  
  #-----------------------------------------------------------------------------
  # Move the sprite towards its new position
  #-----------------------------------------------------------------------------
  def update_move_position
    if @battler.screen_x != @battler.new_screen_x
      if @battler.screen_x < @battler.new_screen_x
        self.x = [@battler.screen_x + update_move_speed, @battler.new_screen_x].min
      else
        self.x = [@battler.screen_x - update_move_speed, @battler.new_screen_x].max
      end
      @battler.screen_x = self.x
    end
    
    if @battler.screen_y != @battler.new_screen_y
      if @battler.screen_x < @battler.new_screen_x
        self.y = [@battler.screen_y + update_move_speed, @battler.new_screen_y].min
      else
        self.y = [@battler.screen_y - update_move_speed, @battler.new_screen_y].max
      end
      @battler.screen_y = self.y
    end
    
    if @battler.screen_x == @battler.new_screen_x && @battler.screen_y == @battler.new_screen_y
      @battler.position_changing = false
    end
    self.z = @battler.screen_z
  end
end

 

FYI: The value of Move_Speed can go above 6.

  • Thanks 1

Share this post


Link to post
Share on other sites

Guys, my solution consists of not using a game variable at all, allowing you to save your game variables for other more important uses than just some very specific enemy's speed. For instance, you could use that variable for storing your party's gold or steps or anything else. To implement it, you'd simply copy the original script posted on the first post and then change the parts that share the same module or class name and method name.

On 4/19/2023 at 9:58 PM, kyonides said:

Simply replace these parts of the code, and it should work normally. There's a caveat, you can't use a global variable that has not been initialized at that point. Thus you gotta pick any valid number instead.

CODE UPDATED


module TH
  module Enemy_Reposition
    @move_speed = 2 # Initial Value, 2 is quite slow
    extend self
    attr_accessor :move_speed
  end
end
# Then jump down to class Sprite_Battler and modifiy this method found inside:
  def update_move_speed
    TH::Enemy_Reposition.move_speed
  end

The update_move_speed method can be found inside the Sprite_Battler class. Just go replace it at once.

The script call would be TH.Enemy_Reposition.move_speed = +number.

I don't think number 0 would ever help you there.

I would also like to inform people that since this seems to be used in battle, the move speed has no real limit. Well, it never had a true limit on the map anyway. It just happens that the GUI doesn't allow you to go above 6, still, you can do that via a script call or a combo of a new script and changing the value of a game variable.

 

Depending on your goals going over 6 might be too fast for your taste or your players' so be careful about setting a high value to it. Of course, if it is supposed to move like a ghost or a speedster by design, it might look "natural" so to say.

And as a friendly reminder, don't rush to radical conclusions like some other forumer did here that can make a scripter feel like he or she is wasting his or her time by trying to offer some assistance to the OP because the poster simply refuses to accept any piece of advice for not getting the type of answer the poster had expected.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
Top ArrowTop Arrow Highlighted