Jump to content
Rook Mk1

unit has string in name

Recommended Posts

So say if I spawned an opfor unit, and only wanted to make a script  activate if they had a certain name followed by any number. Say badman_1 or badman_25 etc. How would I check for that?

 

I tried things like if (vehicleVarName _this == "badman_%1") then {code} but it hasn't worked.

Share this post


Link to post
Share on other sites
2 hours ago, Rook Mk1 said:

So say if I spawned an opfor unit, and only wanted to make a script  activate if they had a certain name followed by any number. Say badman_1 or badman_25 etc. How would I check for that?

 

I tried things like if (vehicleVarName _this == "badman_%1") then {code} but it hasn't worked.

 

EDIT: use foley's solution, not mine as mine is very inefficient. I'm leaving it here just as an example.


This should be doable, although not particularly efficient, with a loop from 1 to 25 (or whatever the highest possible number would be) which uses compile on strings created with format

Something like:
 

private ["_i", "_matched"];
_i = 1;
_matched = false;
while { (_i < 26) and (not _matched) } do {
  private ["_unit"];
  _unit = call compile format ["badman_%1", _i];
  if ((not isNil "_unit") and
      (_this == _unit)) then {
    _matched = true;
  };
  _i = _i + 1;
};
_matched

Assuming _this represents the unit as in your example, then at the end _matched will be a boolean true or false, thus the snippet can be used in a trigger condition, or at the end you can do "if (_matched) then {<code>}".

You can confirm that the snippet works by creating a mission, dropping in a man named badman_12 as the player, then starting it up and pasting the code in the extended debug console replacing "_this" with "player" followed by changing the last line to

hint format ["%1", _matched];

which will report "true"

  • Like 1

Share this post


Link to post
Share on other sites

If by name you mean the variable name specified in editor in unit's properties, then you're on the right track. Try this:

if ((vehicleVarName _unit) regexMatch "badman_[0-9]+") then {
	// _unit is badman
};

A more flexible alternative would be to use setVariable and getVariable.

After spawning the unit (or in init code) you can set a property, for example:

_unit setVariable ["isBadman", true];

And then you can differentiate if a given unit has this property like so:

if (_unit getVariable ["isBadman", false]) then {
	// _unit is badman
};

 

  • Like 4

Share this post


Link to post
Share on other sites

Oh that's awesome, I didn't realize there was regex matching built in.  Learn something new every day even after nearly two decades, lol.

EDIT: well at least I see that it was a relatively recent addition, heh.

Share this post


Link to post
Share on other sites
1 minute ago, dwringer said:

Oh that's awesome, I didn't realize there was regex matching built in.  Learn something new every day even after nearly two decades, lol.

It's quite new actually, added last year.

Share this post


Link to post
Share on other sites
44 minutes ago, _foley said:

It's quite new actually, added last year.

 

Nifty, I've always gone the toArray route. I'm going to convert one of my scripts and check performance tonight.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×