Home   Help Search Login Register  

Author Topic: Pop Quiz - just why does this function work?  (Read 1117 times)

0 Members and 1 Guest are viewing this topic.

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Pop Quiz - just why does this function work?
« on: 19 Jan 2006, 03:30:02 »
The following function is used for spawning:
SpawnSquad.sqf
Code: [Select]
Private ["_squadtype","_location","_group","_behaviour","_combatmode","_formation","_itemnum","_unit",
"_unitClass","_unitCount","_unitType","_unitInit","_unitSkill","_unitRank","_count"];
_squadtype = _this Select 0;
_location = _this Select 1;
_group = _this Select 2;
_behaviour = _this Select 3;
_combatmode = _this Select 4;
_formation = _this Select 5;
_itemnum = 0;
If (local server) Then
{
    While {_itemnum < count _squadtype} Do
    {
         _unit = _squadtype Select _itemnum;
         _unitClass = _unit Select 0;
         _unitCount = _unit Select 1;
         _unitType = _unit Select 2;
         _unitInit = _unit Select 3;
         _unitSkill = _unit Select 4;
         _unitRank = _unit Select 5;
         _count = 0;
         While {_count < _unitCount} Do
         {
             _unitClass createUnit [GetPos _location, _group, _unitInit, _unitSkill, _unitRank];
             _count = _count + 1;
             If (_count == 1 && _itemnum == 0) Then
             {
                  _newLeader = NearestObject [_location, _unitClass];
                  [_newLeader] Join GRPNULL; _group = _newLeader;
             };
         };
         _itemnum = _itemnum + 1;
    };
    {
   _x SetCombatMode _combatmode;
   _x SetBehaviour _behaviour;
   _x RemoveAllEventHandlers "killed";
   _x SetFormation _formation;
   _x AddEventHandler [{Killed}, {_this Exec {OPFOR_Delete.sqs}}];
    } ForEach units _group;
    TRUE
}
Else
{
    FALSE
};
_group

Is is called via the following line:
Code: [Select]
[civiGroup, spawn1, dummy, combat, red, column] call SpawnSquad
where
Code: [Select]
CiviGroup = [["Civilian",3,"sld","[this] exec {civiAK47.sqs}",0.7,"CORPORAL"],["Civilian2",3,"sld","[this] exec {civiFAL.sqs}",0.7,"PRIVATE"],["Civilian3",3,"sld","[this] exec {civiG3.sqs}",0.7,"PRIVATE"],["Civilian4",3,"sld","[this] exec {civiM16.sqs}",0.7,"PRIVATE"]];
;["Civilian2",1,"sld","[this] exec {civiFAL.sqs}",0.7,"PRIVATE"]
(note: the above is all one line)

spawn1 is a game logic
dummy is a single unit of the desired side placed in some far away region of the map.

It works like a charm, but I do not fully understand why.
Here are my questions:

1) On the first iteration loop, the function takes the first unit spawned and assigns it to GRPNULL. I thought OFP could not create groups at run time, so what is the actual group of the spawned squad?

2) Does spawned squad actually have a real leader?

3) I thought lag in missions was related to the number of groups on a map. Would this method of spawning create more lag since it is in essence adding more groups at run time, instead of spawning from old groups?

4) Unit dummy is passed into the function as private variable _group.  _group is assigned to _newLeader The function then uses _group as the group name for the CreateUnit command. This makes it seem like the leader of a group can be used as the group. I have not seen this documented on OFPEC. Furthermore, since _group has been altered in the function wil the original unit dummy be affected, or was it passed by value and not reference?

5) Do the TRUE and FALSE statements do anything?

6) In the line that calls the function, red, column and combat are passed as arguments, not as string arguments.  Are these reserved words automatically treated as strings?



#Long line of code broken so post fits on page - maguba[/size]
« Last Edit: 19 Jan 2006, 16:56:52 by macguba »
urp!

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Pop Quiz - just why does this function work?
« Reply #1 on: 19 Jan 2006, 03:49:58 »
1) not sure what you mean "at run time" but the spawned group will be its own brand new group

2)afaik the first unit spawned will be the leader as all other join under him

3)it does spawn from old groups (the dummy group) but they can't stay in that group because a. it would include the dummy guy and b. the function would only work once

4)if this works then it appears that you can use group leaders as a group for at least this purpose. (b) no, dummy will be unaffected (i'm assuming you're talking about the variablename "dummy" and its reference to the dummy soldier) _group will take on a new value, that of _newleader, dropping its original value that was equal to that of dummy

5)read snYpir's function tutorial thing. its very good practice for a function to return some value

6)not sure, but that definitely appears to be what's happening

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Pop Quiz - just why does this function work?
« Reply #2 on: 19 Jan 2006, 10:37:25 »
Hmmm.

I'm a bit fuzzy about what 'grpNull' actually is. I do know, however, that the concept 'grpnull' is not a group. It's more (as you say) a flag to create a new group. However, this call to create a 'new' group can only be run on an already-existing, alive unit. This is why you can say

[_unit] join grpnull
_unit2 = createunit [ ... group _unit ...]

as a result, _unit and _unit2 are in their own special group. However you cannot spawn directly into grpnull.

In my travels, I have also seen an apparent overflow of groups (guess I was hitting the 63 groups per side limit) created using GrpNull; after a while (in this script of mine), trying to force a unit into a new group via join grpnull did not work, and said unit stayed put in his old group.
« Last Edit: 19 Jan 2006, 10:38:20 by Fragorl »

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:Pop Quiz - just why does this function work?
« Reply #3 on: 19 Jan 2006, 13:59:25 »
1) By run time I mean while the game is being played.  I was positive I had read on the forums that all groups must be placed at design time.

5) I thought the function is returning _group, so I still don't see the purpose of the TRUE and FALSE

1) not sure what you mean "at run time" but the spawned group will be its own brand new group
5)read snYpir's function tutorial thing. its very good practice for a function to return some value
urp!

Offline Mr.Peanut

  • Former Staff
  • ****
  • urp!
Re:Pop Quiz - just why does this function work?
« Reply #4 on: 19 Jan 2006, 14:03:50 »
I know that GRPNULL is being used to separate the new group from the old, but does OFP treat the new "group" as a real group?  Will it automatically be assigned a designation e.g. East Bravo Black ?  

...
I'm a bit fuzzy about what 'grpNull' actually is. I do know, however, that the concept 'grpnull' is not a group. It's more (as you say) a flag to create a new group. However, this call to create a 'new' group can only be run on an already-existing, alive unit. This is why you can say

[_unit] join grpnull
_unit2 = createunit [ ... group _unit ...]

as a result, _unit and _unit2 are in their own special group. However you cannot spawn directly into grpnull.
...
urp!

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Pop Quiz - just why does this function work?
« Reply #5 on: 19 Jan 2006, 22:22:24 »
1) By run time I mean while the game is being played.  I was positive I had read on the forums that all groups must be placed at design time.

5) I thought the function is returning _group, so I still don't see the purpose of the TRUE and FALSE


1) no you can create groups while the game is running

5) ahh yeah you're right... have you tried taking out the true/false bits and see what happens?

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Pop Quiz - just why does this function work?
« Reply #6 on: 20 Jan 2006, 15:25:47 »
Again, I don't know if what i'm saying is 100% ... but the results of groupnulls seem to be that you  can put units together in more groups than you had to start with.

Also, just because a grpnul-created group doesn't have a designation (such as East Bravo Black), doesn't mean that it can't function exactly as a separate group does...

I take it back; that's not what you said. The answer is yes, a grpnull-created group does get it's own designation, such as Bravo Black etc.
« Last Edit: 20 Jan 2006, 15:30:56 by Fragorl »