Getting the position of Enemies/Actors in battle

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
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.
 

Heretic86

Veteran
Veteran
Joined
Nov 30, 2014
Messages
240
Reaction score
167
First Language
Engrish
Primarily Uses
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
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.
 

SuperMasterSword

That Guy You Dont Know
Veteran
Joined
Jun 28, 2014
Messages
117
Reaction score
48
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]).
 

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
 is there a way to set that to a variable?
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:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
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.
 

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
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.
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'".
 

SuperMasterSword

That Guy You Dont Know
Veteran
Joined
Jun 28, 2014
Messages
117
Reaction score
48
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
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.
 

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
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 :)
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 :)
 

SuperMasterSword

That Guy You Dont Know
Veteran
Joined
Jun 28, 2014
Messages
117
Reaction score
48
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
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 :)
 

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
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.
 

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
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
 

Heretic86

Veteran
Veteran
Joined
Nov 30, 2014
Messages
240
Reaction score
167
First Language
Engrish
Primarily Uses
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.
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
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:

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
You can't change it because screen_x is a method, not a property. 

So 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.
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
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.
 

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
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.
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 :)
 

cloakedranger

Snail Developer
Member
Joined
Dec 5, 2014
Messages
39
Reaction score
2
First Language
English
Primarily Uses
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
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c

Forum statistics

Threads
105,857
Messages
1,017,018
Members
137,563
Latest member
MinyakaAeon
Top