• 🏆 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!

[JASS] Library or scope?

Status
Not open for further replies.
Level 11
Joined
Apr 6, 2008
Messages
760
i'v been experimenting abit just wondeing if there is really any diffrent between scopes and librarys...

is it really good 2 do this?

look at this code it uses CSSafety and CSData and i did library it to need CSSAfety(only reason) and the spell works just fine :)

JASS:
library SonicBlast initializer Init needs CSSafety
//!=============================================================================!\\
//! Sonic Blast by Ciebron.                                                    !\\
//! Give Credits is you use this in your map                                    !\\
//! --------------------------------------------------------------------------- !\\
//! Requires:                                                                   !\\
//! ¯¯¯¯¯¯¯¯¯                                                                   !\\
//! - Object Editor  - A hero                                                   !\\
//! - Object Editor  - The Orb of Zeuz ability                                  !\\
//! - Object Editor aadada - The Orb Dummy Unit                                 !\\
//! - Trigger Editor - This Trigger                                             !\\
//! - Trigger Editor - CSData - by Vexorian                                     !\\
//! - Trigger Editor - CSSaftey - by Vexorian                                   !\\
//!   ([url]http://www.wc3campaigns.net/showthread.php?t=80534][/url])                      !\\
//!                                                                             !\\
//!=============================================================================!\\
//!=================================Setup Starts================================!\\
//!=============================================================================!\\
globals
private constant integer Abil_id = 'A002' //! Ability RawCode
private constant integer Dummy_id = 'h001' //! Dummy Unit RawCode
private constant real Time = 1. //! Time to move Dist
private constant real Dist = 1000. //! Distance to move
private constant real Aoe = 200. //! Aoe to pick units Around the dummy
endglobals
//!=============================================================================!\\  
//!=================================Setup Ends==================================!\\
//!=============================================================================!\\
private struct Data
unit c
unit dum

real cos
real sin
real dist
real movedist

integer lvl

group g

timer t

player play
    method onDestroy takes nothing returns nothing
        call KillUnit(.dum)
        call ReleaseTimer(.t)
        call ReleaseGroup(.g)
    endmethod
endstruct

private function preload takes nothing returns nothing
    call RemoveUnit(CreateUnit(Player(15),Dummy_id,0.,0.,0.))
endfunction

private function UnitFilter takes nothing returns boolean
    local Data d = GetCSData(GetExpiredTimer())
    local unit f = GetFilterUnit()
    local boolean ok = GetWidgetLife(f) > 0. and not IsUnitType(f,UNIT_TYPE_MAGIC_IMMUNE) and not IsUnitType(f,UNIT_TYPE_STRUCTURE) and not IsUnitInGroup(f,d.g) and IsUnitEnemy(f,d.play)
    set f = null
    return ok
endfunction

private function Move takes nothing returns nothing
    local Data d = GetCSData(GetExpiredTimer())
    local real x = GetUnitX(d.dum)+d.movedist*d.cos
    local real y = GetUnitY(d.dum)+d.movedist*d.sin
    local group g = NewGroup()
    local unit a
    
    set d.dist = d.dist + d.movedist
    
    if MinX < x and MaxX > x then
        call SetUnitX(d.dum,x)
    endif
    if MinY < y and MaxY > y then
        call SetUnitY(d.dum,y)
    endif
    call GroupEnumUnitsInRange(g,x,y,Aoe,Filter(function UnitFilter))
    
    loop
        set a = FirstOfGroup(g)
        exitwhen a == null
        call GroupRemoveUnit(g,a)
        call UnitDamageTarget(d.c,a,75*d.lvl,false,false,ATTACK_TYPE_MAGIC,null,null)
        call Knockback(a,200.,Atan2(GetUnitY(a)-y,GetUnitX(a)-x),I2R(d.lvl)/2)
        call GroupAddUnit(d.g,a)
    endloop
    
    if d.dist >= Dist then
        call d.destroy()
    endif
    
    call ReleaseGroup(g)
    set g = null
endfunction
    
private function Actions takes nothing returns nothing
    local Data d = Data.create()
    local location loc = GetSpellTargetLoc()
    local real tx = GetLocationX(loc)
    local real ty = GetLocationY(loc)
    local real x
    local real y
    local real angle
    
    set d.c = GetTriggerUnit()
    set d.play = GetOwningPlayer(d.c)
    set x = GetUnitX(d.c)
    set y = GetUnitY(d.c)
    set d.lvl = GetUnitAbilityLevel(d.c,Abil_id)
    set d.dist = 0
    set angle = Atan2(ty-y,tx-x)*57.27589
    set d.cos = Cos(angle*3.14159/180.)
    set d.sin = Sin(angle*3.14159/180.)
    set d.movedist = (Dist/Time)/(1./.035)
    set d.dum = CreateUnit(d.play,Dummy_id,x+50*d.cos,y+50*d.sin,angle)
    set d.g = NewGroup()
    set d.t = NewTimer()
    
    call SetCSData(d.t,d)
    
    call TimerStart(d.t,.035,true,function Move)
    
    call RemoveLocation(loc)
    set loc = null
endfunction

private function FilterFunc takes nothing returns boolean
    return GetSpellAbilityId()==Abil_id
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer index = 0

    loop
        call TriggerRegisterPlayerUnitEvent(t,Player(index),EVENT_PLAYER_UNIT_SPELL_EFFECT,null)

        set index = index + 1
        exitwhen index == bj_MAX_PLAYER_SLOTS
    endloop
    
    call TriggerAddAction(t,function Actions)
    call TriggerAddCondition(t,Filter(function FilterFunc))
    call preload()
    
    set t = null
endfunction

endlibrary
 
Level 23
Joined
Nov 29, 2006
Messages
2,482
Well the only thing I've found is this:
Libraries

Libraries are groups of functions that will be placed before any others in the map script. Useful if you have a function that is called by others.

Scopes

Scopes are similar to libraries, though they don't put the contained code at the top. Useful if you want to have private stuff.

But well, I dont know if it is more differences...
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
Scopes idea is so that they will give a prefix to everything inside of them(an stacking with other codes solution).
While libraries do that(as a bonus I guess) their main purpose is to move the code to the top(note that WE seems to put triggers in the actual script of the map the way it likes to, so it really messes systems that do not have libraries).
 
Last edited:
Level 11
Joined
Apr 6, 2008
Messages
760
got one more question, i heard that using method create will lower the chance for buggs and stuff. So i changed the code to that but if u ask me i look like in increase the chance ^^

does it really help? or did i do it wrong


p.s CSData is that really good? :S seem like a simple system..

JASS:
library SonicBlast initializer Init needs CSSafety,CSData,Knockback,MapLimits
//!=============================================================================!\\
//! Sonic Blast by Ciebron.                                                    !\\
//! Give Credits is you use this in your map                                    !\\
//! --------------------------------------------------------------------------- !\\
//! Requires:                                                                   !\\
//! ¯¯¯¯¯¯¯¯¯                                                                   !\\
//! - Object Editor  - A hero                                                   !\\
//! - Object Editor  - The Orb of Zeuz ability                                  !\\
//! - Object Editor aadada - The Orb Dummy Unit                                 !\\
//! - Trigger Editor - This Trigger                                             !\\
//! - Trigger Editor - CSData - by Vexorian                                     !\\
//! - Trigger Editor - CSSaftey - by Vexorian                                   !\\
//!   ([url]http://www.wc3campaigns.net/showthread.php?t=80534[/url])                      !\\
//!                                                                             !\\
//!=============================================================================!\\
//!=================================Setup Starts================================!\\
//!=============================================================================!\\
globals
private constant integer Abil_id = 'A002' //! Ability RawCode
private constant integer Dummy_id = 'h001' //! Dummy Unit RawCode
private constant real Time = 1. //! Time to move Dist
private constant real Dist = 1000. //! Distance to move
private constant real Aoe = 200. //! Aoe to pick units Around the dummy
endglobals
//!=============================================================================!\\  
//!=================================Setup Ends==================================!\\
//!=============================================================================!\\
private struct Data
unit c
unit dum

real cos
real sin
real dist
real movedist

integer lvl

group g

timer t

player play
    
    static method create takes nothing returns Data //this really helps?
        local Data d = Data.allocate()
        local location loc = GetSpellTargetLoc()
        local real tx = GetLocationX(loc)
        local real ty = GetLocationY(loc)
        local real x
        local real y
        local real angle
        
        set d.c = GetTriggerUnit()
        set d.play = GetOwningPlayer(d.c)
        set x = GetUnitX(d.c)
        set y = GetUnitY(d.c)
        set d.lvl = GetUnitAbilityLevel(d.c,Abil_id)
        set d.dist = 0
        set angle = Atan2(ty-y,tx-x)*57.27589
        set d.cos = Cos(angle*3.14159/180.)
        set d.sin = Sin(angle*3.14159/180.)
        set d.movedist = (Dist/Time)/(1./.035)
        set d.dum = CreateUnit(d.play,Dummy_id,x+50*d.cos,y+50*d.sin,angle)
        set d.g = NewGroup()
        set d.t = NewTimer()
        
        call RemoveLocation(loc)
        set loc = null
        
        return d
    endmethod

    method onDestroy takes nothing returns nothing
        call KillUnit(.dum)
        call ReleaseTimer(.t)
        call ReleaseGroup(.g)
    endmethod
endstruct

private function preload takes nothing returns nothing
    call RemoveUnit(CreateUnit(Player(15),Dummy_id,0.,0.,0.))
endfunction

private function UnitFilter takes nothing returns boolean
    local Data d = GetCSData(GetExpiredTimer())
    local unit f = GetFilterUnit()
    local boolean ok = GetWidgetLife(f) > 0. and not IsUnitType(f,UNIT_TYPE_MAGIC_IMMUNE) and not IsUnitType(f,UNIT_TYPE_STRUCTURE) and not IsUnitInGroup(f,d.g) and IsUnitEnemy(f,d.play)
    set f = null
    return ok
endfunction

private function Move takes nothing returns nothing
    local Data d = GetCSData(GetExpiredTimer())
    local real x = GetUnitX(d.dum)+d.movedist*d.cos
    local real y = GetUnitY(d.dum)+d.movedist*d.sin
    local group g = NewGroup()
    local unit a
    
    set d.dist = d.dist + d.movedist
    
    if MinX < x and MaxX > x then
        call SetUnitX(d.dum,x)
    endif
    if MinY < y and MaxY > y then
        call SetUnitY(d.dum,y)
    endif
    call GroupEnumUnitsInRange(g,x,y,Aoe,Filter(function UnitFilter))
    
    loop
        set a = FirstOfGroup(g)
        exitwhen a == null
        call GroupRemoveUnit(g,a)
        call UnitDamageTarget(d.c,a,75*d.lvl,false,false,ATTACK_TYPE_MAGIC,null,null)
        call Knockback(a,200.,Atan2(GetUnitY(a)-y,GetUnitX(a)-x),I2R(d.lvl)/2)
        call GroupAddUnit(d.g,a)
    endloop
    
    if d.dist >= Dist then
        call d.destroy()
    endif
    
    call ReleaseGroup(g)
    set g = null
endfunction
    
private function Actions takes nothing returns nothing
    local Data d = Data.create()

    call SetCSData(d.t,d)
    call TimerStart(d.t,.035,true,function Move)
endfunction

private function FilterFunc takes nothing returns boolean
    return GetSpellAbilityId()==Abil_id
endfunction

private function Init takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer index = 0

    loop
        call TriggerRegisterPlayerUnitEvent(t,Player(index),EVENT_PLAYER_UNIT_SPELL_EFFECT,null)

        set index = index + 1
        exitwhen index == bj_MAX_PLAYER_SLOTS
    endloop
    
    call TriggerAddAction(t,function Actions)
    call TriggerAddCondition(t,Filter(function FilterFunc))
    call preload()
    
    set t = null
endfunction

endlibrary
 
Level 12
Joined
Apr 27, 2008
Messages
1,228
Erm, I have not studied structs in such depths to say whether this way it is better or not.
But I can say that this create method is rather buggy.
You use event responses in there. I think they will not work there.
I usually create a method like Createall it uses a method I do not create - the .create()
This Createall takes as parameters everything it needs.
 
Status
Not open for further replies.
Top