• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Solved] Pre-Loaded Functions

Status
Not open for further replies.
Level 7
Joined
Feb 9, 2021
Messages
301
I wanted to ask whether it is okay to use pre-loaded functions. What I mean by this is to create some functions beforehand and then use them in other spells. This significantly increases map saving time, so I wondered whether it can effect performance. Here is an example:

JASS:
#include "cj_types_priv.j"
hashtable h = InitHashtable()
group g = CreateGroup()
unit E = null
boolexpr Base
function Condition_Base takes player p,unit e returns boolean
   return IsUnitEnemy(e,p) && !IsUnitType(e,UNIT_TYPE_DEAD) && !IsUnitType(e,UNIT_TYPE_STRUCTURE)
endfunction
function BaseBool takes nothing returns boolean
   return GetUnitAbilityLevel(GetFilterUnit(),'Aloc') == 0 and not IsUnitType(GetFilterUnit(),UNIT_TYPE_DEAD) and not IsUnitType(GetFilterUnit(),UNIT_TYPE_STRUCTURE)
endfunction

function GetAngleBetweenPoints takes real x1, real x2, real y1, real y2 returns real
    real result = bj_RADTODEG * Atan2(y2 - y1, x2 - x1)
    return result
endfunction

function GetXWithOffset takes real x, real distance, real angle returns real
    local real xWithOffset = x + distance * Cos(angle * bj_DEGTORAD)
    return xWithOffset
endfunction

function GetYWithOffset takes real y, real distance, real angle returns real
    local real yWithOffset = y + distance * Sin(angle * bj_DEGTORAD)
    return yWithOffset
endfunction

function GetDistanceBetweenCoordinates takes real x1, real x2, real y1, real y2 returns real
    local real dx = x2 - x1
    local real dy = y2 - y1
    return SquareRoot(dx * dx + dy * dy)
endfunction

function setUnitPosition takes unit u, real x, real y returns nothing
    SetUnitX(u, x)
    SetUnitY(u, y)
    u = null
endfunction
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,197
You mean a collection of utility functions that you use throughout the script? Yes that is pretty standard and a recommended programming practice. Performance wise it should make close to no difference possibly in either direction over using copies of those functions unique to the scripts that use them. An exception would be very performance intensive sections of code (run hundreds or thousands of times per update) in which case inlining the logic is recommended to avoid function call overhead. In natively compiled programming languages like C/C++ it usually is beneficial to performance as it slightly improves the effectiveness of caches over having the same logic declared multiple times throughout the code, and the compiles usually automatically inline when it is best to do so.
 
Status
Not open for further replies.
Top