Announcement

Collapse
No announcement yet.

Tutorial Needed - Functions

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Tutorial Needed - Functions

    Hi gang,

    I need some help in understanding how to write functions. But first let me give you an example.

    I want to spawn in some enemies but I need some quality checks on where exactly I'm going to spawn them in. I want them to spawn in a building that is located at a random position generated 360 degrees around the players. This works great when the players are in the middle of Altis where there is no water within my spawn distance. But what if the players are up in the north west corner of the island with ocean on two sides (to the west and north).

    I want to call a function that will determine a random location and return it to the original script so that the original script can then check that the location is on land. If not, then it should be an easy thing to call the function again until a land position is determined at random.

    Is this the kind of thing functions are for? How do I handle the syntax of this? What is possible with them? Examples please!
    |TG-189th| Unkl
    ArmA 3 Game Officer
    Dean of Tactical Gamer University
    189th Infantry Brigade Member
    SUBMIT A RIBBON NOMINATION OR CONTACT AN ARMA ADMIN
    "We quickly advance in the opposite direction and take cover in a house on the SW side of town." - BadStache

  • #2
    Re: Tutorial Needed - Functions

    Originally posted by Unkl View Post
    I want to call a function that will determine a random location and return it to the original script so that the original script can then check that the location is on land. If not, then it should be an easy thing to call the function again until a land position is determined at random.

    Is this the kind of thing functions are for? How do I handle the syntax of this? What is possible with them? Examples please!
    Yes you can use functions like that. That said you can do one better than that. You can divide your entire logic into seperate functions. That means have one function to randomize your application, another one to check if it is valid, one to spawn the AI etc.. Basically if things logically belong together they probably belong into a function. That will make your code a lot easier to read, understand, update and reuse. Don't be afraid to write functions that are only 3 lines of code - if the lines belong together then it makes sense to put them in a seperate function.

    Ultimately you could that way have a library of useful functions that you can reuse across many missions. That's kind of what CBA and many other projects do.

    Here is a brief speudo-code example to illustrate the usefulness of functions:

    Without using functions
    Code:
    if(long condition) then {
    stuff;
    more stuff;
    even more complicated stuff;
    
    if(really long nested condition) {
    stuff;
    more stuff;
    }
    };
    With functions:
    Code:
    _condition1 = {
    long condition
    };
    
    _condition2 = {
    really;
    long;
    nested;
    condition;
    };
    
    _reuseableCode = {
    stuff;
    more stuff;
    };
    
    _complicatedCode = {
    even;
    more;
    complicated;
    stuff;
    };
    
    if(call _condition1) then {
    call _reusableCode;
    call _complicatedCode;
    
    if(call _condition2) {
    call _reusableCode;
    }
    };
    Here is how your case could look using functions.

    Code:
    //Returns a randomized poition based on an _origin and _maxradius
    randomizePosition = {
      private ["_origin", "_maxradius", "_x", "_y", "_z"];
      //Parameters
      _origin = _this select 0;
      _maxradius = _this select 1;
    
      //Calculate Position
      _x = (_origin select 0) + (random (_maxradius * 2)) - _maxradius;
      _y = (_origin select 1) + (random (_maxradius * 2)) - _maxradius;
      _z = 0;
    	_pos = [_x, _y, _z];
    
      //Return value
      _pos
    };
    
    //Returns if a position is valid to spawn infantry at
    isValidPosition = {
        private ["_pos"];
        //Parameter
        _pos = _this select 0;
    
        //Check if position is water
        _valid = !(surfaceIsWater _pos);
    
        //Do other checks...
    
        //Return value
        _valid
    };
    
    //Example Usage
    _p = [position player, 100] call randomizePosition;
    _v = [_p] call isValidPosition;
    hint format ["%1 + %2", _p, _v];

    Comment


    • #3
      Re: Tutorial Needed - Functions

      I will leave two links for reference. In terms of arma, I read somewhere that compiled functions are much much faster. I have been trying to use it almost exclusively in the missions I develop since then.
      https://community.bistudio.com/wiki/Function
      https://www.youtube.com/watch?v=-W1-rg429RM

      I have not checked this script out myself in depth. But this is one I have been using recently. I dunno if there would be any hints on how to accomplish what you want.
      http://www.armaholic.com/page.php?id=26343

      Some dynamic enemy spawning scripts:
      http://www.armaholic.com/page.php?id=20262
      http://www.armaholic.com/page.php?id=28107

      The simplest ones can be found in my latest ones using one of the BIS functions.
      https://community.bistudio.com/wiki/BIS_fnc_spawnGroup

      I will try to answer questions as they arise.

      Comment


      • #4
        Re: Tutorial Needed - Functions

        That is great...some stuff to chew on. Thanks guys! I'll pop back in with questions for sure.
        |TG-189th| Unkl
        ArmA 3 Game Officer
        Dean of Tactical Gamer University
        189th Infantry Brigade Member
        SUBMIT A RIBBON NOMINATION OR CONTACT AN ARMA ADMIN
        "We quickly advance in the opposite direction and take cover in a house on the SW side of town." - BadStache

        Comment


        • #5
          Re: Tutorial Needed - Functions

          I am beginning to see the power in functions here. While I try to work the editor and script as much as possible, this is definitely a better way to accomplish certain things.

          Current ARMA Development Project: No Current Project

          "An infantryman needs a leader to be the standard against which he can judge all soldiers."

          Friend of |TG| Chief

          Comment

          Connect

          Collapse

          TeamSpeak 3 Server

          Collapse

          Advertisement

          Collapse

          Twitter Feed

          Collapse

          Working...
          X