Jump to content
Sign in to follow this  
Atlas1205

is there a way to quickly add objects with same prefix to an array?

Recommended Posts

just an example, i have 5 game logics: obj_1, obj_2, obj_3, obj_4, obj_5

and i have an array named OBJ_ARY = [];

 

now how do i quickly add all varnames starting with obj to this array? if it's not many, it can be done manually. but when there are dozens of objects to add, it's really tedious to type them all.

 

Any method i can perform a "search & add" operation?

Share this post


Link to post
Share on other sites

Never mind, my code was working after all. i made a mistake by writing {entities "logics"}, which should be "logic".

patrol_logics = [];
outpost_logics = [];
tower_logics = [];
_alllogs = entities "Logic";
	  {
          if ((str _x) find "patrollog_" == 0) then
            {
			patrol_logics pushBackUnique _x;
            	}
					
       } forEach _alllogs;  
	  {
          if ((str _x) find "outpostlog_" == 0) then
            {
			outpost_logics pushBackUnique _x;
            	}
					
       } forEach _alllogs;  
	   	  {
          if ((str _x) find "towerlog_" == 0) then
            {
			tower_logics pushBackUnique _x;
            	}
					
       } forEach _alllogs;  

patrol_logics;// = [patrollog_1,patrollog_2,patrollog_3,patrollog_4,patrollog_5,patrollog_6];
outpost_logics;// = [outpostlog_1,outpostlog_2,outpostlog_3,outpostlog_4];
tower_logics;// = [towerlog_1];

 

Share this post


Link to post
Share on other sites

Yes. Yo can use also select (alternative syntax 3). And, anyway, for optimization, avoid useless characters like termination  l o g _  , (not discriminative). The shorter, the better.

Share this post


Link to post
Share on other sites

As @pierremgi said, the select command is a worthy alternative.

It can be both faster and easier to read.

 

Could be as simple as that:

_logics = entities "Logic";
patrol_logics = _logics select {str _x find "patrol_" >= 0};
outpost_logics = _logics select {str _x find "outpost_" >= 0};
tower_logics = _logics select {str _x find "tower_" >= 0};

 

Cheers

Share this post


Link to post
Share on other sites

I had on mind:

_logics = entities "logic";

patrol_logics = _logics select {(str _x) select [0,3] == "pat"};

outpost_logics = _logics select {(str _x) select [0,3] == "out"};

tower_logics = _logics select {(str _x) select [0,3] == "tow"};

 

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
Sign in to follow this  

×