Getting the position of Enemies/Actors in battle

Discussion in 'RGSS Script Requests' started by cloakedranger, Jan 18, 2015.

  1. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    How can I get the position of enemies or actors in battle? I'm trying to get actors and enemies to move toward their target, and I've not found any way to do this yet, nor have a figured out how to reference enemy and actor positions through script or common events (and no, I don't want to use anyone's premade battle systems because I plan on adding stuff they don't have, and in my experiences those systems aren't properly supported, not to mention have a bunch of features I don't plan to use... a recipe for bugs and conflicts that I don't have time to deal with). Also, I would prefer if whatever snippet was provided, could be something I could use at will through common events. I really appreciate the help, thank you.
     
    #1
  2. Heretic86

    Heretic86 Veteran Veteran

    Messages:
    240
    Likes Received:
    163
    First Language:
    Engrish
    If you have some basic understanding of scripting, go for Index.  $game_party.actors.index(actor) should return which position a particular actor is in.
     
    #2
    cloakedranger likes this.
  3. Shaz

    Shaz Veteran Veteran

    Messages:
    37,956
    Likes Received:
    11,615
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    That is the position in the enemy troop, not the position of the actual sprite on the screen.


    However, $game_troop.members[index].enemy should give you x and y values, or screen_x and screen_y values, which you can then combine with the size of the battler sprite to work out your rectangle.
     
    #3
  4. SuperMasterSword

    SuperMasterSword That Guy You Dont Know Veteran

    Messages:
    117
    Likes Received:
    48
    Location:
    Awesomeville
    First Language:
    Javascript
    Primarily Uses:
    RMMV
    So Heretic86, is there a way to set that to a variable? because I actually needed something like that for a skill I was making, (if you want the long reason why it's in another request post on this forum [i would provide a link anyway but every time I try to click the "link" icon {or "image" for that  matter} the website freezes up and I can't get it to go away and I have to refresh the page]).
     
    #4
  5. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    Took some doing (I'm not too knowlegable on Ruby) but this is how I did it:

    var1 = $data_troops[1].members[1].x

    and then this to display the gotten variable.

    print var1

    Another question... how do I reference the current troop? While it's cool to be able to get the monster stats from any troop, using "1" manually for the first troop is going to get cumbersome when I use the script in other troops.
     
    Last edited by a moderator: Jan 19, 2015
    #5
  6. Shaz

    Shaz Veteran Veteran

    Messages:
    37,956
    Likes Received:
    11,615
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    I just posted that - did you not read my post?


    $game_troop gives you the current troop. $game_troop.members gives you all the members.
     
    #6
  7. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    Sorry, I see nothing in this post that tells me "$game_troop" is the current troop. Or do you mean what I posted? "$data_troops" is not the same thing as "$game_troop". I appreciate your help, but you may want to learn how to explain things better. In fact, could you please explain the proper usage? Because when I do it my way, it works... putting in yours gives me an error "unidentified method 'members'".
     
    #7
  8. SuperMasterSword

    SuperMasterSword That Guy You Dont Know Veteran

    Messages:
    117
    Likes Received:
    48
    Location:
    Awesomeville
    First Language:
    Javascript
    Primarily Uses:
    RMMV
    Uh, thanks cloakedranger, but I was asking Heretic because I needed what spot a specific actor is in and to set that to a global variable so I could use it in regular eventing conditional branches, I could probably just switch a few parts out. (I think I could reference a script or two I saw to see how to set the global variable) also, do I just replace the "actor" in parenthesis with the actor's id? That's what I'm guessing but I'm new to this and want to make sure.
     
    #8
  9. Shaz

    Shaz Veteran Veteran

    Messages:
    37,956
    Likes Received:
    11,615
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    There IS only one $game_troop. It is the troop that is currently taking part in battle against your party.


    $data_troops is the list of ALL your troops. $game_troop is the one that is currently in battle. $game_troop will contain the enemies in one of your $data_troops, as well as other information. If there is anything else you need to know about how they go together, let me know and I'll try to explain.


    Try $game_troop.enemies instead of $game_troop.members - I didn't realize you posted in the RGSS Scripts for XP, and gave the answer for Ace, since that's the engine most questions are about :)


    @SuperMasterSword, please do not hijack someone else's thread with your own questions. Start your own thread and ask your questions there, so this thread doesn't become all about helping you.
     
    #9
    cloakedranger likes this.
  10. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    Ok, that makes much more sense :) I seem to have it working now with:

    $game_troop.enemies[1].screen_x

    It's interesting though that the other way works with just .x but I had to use screen_x (like you suggested) with this method. But it works, and that's all I care about xD

    Thank you very much for your help (and patience). I think I have other questions, but probably not too related to this... so if I need to, I will just open another thread when I'm ready.

    Oh, and I will try to include the version I'm using in the post, just to make sure next time :)
     
    #10
  11. SuperMasterSword

    SuperMasterSword That Guy You Dont Know Veteran

    Messages:
    117
    Likes Received:
    48
    Location:
    Awesomeville
    First Language:
    Javascript
    Primarily Uses:
    RMMV
    Sorry, Shaz. I tried starting one but didn't really find the answers I was looking for. I saw someone posted something that it looked like I could use here, but I'll stop now.
     
    #11
  12. Shaz

    Shaz Veteran Veteran

    Messages:
    37,956
    Likes Received:
    11,615
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    @cloakedranger, including the version in your post would be helpful. In this case, I just wasn't looking carefully enough. But sometimes people post in the wrong forum, or the forum they post in is not the same as what they have as "Primarily uses ..." below their avatar, so sometimes the question is still there. Including it would leave no doubt :)
     
    #12
  13. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    Understood :) (I'm still not sure which program I ought to have as my primary, since I'm currently using both) This can probably be closed now. Again, thank you for the assistance.
     
    #13
  14. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    I have a bit of a problem... I thought this was solved,(and the problem in the title has been), but I still can't properly EDIT the positions in real time. I can get the positions with $game_troop.enemies[1].screen_x, but when I try...

    $game_troop.enemies[1].screen_x += 32

    ... I get this error: NoMethodError occurred while running script.

                                undefined method 'screen_x=' for #<Game_Enemy:0x28ace60>

    Why can it read the variable, but not write it? I've been able to change these variables in the editor (at least, for the actors' starting positions)... so I don't quite understand what I'm missing. Is my syntax wrong? And if so, why does $data_troops[1].members[1].x += 32 work?

    I really do appreciate the help... I probably would have given up by now if not for all of you xD
     
    #14
  15. Heretic86

    Heretic86 Veteran Veteran

    Messages:
    240
    Likes Received:
    163
    First Language:
    Engrish
    Sorry that I missed your post, but I'll just repeat what was stated above.

    e_index = $game_troop.enemies.index(enemy)

    Then in the same script box, you can use

    $game_troop.enemies[e_index]

    It may make sense to read up on .index for Ruby.

    @hash = {:cat, :dog, :bird}

    index = hash.index:)bird) # Returns the Index, which is 2

    Works on Arrays also

    @array = [:cat, :dog, :tomato, :pizza]

    index = array.index:)tomato) # Again, 2

    Then you can use this:

    @array[index].attack

    @array[index].defeat_anim

    Any method that is available for Enemy Battlers can be called in this fashion.

    If you need the value to stick around, use a unique name and just put a @ in front of it.  Like @my_last_enemy_index = $game_troop...  Recommend not using globals ($) as they stick around forever and bloat the memory.  @my_unique_name gets dumped once the scene changes back to the Map, and without the @, my_unique_name gets dumped at the end of script execution, so it has to be done from within the same script box.

    Other tricks you can use is to find out your "active_battler", and how it is nested.  Not the best reply but I hope it helps.  Im not that familiar with VXA so probably couldnt point out the exact code needed to do this, making this not the best possible answer for your situation.
     
    #15
  16. Shaz

    Shaz Veteran Veteran

    Messages:
    37,956
    Likes Received:
    11,615
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    You can't change it because screen_x is a method, not a property. This is the Game_Enemy.screen_x method:

    def screen_x return $data_troops[@troop_id].members[@member_index].x end def screen_y return $data_troops[@troop_id].members[@member_index].y endSo if you want to change it, you'd have to change $data_troops[@troop_id].members[@member_index].x - which means you'd need to retrieve the troop id and member index first.@Heretic86, this question is about RMXP, not Ace. However, if you don't have Ace, you really shouldn't be giving script help for Ace, as the classes and scripts are VERY different from XP.
     
    Last edited by a moderator: Jan 31, 2015
    #16
  17. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    I think I understand now. So how do I retrieve the troop index? I've tried inputing $game_troop, but it says it can't be converted to an integer.
     
    #17
  18. Shaz

    Shaz Veteran Veteran

    Messages:
    37,956
    Likes Received:
    11,615
    Location:
    Australia
    First Language:
    English
    Primarily Uses:
    RMMV
    Go to the scripts (F11) and scroll down until you see Game_Troop. Click on it. Have a look at all the code there. THAT is Game_Troop (or $game_troop). An index is a number.


    It seems the troop_id passed into setup is not saved anywhere.


    Ahhh, there we go. The current troop id is saved in $game_temp.battle_troop_id - I found this by doing a global search (control+shift+f) for troop_id, as it didn't seem that Game_Troop made that info available anywhere.
     
    #18
    cloakedranger likes this.
  19. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    Brilliant :D I love how you explain your process as you go too... helps me understand things a LOT better. All I need now is the actor equivalent, and I should be set. But from the info you've already given me, I THINK I should be able to figure it out on my own... I'll give it a try and see :)
     
    #19
  20. cloakedranger

    cloakedranger Snail Developer Member

    Messages:
    39
    Likes Received:
    2
    First Language:
    English
    Ok, so it looks like there is no actor equivalent... I mean there is, but it's not quite the same. The actors' screen_x  is done by a formula rather than a variable from the database like the troops are. So what I had to do (and I don't know if this is the best way or not), was set the actor's screen_x according to a global variable, so that I could manipulate the global in real time.

    class Game_Actor  
      #--------------------------------------------------------------------------
      # * Get Battle Screen X-Coordinate
      #--------------------------------------------------------------------------
      def screen_x
        # Return after calculating x-coordinate by order of members in party
        if self.index != nil
          return $test
        else
          return 0
        end
      end

    It looks like maybe one could use the sprite_battler class to do this for both enemies and actors, but I have no idea how (something to do with the "self.x = @battler.screen_x" at the end of the script maybe?). I would like, for simplicity's sake, to have the same kind of script for both actors and enemies; but in essence this does do what I want it to (so I can at least move on now). :D
     
    #20

Share This Page