[Request] Removing BJs tutorial

jig7c

Stop reading me...-statement
Reaction score
123
Hello readers,

I need a tutorial, if possible, on removing BJs from jass scripts... from what i understand, BJs are slow, they suck, etc...
 

Executor

I see you
Reaction score
57
There are 3 types of BJs used (or not used) in JASS. (BJ = BlizzardJass).

First type

Well, many "BJ"s are simply wrappers for natives, for example:

JASS:
function AddItemToStockBJ takes integer itemId, unit whichUnit, integer currentStock, integer stockMax returns nothing
    call AddItemToStock(whichUnit, itemId, currentStock, stockMax)
endfunction

native AddItemToStock takes unit whichUnit, integer itemId, integer currentStock, integer stockMax returns nothing

function AttachSoundToUnitBJ takes sound soundHandle, unit whichUnit returns nothing
    call AttachSoundToUnit(soundHandle, whichUnit)
endfunction

native AttachSoundToUnit takes sound soundHandle, unit whichUnit returns nothing


These can be easily replaced by using the natives.

Second type

Another type of "BJ"s are BJs with GUI callback values:

JASS:
function CreateUnitAtLocSaveLast takes player id, integer unitid, location loc, real face returns unit
    if (unitid == 'ugol') then
        set bj_lastCreatedUnit = CreateBlightedGoldmine(id, GetLocationX(loc), GetLocationY(loc), face)
    else
        set bj_lastCreatedUnit = CreateUnitAtLoc(id, unitid, loc, face)
    endif

    return bj_lastCreatedUnit
endfunction


You should not use them, cause they only set bj_lastCreatedUnit to the last unit created, but you don't need this in JASS as you can easily set the var yourself:

JASS:
local unit u = CreateUnit(...)


Third type

But, there are also useful BJs, which can be used:

JASS:
function BlightGoldMineForPlayerBJ takes unit goldMine, player whichPlayer returns unit
    local real    mineX
    local real    mineY
    local integer mineGold
    local unit    newMine

    // Make sure we're replacing a Gold Mine and not some other type of unit.
    if GetUnitTypeId(goldMine) != 'ngol' then
        return null
    endif

    // Save the Gold Mine's properties and remove it.
    set mineX    = GetUnitX(goldMine)
    set mineY    = GetUnitY(goldMine)
    set mineGold = GetResourceAmount(goldMine)
    call RemoveUnit(goldMine)

    // Create a Haunted Gold Mine to replace the Gold Mine.
    set newMine = CreateBlightedGoldmine(whichPlayer, mineX, mineY, bj_UNIT_FACING)
    call SetResourceAmount(newMine, mineGold)
    return newMine
endfunction

function RAbsBJ takes real a returns real
    if (a >= 0) then
        return a
    else
        return -a
    endif
endfunction


All in all:
- Replace the first type of BJs with the matching natives
- Never use the second type, as you can use the return value directly
- You may use the third type
 

jig7c

Stop reading me...-statement
Reaction score
123
looking good...
now the question is how do i know which ones to use and which ones not to use..

this trigger has few BJs, which im sure can be removed, but i dont' know how to go about doing it...
JASS:
private function team1win takes nothing returns nothing
    call CustomVictoryBJ ( GetEnumPlayer(), true, true )
endfunction

private function team2lose takes nothing returns nothing
    local string s = "The Palace is Destroyed!!"
    call CustomDefeatBJ( GetEnumPlayer(), s)
    set s = null
endfunction

private function Pause takes nothing returns boolean
    local unit u = GetFilterUnit ()
    call PauseUnit (u, true)
    return true
endfunction

private function Actions takes nothing returns nothing
    local rect r = GetPlayableMapRect()
    local group g = GetUnitsInRectAll(r)
    call GroupEnumUnitsInRect (g,r,functions Pause)
    call TriggerSleepAction( 2 )
    call ForForce( GetPlayersAllies(Player(10)), function team1win )
    call ForForce( GetPlayersAllies(Player(11)), function team2lose )
endfunction

//===========================================================================
private function InitTrig takes nothing returns nothing
    local team1 = CreateTrigger()
    call TriggerRegisterUnitEvent(team1, gg_unit_ofrt_0002, EVENT_UNIT_DEATH )
    call TriggerAddAction(team1, function Actions )
endfunction


JASS:
scope Ping initializer Init
   private function Actions takes nothing returns nothing // Private, so the name does not collide.
         local real x = GetUnitX(GetEnumUnit()) // Set the horizontal position of the unit.
                                                   // EnumUnit is JASS for 'Picked Unit'.
         local real y = GetUnitY(GetEnumUnit()) // Set the vertical position of the unit.
                                                  // Notice that the native is 'GetUnitY'.
         call PingMinimap(x,y,3.00)              // Pings the location where they interlap.
                                                        // This is faster than PingMinimapForForce(FORCE,x,y,3).
   endfunction

  private function Action2 takes nothing returns nothing
    local integer i = 'hpea'
    local group g = GetUnitsOfTypeIdAll(i)
    call ForGroup (g, function Actions)
    call DestroyGroup (g)
    set g = null
  endfunction

  function Init takes nothing returns nothing
       local trigger t = CreateTrigger()        // A local trigger is more 'portable'. (Acording to Vex.)
       call TriggerAddAction(t,function Actions)
      call TriggerRegisterTimerEventSingle( Ping, 10.00 )
  endfunction
endscope


the above script with BJs got turned into the bottom script, w/o BJs, obviously komaqtion did it for me...

JASS:
scope Ping initializer Init

    globals
        private constant string UNIT_NAME = "Peasant"
        
        private group ENUM = CreateGroup()
    endglobals

    private function EnumActions takes nothing returns boolean
        local unit u = GetFilterUnit()
        local real x = GetUnitX( u )
        local real y = GetUnitY( u )
        
        call PingMinimap(x,y,3.00)
        
        return false
    endfunction

    private function Actions takes nothing returns boolean
        call GroupEnumUnitsOfType( ENUM, UNIT_NAME, Filter( function EnumActions ) )
        
        return false
    endfunction

    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        
        call TriggerRegisterTimerEvent( t, 1.00, false )
        call TriggerAddCondition( t, Condition( function Actions ) )
    endfunction
    
endscope


and thats what i need to know.. how do i convert BJs into native functions

would i be asking too much if someone posted a list of all the BJs that are good to use and which ones aren't..
 

Executor

I see you
Reaction score
57
The best method is to code in the NewGen. There is a function list button, which opens a window, where you can enter the name of a function and by clicking on the function you get the source code.

The alternative would be http://wiki.thehelper.net/wc3/jass/Blizzard.j and search the bj you'd like to check with [ctrl + f].
 

Executor

I see you
Reaction score
57
komaqtion did the following things:
  • replaced TriggerRegisterTimerEventSingle with the native ("single"s are also native wrappers)
  • replaced the group create/destroy with one global group (there are tutorials out there to this topic)
  • replaced the 2 functions calls "GetEnumUnit()" with one call by using a local var
 

Romek

Super Moderator
Reaction score
964
My Tutorial covers this.
Search for "Natives and BJs".
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top