Trying to adjust the location of an object based on the number of enemies

I'm currently trying to adjust something where it appears in the middle of screen. It has 4 objects that are 24 pixels each representing  + the number of enemies on the screen which are 24 pixels each

So my location of x was:

x= 272 - [12 * $data_enemies]

with $data_enemies being the variable. Since the number of enemies change each round.

Instead we have been getting a nil Error

Then when I use an unless nil argument

it will error out with a uninitialized constant error.

is there a way to put a location based on the number of enemies on the screen?
I'm currently trying to adjust something where it appears in the middle of screen. It has 4 objects that are 24 pixels each representing  + the number of enemies on the screen which are 24 pixels each
I'm afraid this doesn't make much sense. Can you be more descriptive, or actually show us a picture of what you're trying to do?


I am guessing you just want to evenly line up all the enemies along the screen, based on how many there are in the troop?


$data_enemies is an array of Game_Enemy objects. It contains ALL the enemies that are in a game - one of each. It does not contain what enemies you are about to face in a battle. You cannot perform arithmetic on it. I suspect you are after $game_troop.members.size


And since you're getting a nil error (next time, include the EXACT text of the error message), I'm guessing, however you're trying to run this, you're doing it before $data_enemies has even been created, which means you're doing it in the wrong spot. So perhaps give us the whole method that you're working on and state what class it's in, so we know if you're trying to put it in the right place.
Yep! Sorry about that. The Gauge itself is left aligned. So the more enemies the more to the left it needs to moved. Worse case if I can't get it to work I'll see about changing it to being vertical and finding a new location. I pasted the section of code that I was editing. If you need the whole piece it's located here. http://yamiworld.wordpress.com/rgss3/battle-scripts/yeas-add-on/ysa-battle-add-on-order-gauge/. I understand that this should probably be moved to support now. Sorry in advance. 

[IMG]http://i1272.photobucket.com/albums/y396/devilarms2099/CenterGuage_zps30981ffb.jpg[/IMG]

Code:
module YSA  module ORDER_GAUGE        # Default Icon for actor and enemy    DEFAULT_ENEMY_ICON = 1    DEFAULT_ACTOR_ICON = 189        # Order Sprite Visual. Decide Order's Background and Border.    BATTLER_ICON_BORDERS = { # Do not remove this.      # Type => [Back, Border,  ],      :actor => [ 0,    0,  ],      :enemy => [ 0,    0,  ],    } # Do not remove this.        # Turn this to true if you want to show death battlers.    # SHOW_DEATH = false        # Coordinate-X of order gauge    GAUGE_X = 272    # Coordinate-Y of order gauge    GAUGE_Y = 24      # Show Switch. Turn this switch on to show it. If you want to disable, set this    # to 0.    SHOW_SWITCH = 0  endend
Okay, $game_troop.members.size is the number you need.


However, you need to figure out where it's drawn. Where were you putting that line of code that you had? You can't just bang it into the module, you have to find out where it says to actually draw it (by this time the database has been populated), and make your adjustment there. It'll probably be a Scene_Battle class or one of its windows.


GAUGE_X = 272


is populating a constant. When this happens, $data_enemies and $game_troop do not exist. That's where your 'nil' error is coming from.


You will have to add YSA:😮RDER_GAUGE::GAUGE_X = 272 - 12 * $game_troop.members.size somewhere in the script (and even then I'm not sure if it'll just give an error - if it's REALLY treating that like a constant and not allowing you to change it). Do a search for SHOW_SWITCH - I'm betting there's a condition using it that says whether to show the gauge or not, and that would probably be a likely spot to put the new position.


Maybe a link to the entire script would be helpful. In fact, when you're asking for help with a script, you should always add a link to it, so people don't have to go searching and hope they find the same one.
Oh the line of code was actually being placed where the Gauge_X= on the snippet I put up.

Here's the original code that I'm using from yami's site

http://dl.dropboxusercontent.com/u/38072264/ordergauge212129012012.txt

This is where that bit of code went.

module YSA module ORDER_GAUGE # Default Icon for actor and enemy DEFAULT_ENEMY_ICON = 1 DEFAULT_ACTOR_ICON = 189 # Order Sprite Visual. Decide Order's Background and Border. BATTLER_ICON_BORDERS = { # Do not remove this. # Type => [Back, Border, ], :actor => [ 0, 0, ], :enemy => [ 0, 0, ], } # Do not remove this. # Turn this to true if you want to show death battlers. # SHOW_DEATH = false # Coordinate-X of order gauge unless $data_enemies.nil? GAUGE_X = 272 - [12*$data_enemies] # Coordinate-Y of order gauge GAUGE_Y = 24 end # Show Switch. Turn this switch on to show it. If you want to disable, set this # to 0. SHOW_SWITCH = 0 else GUAGE_X = 272 GUAGE_Y = 24 endendI did a search on $game_troop.members.size in the script and it doesn't seem to call for it. I did find $game_troop.members inside of the scene_battle in the script though under an alias method

Code:
# alias method: create_all_windows  #--------------------------------------------------------------------------  alias order_gauge_create_all_windows create_all_windows  def create_all_windows    order_gauge_create_all_windows    @spriteset_order = []    if $imported["YSA-PCTB"] && YSA::PCTB::CTB_MECHANIC[:predict] == 1      @active_order_sprite = Sprite_OrderBattler.new(@spriteset.viewportOrder, nil, :pctb2)     end    if $imported["YSA-PCTB"] && YSA::PCTB::CTB_MECHANIC[:predict] == 2      num = YSA::PCTB::CTB_MECHANIC[:pre_turns]      i = 0      num.times {        order = Sprite_OrderBattler.new(@spriteset.viewportOrder, nil, :pctb3, i)         @spriteset_order.push(order)        i += 1      }      return    end    for battler in $game_party.members + $game_troop.members      battle_type = :dtb      battle_type = :pctb if BattleManager.btype?(:pctb)      battle_type = :catb if BattleManager.btype?(:catb)      order = Sprite_OrderBattler.new(@spriteset.viewportOrder, battler, battle_type)       @spriteset_order.push(order)    end  end
The other location I Found it in was inside of this snippet
Code:
#--------------------------------------------------------------------------  # update_dtb_style  #--------------------------------------------------------------------------  def update_dtb_style    #---    actor_window = SceneManager.scene.actor_window    enemy_window = SceneManager.scene.enemy_window    if actor_window.active      if $game_party.members[actor_window.index] == @battler        @move_y = 12       else        @move_y = 24       end    end    if enemy_window.active      if $game_troop.members[enemy_window.index] == @battler        @move_y = 12       else        @move_y = 24       end    end    if !actor_window.active && !enemy_window.active      @move_y = 24     end    #---    return if !@move_x && !@move_y    if @battler.hidden? || (!@show_dead && @battler.dead?)      self.opacity -= 20    end    if self.x != @move_x && @move_x      if @move_x > self.x        @move_y = 30      elsif @move_x < self.x        @move_y = 16      else        @move_y = 20      end      self.z = (@move_x < self.x) ? 7500 : 8500      if @move_x >= self.x        self.x += [@move_rate_x, @move_x - self.x].min      else        self.x -= [@move_rate_x, - @move_x + self.x].min      end    end    if self.y != @move_y && @move_y      self.y += (self.y > @move_y) ? -@move_rate_y : @move_rate_y    end    if self.x == @move_x && @move_x      @first_time = false if @first_time      @move_x = nil    end    if self.y == @move_y && @move_y      @move_y = nil    end  end
Please give a link to the FULL script, as you downloaded it. There's obviously more to it than this, and I don't really want to keep asking for bits and pieces until you hopefully provide the particular bit I need to see.


As I said, if you put it where the constant was defined, then $data_enemies and $game_troop don't exist at that point. So obviously it won't work. You need to put it somewhere that's got code that will run at the beginning of each individual battle, because each battle will have a specific number of enemies, and you want the gauge positioned according to the number of enemies.


Once I can see the FULL script (link to it, do NOT post it here), I'll be able to find out where it should go.
http://dl.dropboxusercontent.com/u/38072264/ordergauge212129012012.txt

Actually I posted it in my 1st reply to ya

Here it is again if that link doesn't work sorry about that
This method:

Code:
  #--------------------------------------------------------------------------  # alias method: update_basic  #--------------------------------------------------------------------------  alias order_gauge_update_basic update_basic  def update_basic    order_gauge_update_basic    for order in @spriteset_order      order.update    end    if @update_ordergauge      BattleManager.make_action_orders      for order in @spriteset_order        order.make_destination      end      @update_ordergauge = false    end    if YSA::ORDER_GAUGE::SHOW_SWITCH == 0 || $game_switches[YSA::ORDER_GAUGE::SHOW_SWITCH]      @spriteset.viewportOrder.ox = -YSA::ORDER_GAUGE::GAUGE_X if @spriteset.viewportOrder.ox != -YSA::ORDER_GAUGE::GAUGE_X      @spriteset.viewportOrder.oy = -YSA::ORDER_GAUGE::GAUGE_Y if @spriteset.viewportOrder.oy != -YSA::ORDER_GAUGE::GAUGE_Y      if @spriteset.viewportOrder.oy <= @help_window.y + @help_window.height        @spriteset.viewportOrder.oy = -(@help_window.y + @help_window.height) if @help_window.visible      end    else      @spriteset.viewportOrder.ox = Graphics.height if @spriteset.viewportOrder.ox != Graphics.width      @spriteset.viewportOrder.oy = Graphics.width if @spriteset.viewportOrder.oy != Graphics.height    end    @spriteset.viewportOrder.oy = Graphics.height if $game_troop.all_dead? || $game_party.all_dead?  end
We need to add one line to do the calculation, and change one line to make it use the new figure instead of the constant.

Code:
  #--------------------------------------------------------------------------  # alias method: update_basic  #--------------------------------------------------------------------------  alias order_gauge_update_basic update_basic  def update_basic    order_gauge_update_basic    for order in @spriteset_order      order.update    end    if @update_ordergauge      BattleManager.make_action_orders      for order in @spriteset_order        order.make_destination      end      @update_ordergauge = false    end    if YSA::ORDER_GAUGE::SHOW_SWITCH == 0 || $game_switches[YSA::ORDER_GAUGE::SHOW_SWITCH]      # changed below ---------------------------------------------------      gauge_ox = 12 * $game_troop.members.size   # ADDED to calculate position based on troop size      @spriteset.viewportOrder.ox = -gauge_ox if @spriteset.viewportOrder.ox != -guage_ox   # USE position based on troop size, not constant      # changed above ---------------------------------------------------      @spriteset.viewportOrder.oy = -YSA::ORDER_GAUGE::GAUGE_Y if @spriteset.viewportOrder.oy != -YSA::ORDER_GAUGE::GAUGE_Y      if @spriteset.viewportOrder.oy <= @help_window.y + @help_window.height        @spriteset.viewportOrder.oy = -(@help_window.y + @help_window.height) if @help_window.visible      end    else      @spriteset.viewportOrder.ox = Graphics.height if @spriteset.viewportOrder.ox != Graphics.width      @spriteset.viewportOrder.oy = Graphics.width if @spriteset.viewportOrder.oy != Graphics.height    end    @spriteset.viewportOrder.oy = Graphics.height if $game_troop.all_dead? || $game_party.all_dead?  end
Obviously the --------------------------------- lines are there to make it obvious what change I made. You don't have to include them.
Alright got the changes in. Do i need to put 272 up in the top x_gauge= or leave it as is at 0.

Currently I tried with both and now it's align to the left again.
You should be able to completely ignore YSA:😮RDER_GAUGE::GAUGE_X now, as it shouldn't be used anywhere.


I think I missed one line, and looking at it, it appears there's also a bug in the script, where it's positioning the y coordinate based on the width, not the height.


Try this instead:


<removed as it wasn't right>


I'm not quite sure it's right, but it's pretty early in the day and the best I can do at the moment.
No worries. You caught me really late last night and I posted the reply really early. 
Take your time on it. I'm not releasing the demo anytime soon. (LOL I still have about 50 skills to design)
did you try what I just posted?


oh - it probably won't work, as it looks like it's just hiding it off the screen.


Can you JUST make the changes I gave you yesterday, and post a screenshot?
Yep here ya go!

[IMG]http://i1272.photobucket.com/albums/y396/devilarms2099/CenterGuage1_zps1692431c.jpg[/IMG]
Okay, yes, set that GAUGE_X back to 0 - I didn't realize you'd changed it from the default.

THIS method works for me. I don't really understand why the script is doing things the way it is, so it may not be the most appropriate solution, but it works.

Spoiler
Code:
  #--------------------------------------------------------------------------  # alias method: update_basic  #--------------------------------------------------------------------------  alias order_gauge_update_basic update_basic  def update_basic    order_gauge_update_basic    for order in @spriteset_order      order.update    end    if @update_ordergauge      BattleManager.make_action_orders      for order in @spriteset_order        order.make_destination      end      @update_ordergauge = false    end    if YSA::ORDER_GAUGE::SHOW_SWITCH == 0 || $game_switches[YSA::ORDER_GAUGE::SHOW_SWITCH]      ox = -Graphics.width / 2 + @spriteset_order.size * 12      @spriteset.viewportOrder.ox = ox if @spriteset.viewportOrder.ox != ox      @spriteset.viewportOrder.oy = -YSA::ORDER_GAUGE::GAUGE_Y if @spriteset.viewportOrder.oy != -YSA::ORDER_GAUGE::GAUGE_Y      if @spriteset.viewportOrder.oy <= @help_window.y + @help_window.height        @spriteset.viewportOrder.oy = -(@help_window.y + @help_window.height) if @help_window.visible      end    else      @spriteset.viewportOrder.ox = Graphics.height if @spriteset.viewportOrder.ox != Graphics.width      @spriteset.viewportOrder.oy = Graphics.width if @spriteset.viewportOrder.oy != Graphics.height    end    @spriteset.viewportOrder.oy = Graphics.height if $game_troop.all_dead? || $game_party.all_dead?  end
Yep looks good. The only thing weird left on it is that the dead battler's icon's don't disappear from the screen I'll take a look at it in the morning though.

[IMG]http://i1272.photobucket.com/albums/y396/devilarms2099/CenterGuage2_zps2c8d16f1.jpg[/IMG]
 
Good luck. I don't THINK the changes I made would have anything to do with this, but if you want to confirm, you could just disable that script (click somewhere in the script, Control+A then Control+Q), then copy the original script into a new script slot and see if it still does it. Then of course, to reinstate, just delete the newest script, and Control+A, Control+Q to uncomment the original.

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-adjust-the-location-of-an-object-based-on-the-number-of-enemies.16726/