Home   Help Search Login Register  

Author Topic: How to get leader from group name  (Read 2023 times)

0 Members and 1 Guest are viewing this topic.

Offline mcnorth

  • Members
  • *
How to get leader from group name
« on: 14 Mar 2007, 22:33:49 »
Can anyone tell me how to get the leader from a group after the group name has been passed to a script?

I'm trying to determine the distance between the player and an enemy group leader for spawning purposes. I want to work with the group name rather than the enemy leader name so that if the leader is killed the script will then work with the new leader.

Any tips are appreciated.

Thanks

Offline myke13021

  • Contributing Member
  • **
  • Myke
Re: How to get leader from group name
« Reply #1 on: 14 Mar 2007, 22:43:17 »
inside the script this should work

Code: [Select]
_myleader = leader groupname

Offline mcnorth

  • Members
  • *
Re: How to get leader from group name
« Reply #2 on: 15 Mar 2007, 02:58:26 »
Thanks Myke but there's still a problem with this.

_myleader = leader groupname

will work if groupname is the global variable defined in the leaders init field. ie "groupname = group this"

But after I pass groupname to a variable in the script it errors.

// Return the distance between two units;
// define variables for units;
private ["_Aunit", "_Bunit", "_grp"];

_Aunit = _this select 0;
_grp = this select 1;                            <---------- This line errors
_Bunit = leader grp;

// get the x and y coord of the units;
_pos = GetPos _Aunit;
_xA = _pos select 0;

I think it says error select: Type object, expected array, Config entry.

Any ideas?

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: How to get leader from group name
« Reply #3 on: 15 Mar 2007, 03:40:38 »
Quote
private ["_Aunit", "_Bunit", "_grp"];

_Aunit = _this select 0;
_grp = this select 1;                            <---------- This line errors
_Bunit = leader grp;

Surely _grp is select 2 in the above array?

Or did I miss something out?


Planck
I know a little about a lot, and a lot about a little.

Offline mcnorth

  • Members
  • *
Re: How to get leader from group name
« Reply #4 on: 15 Mar 2007, 05:38:04 »
Since the script is called with,

nul = [aP, squad] execVM "distance.sqf"

wouldn't "select 0" be how you assign the first variable and "select 1" how you assign the second one?

I should have mentioned it's a .sqf file. Does that make a difference?

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: How to get leader from group name
« Reply #5 on: 15 Mar 2007, 08:16:05 »
I think Planck just misunderstood there a bit..  :scratch:
To me it seems he thought the private [] bit was the array you called the script with..

Anyway, there is no difference in sqf how the arrays are handled.
I think that your script 'errors up' because you are missing the underscore on this in that line you point out, should be _this select 1;
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline mcnorth

  • Members
  • *
Re: How to get leader from group name
« Reply #6 on: 15 Mar 2007, 08:50:04 »
Hello -h,

You are dead right!!! Thanks for curing my headache. Apparently the "private []" isn't necessary either. No telling where I picked that up from.

Thanks to everyone that helped out here.

And for anyone else that may come along, here's the whole script. (distance.sqf)

Code: [Select]
// Return the distance between 2 units;
// Called with nul = [player, group] execVM "Distance.sqf";
_Aunit = _this select 0;
_Bunit = leader (_this select 1);

// get the x and y coord of the units;
_pos = GetPos _Aunit;
_xA = _pos select 0;
_yA = _pos select 1;

//Titletext [Format ["Aunit's x coord is %1 and y coord is %2", _xA, _yA], "Plain"];

_pos = GetPos _Bunit;
_xB = _pos select 0;
_yB = _pos select 1;

// dist equals sq root of diff x axis' squared plus diff y axis' squared;
_dist = sqrt ((_xA - _xB)^2 + (_yA - _yB)^2);

// display the distance;
Titletext [Format ["Distance from %1 to %2 is: %3", _Aunit, _Bunit, _dist], "Plain"];
« Last Edit: 15 Mar 2007, 17:49:27 by mcnorth »

Offline h-

  • OFPEC Site
  • Administrator
  • *****
  • Formerly HateR_Kint
    • OFPEC
Re: How to get leader from group name
« Reply #7 on: 15 Mar 2007, 09:07:00 »
private [] was necessary at least in OFP with functions (sqf)..
Otherwise the variables in the function were not in it's private scope, and could interfere with variables with the same name being used elsewhere in the script that called the function..

Don't know about the sqf scripts in ArmA (executed with execVM) but I would use them in functions because I'm pretty sure the above OFP reason applies to them..


And please use the code tags when posting a bit longer pieces of code, makes it easier to read :)
Project MCAR   ---   Northern Fronts   ---   Emitter 3Ditor
INFORMATIVE THREAD TITLES PLEASE. "PLEASE HELP" IS NOT ONE..
Chuck Norris can divide by zero.

Offline Planck

  • Honoured
  • Former Staff
  • ****
  • I'm never wrong ....I'm just not always right !
Re: How to get leader from group name
« Reply #8 on: 15 Mar 2007, 11:53:38 »
Ahhh yes, private of course is just a list of variables private to a private in private.

Yes, thought I was missing something there.  :D


Planck
I know a little about a lot, and a lot about a little.

Offline mcnorth

  • Members
  • *
Re: How to get leader from group name
« Reply #9 on: 15 Mar 2007, 17:57:27 »
private [] was necessary at least in OFP with functions (sqf)..
Otherwise the variables in the function were not in it's private scope, and could interfere with variables with the same name being used elsewhere in the script that called the function..

Don't know about the sqf scripts in ArmA (executed with execVM) but I would use them in functions because I'm pretty sure the above OFP reason applies to them..

That would make sense. I can't see ArmA going backwards from OFP

Quote
And please use the code tags when posting a bit longer pieces of code, makes it easier to read :)

Sorry about that. I haven't been around here for a very long time and have to get re-acquainted. :)