Retrieving a struct using PUI outside scope?

saw792

Is known to say things. That is all.
Reaction score
280
Since the name of both structs is Data that could be casuing issues with the keyword. Try changing one of them to data or something else. Make sure you fix the references through the code.
 

cleeezzz

The Undead Ranger.
Reaction score
268
question, when i do CAS_e.moved

what exactly does that return.? because structs can be multi instanceable but libraries cant, but if you call for a struct member, what exactly does that give you?

im still not getting how i can just put down CAS_e.moved in some trigger and make it refer to multiple instances of the struct Data.

like if you browse over the 2 codes in post 18, they have no correlation at all and its not taking a specific struct (like PUI) from what i know, PUI can store a specific instance of the struct. but CAS_e.moved is taken from no specific instance of the struct

now that i think about it, could i attach the struct to the unit, and then call it using set Data e = CAS_e
or is pui not even necessary to do this, if so, how? (i dont understand how the previous methods use mutli instanceable structs.
 

Builder Bob

Live free or don't
Reaction score
249
I haven't read the whole thing, so I'll just answer your last post.

because structs can be multi instanceable but libraries cant
I really don't like the word multi instanceable. It's just so confusing.


I just think of structs like a collection of arrays.

JASS:
local Data d = 1
set d.moved = 1.1

Translates to
JASS:
local integer d = 1
set somePrefixName_moved[d] = 1.1


-----------------------
Or in the case of globals

JASS:
set CAS_e = 5
set CAS_e.moved = 5.5

Translates to
JASS:
set CAS_e = 5
set somePrefixName_moved[CAS_e] = 5.5


Now CAS_e.moved will return 5.5.
If you set CAS_e to 1 however, CAS_e.moved will return 1.1 since it will look in place 1 in the array (I set somePrefixName_moved[1] = 1.1 in the first example)
 

cleeezzz

The Undead Ranger.
Reaction score
268
i kinda see what you mean, but how does that help me get e.moved from a specific struct instance? like, i can run CAS 5 times, and have 5 structs. how do i get e.moved from the one i want.
 

Builder Bob

Live free or don't
Reaction score
249
i kinda see what you mean, but how does that help me get e.moved from a specific struct instance? like, i can run CAS 5 times, and have 5 structs. how do i get e.moved from the one i want.

I don't know which instance you want, so that's kind of hard to answer.

JASS:
call SetUnitFacing(u, GetUnitFacing(u) - (CAS_e.moved/11))
It looks like you're tracking arrows or something, but what is this line supposed to do? What I'm trying to find out is what instance of CAS_Data are you trying to access here.
 

cleeezzz

The Undead Ranger.
Reaction score
268
ok so basically CAS creates an arrow that goes straight. GS, picks all arrows in range, and curves them (hence GS = Gravity Shifter/Field). since e.moved is the distance moved per interval (of the timer), i want to take that number and divide it by a constant 11. This way, if the arrow is slowed by some outside force, the arrow will change angle slower. (like a super slow arrow and go around and around in a circle. but a fast arrow changes angle about 90 degrees)

(and yes, im well aware that i can use the unit movespeed like i always have but being able to configure the codes to actually work off each other is a lot better than trying to base the code off movespeed.
 

Builder Bob

Live free or don't
Reaction score
249
Ok, that's doable.
GS, picks all arrows in range
When you pick units in range, your link must be the units themselves. Use PUI. You don't need the global variable CAS_e.
 

cleeezzz

The Undead Ranger.
Reaction score
268
when i try to call for the data with PUI, it tries to take the data from GS_Struct and says moved is not a member of GS_Struct, do i change the struct name so they dont conflict?
 

Builder Bob

Live free or don't
Reaction score
249
I'm going to make it simple and just say yes. After you've got it to work with different struct names you can choose whether you want to go back to both structs named public Data again if you prefer that.

Personally I always give structs that are going to be called outside of the library/scope unique names that describe the structs. To me that makes it easier to both write and read my code.
 

cleeezzz

The Undead Ranger.
Reaction score
268
JASS:
library CAS requires CSData,CSSafety,HandyFunctions

globals
    private constant real Start = 50.00   
    private constant real Scale = 1.00            
    private constant real Fly_height = 60.00   
    private constant attacktype A = ATTACK_TYPE_CHAOS
    private constant damagetype D = DAMAGE_TYPE_UNIVERSAL
    private unit Caster = null
endglobals

private function FilterIsEnemyAlive takes nothing returns boolean
    return IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(Caster)) and GetWidgetLife(GetFilterUnit())>0.405
endfunction  

               
//=======================================================================
private struct Arrow
    //! runtextmacro PUI()
    unit u
    unit d
    unit a
    real moved
    real cd
    real maxd
    real dmg
    real angle
    real r
    real plx
    real ply
    real bases
    group g = CreateGroup()
    timer t
    integer aid
    integer lvl
    real int
    real next
    boolean spell
    string sfx
    string as
    integer func
    integer slow
    real csd
       
    static method create takes integer ut, unit u, real cx, real cy, real tx, real ty, real period, real r, real moved, real maxd, real dmg, integer aid, string as, integer lvl, real int, string sfx, integer func returns Arrow
        local integer index
        local item    indexItem
        local integer i
        local Arrow d = Arrow<u>
        set d.t = NewTimer()
        set d.u = u
        set d.cd = Start
        set d.csd = 0
        set d.maxd = maxd
        set d.angle = AngleXY(cx,cy,tx,ty)
        set d.dmg = dmg
        set d.r = r
        set d.d = CreateUnit(GetOwningPlayer(u),ut, PolarProjectionX (cx, Start, d.angle), PolarProjectionY (cy, Start, d.angle) , Rad2Deg(d.angle))
        set d.plx = GetUnitX(d.d)
        set d.ply = GetUnitY(d.d)
        set d.slow = 0
        call GroupAddUnit(Arrows,d.d)
        call SetUnitMoveSpeed(d.d, moved)
        set d.bases = moved
        set index = 0
            loop
                exitwhen index &gt; 5
                set indexItem = UnitItemInSlot(u, index)
                    if indexItem != null then
                        if GetItemTypeId(indexItem) == &#039;I00G&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1, 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d,&#039;I00V&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I00J&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d,&#039;I00X&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I00F&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 17 then
                                    call UnitAddItemById(d.d, &#039;I00W&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I007&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 10 then
                                    call UnitAddItemById(d.d, &#039;I007&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I006&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d, &#039;I006&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I008&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 20 then
                                    call UnitAddItemById(d.d, &#039;I008&#039;)
                                endif
                        endif
                    endif
                set index = index + 1
            endloop
        set indexItem = null
        set d.moved = moved
        set Caster = u
            if aid != 0 and sfx != null then
                set d.spell = true
                    if aid != 0 then
                        set d.aid = aid
                        set d.as = as
                        set d.int = int
                        set d.next = int
                        set d.a = CreateUnit(GetOwningPlayer(u),&#039;h000&#039;,cx,cy,0)
                        call UnitAddAbility(d.a,aid)
                        call SetUnitAbilityLevel(d.a,aid,lvl)
                        call UnitApplyTimedLife(d.a,&#039;BTLF&#039;,60)
                    else
                        set d.aid = 0
                    endif
                    if sfx != null then
                        set d.sfx = sfx
                    else
                        set d.sfx = null
                    endif
            else
                set d.spell = false
            endif
                if func == 1 then
                    if GetUnitState( u, UNIT_STATE_MANA) &gt;= 1 then
                        call SetUnitState( u, UNIT_STATE_MANA, GetUnitState( u, UNIT_STATE_MANA) - 1)
                        call SetUnitMoveSpeed(d.d, moved - 7)
                        set d.bases = moved - 7
                        set d.func = func
                    else
                        set d.func = 0
                    endif
                else 
                    set d.func = 0
                endif
        call SetUnitPathing(d.d,false)
        call SetUnitScale(d.d,Scale,Scale,Scale)
        call SetUnitFlyHeight(d.d,Fly_height,0.00)
        
        call TimerStart(d.t,period,true,function Arrow.Execute)
        call SetCSData(d.t,d)
        
        return d
    endmethod
    
    static method Execute takes nothing returns nothing
        local real x
        local real y
        local real x2
        local real y2
        local real a
        local unit u
        local group g = CreateGroup()
        local real dist
        local real rdist
        local Arrow d = GetCSData(GetExpiredTimer())
        
        set x = GetUnitX(d.d)
        set y = GetUnitY(d.d)
        
        set rdist = DistanceXY( x, y, d.plx, d.ply)
        
            if d.slow == 0 then
                set d.moved = GetUnitMoveSpeed(d.d)
                    if GetUnitMoveSpeed(d.d) &lt; d.bases then
                        call SetUnitMoveSpeed(d.d, GetUnitMoveSpeed(d.d) + 0.3)
                    endif
            endif
        set d.cd = d.cd + rdist
        set d.plx = x
        set d.ply = y
        
            if d.cd &gt;= d.maxd or d.d == null then
                call d.destroy()
                set u = null
                call DestroyGroup(g)
                set g = null
                return
            endif

  
        
        set d.angle =  Deg2Rad(GetUnitFacing(d.d))
        set x = PolarProjectionX(x, d.moved, d.angle)
        set y = PolarProjectionY(y, d.moved, d.angle)
        
            if x &gt; GMaxX or x &lt; GMinX or y &gt; GMaxY or y &lt; GMinY then
                call RemoveUnit(d.d)
                set u = null
                call DestroyGroup(g)
                set g = null
                call d.destroy()
                return
            endif
            
        call SetUnitX(d.d, x)
        call SetUnitY(d.d, y)
        call GroupEnumUnitsInRange(g,x,y,d.r,Condition(function FilterIsEnemyAlive))
            
            loop
                set u = FirstOfGroup(g)
                exitwhen u == null
                    if not(IsUnitInGroup(u,d.g)) then
                        call GroupAddUnit(d.g,u)
                        call UnitDamageTarget(d.d,u,d.dmg,false,false,A,D,null)
                        
                    endif 
                call GroupRemoveUnit(g,u)                
            endloop
        call DestroyGroup(g)
        set g = null

            if GetUnitState(d.d, UNIT_STATE_LIFE) &gt; .405 then
                if d.spell == true then
                    set d.csd = d.csd + rdist
                        if d.csd &gt;= d.int then
                            set d.csd = 0
                                if d.aid != 0 then
                                    call IssuePointOrder(d.a, d.as, x, y)
                                endif
                                if d.sfx != null then
                                    call DestroyEffect(AddSpecialEffect( d.sfx, x, y ))
                                endif
                        endif
                endif
            endif
            if d.func == 1 then
                set g = CreateGroup()
                call GroupEnumUnitsInRange(g,x,y,600,Condition(function FilterIsEnemyAlive))
                call GroupAddGroupAdv(Arrows, g)
                loop
                    set u = FirstOfGroup(g)
                    if u == d.d then
                        call GroupRemoveUnit(g,u)
                        set u = FirstOfGroup(g)
                    endif
                    exitwhen u == null
                    set x2 = GetUnitX(u)
                    set y2 = GetUnitY(u)
                    set dist = DistanceXY(x,y,x2,y2)
                        if dist &lt;= 600 then
                            set a = AngleXY(x2,y2,x,y)
                                if 400/dist &lt;= dist then
                                    set x2 = SafeX(PolarProjectionX(x2,(400/dist),a))
                                    set y2 = SafeY(PolarProjectionY(y2,(400/dist),a))
                                endif
                            call SetUnitX(u,x2)
                            call SetUnitY(u,y2)
                        endif
                    call GroupRemoveUnit(g,u)
                endloop
                call DestroyGroup(g)
                set g = null
            endif                                   
    endmethod
    
    private method onDestroy takes nothing returns nothing
        call RemoveUnit(.d)
        call RemoveUnit(.a)
        set .u = null
        set .d = null
        set .a = null
        call GroupRemoveUnit(Arrows,.d)
        call GroupClear(.g)
        call DestroyGroup(.g)
        set .g = null
        call ReleaseTimer(.t)
        set .spell = false
        set .func = 0

    endmethod
endstruct

public function SA takes integer ut, unit u, real cx, real cy, real tx, real ty, real period, real r, real moved, real maxd, real dmg, integer aid, string as, integer lvl, real int, string sfx, integer func returns nothing    
    call Arrow.create(ut,u,cx,cy,tx,ty,period,r,moved,maxd,dmg,aid,as,lvl,int,sfx,func)
endfunction  

endlibrary
</u>


JASS:
scope GF initializer Init

private struct Data
    timer t
    unit d
    real x
    real y
    integer i
    group g = CreateGroup()
    
    private method onDestroy takes nothing returns nothing
        call ReleaseTimer(.t)
        call RemoveUnit(.d)
        call DestroyGroup(.g)
        set .g = null
        set .d = null
    endmethod
endstruct               

private function Shift takes nothing returns nothing
    local Data d = GetCSData(GetExpiredTimer())
    local unit u
    local real a
    local real dist
    local real x2
    local real y2
    local real f
    local real f2
    local group g = CreateGroup()
    local Arrow e //undefined variable &quot;arrow&quot;,  isn&#039;t this how you use PUI, but it cant seem to find the struct Arrow, i even made it public, it cant find it, do i have to make it global?
    set d.x = GetUnitX(d.d)
    set d.y = GetUnitY(d.d)
    call GroupAddGroupAdv(Arrows,g)
    
    if GetUnitState(d.d,UNIT_STATE_LIFE)&lt;.405 then
        call d.destroy()
        set u = null
        call DestroyGroup(g)
        set g = null
        return
    endif

    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        set e = Arrow<u>
        set x2 = GetUnitX(u)
        set y2 = GetUnitY(u)
        set f = GetUnitFacing(u)
        set f2 = f + 180
        set dist = DistanceXY(d.x,d.y,x2,y2)
            if dist &lt;= d.i then
                set a = Rad2Deg(AngleXY(x2,y2,d.x,d.y))
                    if a &lt; 0 then
                        set a = a + 360
                    endif
                    if f &gt; 180 then
                        if a &lt;= f and a &gt;= f2 - 360 then
                            call SetUnitFacing(u, GetUnitFacing(u) - 3)
                        else
                            call SetUnitFacing(u, GetUnitFacing(u) + 3)
                        endif 
                    else
                        if a &gt;= f and a &lt;= f2 then
                            call SetUnitFacing(u, GetUnitFacing(u) + 3)
                        else
                            call SetUnitFacing(u, GetUnitFacing(u) - 3)
                        endif 
                    endif
            endif
        call GroupRemoveUnit(g,u)
    endloop
    call DestroyGroup(g)
    set g = null
    set u = null   
endfunction
    
//=======================================================================
private function Actions takes nothing returns nothing
    local Data d = Data.create()
    local unit u = GetTriggerUnit()
    local location l = GetSpellTargetLoc()
    call RemoveUnit(GF[GetPlayerId(GetOwningPlayer(u))])
    set GF[GetPlayerId(GetOwningPlayer(u))] = null
    set d.t = NewTimer()
    set d.x = GetLocationX(l)
    set d.y = GetLocationY(l)
    set d.d = CreateUnit(GetOwningPlayer(u), &#039;h00L&#039;, d.x, d.y, 0)
    set GF[GetPlayerId(GetOwningPlayer(u))] = d.d
    set d.i = (200 + (100 * GetUnitAbilityLevel( u, &#039;A01W&#039;)))
    
    call TimerStart(d.t, 0.03, true, function Shift)
    call SetCSData(d.t, d)
    call RemoveLocation(l)
    set u = null
    set l = null
endfunction

//=======================================================================
private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == &#039;A01W&#039;
endfunction


//=======================================================================
private function Init takes nothing returns nothing
    local trigger trig = CreateTrigger()   
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(trig,Condition(function Conditions))
    call TriggerAddAction(trig,function Actions)
endfunction

endscope

</u>


read comment in 2nd code
 

Builder Bob

Live free or don't
Reaction score
249
change
JASS:
private struct Arrow

to
JASS:
struct Arrow



Edit:
library/scope private/public are only used to tell jasshelper to rename and rearrange code for you.

Code within libraries are written before code within scopes. This is to make sure functions within libraries can be called from all scopes.

public renames a function/struct/variable in such a way that you can still call it by using the library/scope prefix in front of the function/struct/variable name.
private renames the functions/struct/variables in such a way that you are unable to call the function/struct/variable from anywhere outside of the library/scope that you declared the function/struct/variable

When using libraries/scopes, you do not have to use public/private prefixes. They are just options to make certain functions/structs/variables renamed so they become uncallable from outside of the library/scope. By doing so, you can reuse the names in other libraries/scopes.
 

cleeezzz

The Undead Ranger.
Reaction score
268
ok now that i changed it to struct Arrow, it has a problem destroying it..(but its the right step in the right direction because PUI is working)


(oh and everytime i shoot an arrow, the speed increasingly gets faster. even though a debug telling me d.moved is still 33. is it cuz the instances are overlapping, and 2 instances are moving the same arrow?
JASS:
library CAS requires CSData,CSSafety,HandyFunctions

globals
    private constant real Start = 50.00   
    private constant real Scale = 1.00            
    private constant real Fly_height = 60.00   
    private constant attacktype A = ATTACK_TYPE_CHAOS
    private constant damagetype D = DAMAGE_TYPE_UNIVERSAL
    private unit Caster = null
endglobals

private function FilterIsEnemyAlive takes nothing returns boolean
    return IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(Caster)) and GetWidgetLife(GetFilterUnit())&gt;0.405
endfunction  

               
//=======================================================================
struct Arrow
    //! runtextmacro PUI()
    unit u
    unit d
    unit a
    real moved
    real cd
    real maxd
    real dmg
    real angle
    real r
    real plx
    real ply
    real bases
    group g = CreateGroup()
    timer t
    integer aid
    integer lvl
    real int
    real next
    boolean spell
    string sfx
    string as
    integer func
    real csd
       
    static method create takes integer ut, unit u, real cx, real cy, real tx, real ty, real period, real r, real moved, real maxd, real dmg, integer aid, string as, integer lvl, real int, string sfx, integer func returns Arrow
        local integer index
        local item    indexItem
        local integer i
        local Arrow d = Arrow<u>
        set d.t = NewTimer()
        set d.u = u
        set d.cd = Start
        set d.csd = 0
        set d.maxd = maxd
        set d.angle = AngleXY(cx,cy,tx,ty)
        set d.dmg = dmg
        set d.r = r
        set d.d = CreateUnit(GetOwningPlayer(u),ut, PolarProjectionX (cx, Start, d.angle), PolarProjectionY (cy, Start, d.angle) , Rad2Deg(d.angle))
        set d.plx = GetUnitX(d.d)
        set d.ply = GetUnitY(d.d)
        call GroupAddUnit(Arrows,d.d)
        call SetUnitMoveSpeed(d.d, moved)
        set d.bases = moved
        set index = 0
            loop
                exitwhen index &gt; 5
                set indexItem = UnitItemInSlot(u, index)
                    if indexItem != null then
                        if GetItemTypeId(indexItem) == &#039;I00G&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1, 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d,&#039;I00V&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I00J&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d,&#039;I00X&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I00F&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 17 then
                                    call UnitAddItemById(d.d, &#039;I00W&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I007&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 10 then
                                    call UnitAddItemById(d.d, &#039;I007&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I006&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d, &#039;I006&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I008&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 20 then
                                    call UnitAddItemById(d.d, &#039;I008&#039;)
                                endif
                        endif
                    endif
                set index = index + 1
            endloop
        set indexItem = null
        set d.moved = moved
        set Caster = u
            if aid != 0 and sfx != null then
                set d.spell = true
                    if aid != 0 then
                        set d.aid = aid
                        set d.as = as
                        set d.int = int
                        set d.next = int
                        set d.a = CreateUnit(GetOwningPlayer(u),&#039;h000&#039;,cx,cy,0)
                        call UnitAddAbility(d.a,aid)
                        call SetUnitAbilityLevel(d.a,aid,lvl)
                        call UnitApplyTimedLife(d.a,&#039;BTLF&#039;,60)
                    else
                        set d.aid = 0
                    endif
                    if sfx != null then
                        set d.sfx = sfx
                    else
                        set d.sfx = null
                    endif
            else
                set d.spell = false
            endif
                if func == 1 then
                    if GetUnitState( u, UNIT_STATE_MANA) &gt;= 1 then
                        call SetUnitState( u, UNIT_STATE_MANA, GetUnitState( u, UNIT_STATE_MANA) - 1)
                        call SetUnitMoveSpeed(d.d, moved - 7)
                        set d.bases = moved - 7
                        set d.func = func
                    else
                        set d.func = 0
                    endif
                else 
                    set d.func = 0
                endif
        call SetUnitPathing(d.d,false)
        call SetUnitScale(d.d,Scale,Scale,Scale)
        call SetUnitFlyHeight(d.d,Fly_height,0.00)
        
        call TimerStart(d.t,period,true,function Arrow.Execute)
        call SetCSData(d.t,d)
        
        return d
    endmethod
    
    static method Execute takes nothing returns nothing
        local real x
        local real y
        local real x2
        local real y2
        local real a
        local unit u
        local group g = CreateGroup()
        local real dist
        local real rdist
        local Arrow d = GetCSData(GetExpiredTimer())
        
        set x = GetUnitX(d.d)
        set y = GetUnitY(d.d)
        
        set rdist = DistanceXY( x, y, d.plx, d.ply)
            if d.moved &lt; d.bases then
                call SetUnitMoveSpeed(d.d, GetUnitMoveSpeed(d.d) + 0.3)
            endif
        call BJDebugMsg(R2S(d.moved))
        set d.cd = d.cd + rdist
        set d.plx = x
        set d.ply = y
        
            if d.cd &gt;= d.maxd or d.d == null then
                call d.destroy()
                set u = null
                call DestroyGroup(g)
                set g = null
                return
            endif

  
        
        set d.angle =  Deg2Rad(GetUnitFacing(d.d))
        set x = PolarProjectionX(x, d.moved, d.angle)
        set y = PolarProjectionY(y, d.moved, d.angle)
        
            if x &gt; GMaxX or x &lt; GMinX or y &gt; GMaxY or y &lt; GMinY then
                call RemoveUnit(d.d)
                set u = null
                call DestroyGroup(g)
                set g = null
                call d.destroy()
                return
            endif
            
        call SetUnitX(d.d, x)
        call SetUnitY(d.d, y)
        call GroupEnumUnitsInRange(g,x,y,d.r,Condition(function FilterIsEnemyAlive))
            
            loop
                set u = FirstOfGroup(g)
                exitwhen u == null
                    if not(IsUnitInGroup(u,d.g)) then
                        call GroupAddUnit(d.g,u)
                        call UnitDamageTarget(d.d,u,d.dmg,false,false,A,D,null)
                        
                    endif 
                call GroupRemoveUnit(g,u)                
            endloop
        call DestroyGroup(g)
        set g = null

            if GetUnitState(d.d, UNIT_STATE_LIFE) &gt; .405 then
                if d.spell == true then
                    set d.csd = d.csd + rdist
                        if d.csd &gt;= d.int then
                            set d.csd = 0
                                if d.aid != 0 then
                                    call IssuePointOrder(d.a, d.as, x, y)
                                endif
                                if d.sfx != null then
                                    call DestroyEffect(AddSpecialEffect( d.sfx, x, y ))
                                endif
                        endif
                endif
            endif
            if d.func == 1 then
                set g = CreateGroup()
                call GroupEnumUnitsInRange(g,x,y,600,Condition(function FilterIsEnemyAlive))
                call GroupAddGroupAdv(Arrows, g)
                loop
                    set u = FirstOfGroup(g)
                    if u == d.d then
                        call GroupRemoveUnit(g,u)
                        set u = FirstOfGroup(g)
                    endif
                    exitwhen u == null
                    set x2 = GetUnitX(u)
                    set y2 = GetUnitY(u)
                    set dist = DistanceXY(x,y,x2,y2)
                        if dist &lt;= 600 then
                            set a = AngleXY(x2,y2,x,y)
                                if 400/dist &lt;= dist then
                                    set x2 = SafeX(PolarProjectionX(x2,(13200/dist*d.moved),a))
                                    set y2 = SafeY(PolarProjectionY(y2,(13200/dist*d.moved),a))
                                endif
                            call SetUnitX(u,x2)
                            call SetUnitY(u,y2)
                        endif
                    call GroupRemoveUnit(g,u)
                endloop
                call DestroyGroup(g)
                set g = null
            endif                                   
    endmethod
    
    private method onDestroy takes nothing returns nothing
        call RemoveUnit(.d)
        call RemoveUnit(.a)
        set .u = null
        set .d = null
        set .a = null
        call GroupRemoveUnit(Arrows,.d)
        call GroupClear(.g)
        call DestroyGroup(.g)
        set .g = null
        call ReleaseTimer(.t)
        set .spell = false
        set .func = 0

    endmethod
endstruct

public function SA takes integer ut, unit u, real cx, real cy, real tx, real ty, real period, real r, real moved, real maxd, real dmg, integer aid, string as, integer lvl, real int, string sfx, integer func returns nothing    
    call Arrow.create(ut,u,cx,cy,tx,ty,period,r,moved,maxd,dmg,aid,as,lvl,int,sfx,func)
endfunction  

endlibrary
</u>


it has to do with public/private/static methods doesn't it.
 

Builder Bob

Live free or don't
Reaction score
249
I don't have much time to look at your code right now, but I'll do it a bit later.

What I can say for now is that it seems that you are using the unit shooting the arrow as an identifier for PUI. When you do that, the same instance will be used for all arrows being shot by that unit. If you instead use the arrow dummy unit as the identifier you should get a fresh instance for each arrow being shot.
 

saw792

Is known to say things. That is all.
Reaction score
280
Remove the static keyword from the execute method and try again.
 

cleeezzz

The Undead Ranger.
Reaction score
268
oh your right, why did i set the struct on the triggering unit, it should be on the arrow. hmm.. now its saying double free of type Arrow.

JASS:
library CAS requires CSData,CSSafety,HandyFunctions

globals
    private constant real Start = 50.00   
    private constant real Scale = 1.00            
    private constant real Fly_height = 60.00   
    private constant attacktype A = ATTACK_TYPE_CHAOS
    private constant damagetype D = DAMAGE_TYPE_UNIVERSAL
    private unit Caster = null
endglobals

private function FilterIsEnemyAlive takes nothing returns boolean
    return IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(Caster)) and GetWidgetLife(GetFilterUnit())&gt;0.405
endfunction  

               
//=======================================================================
struct Arrow
    //! runtextmacro PUI()
    unit u
    unit d
    unit a
    real moved
    real cd
    real maxd
    real dmg
    real angle
    real r
    real plx
    real ply
    real bases
    group g = CreateGroup()
    timer t
    integer aid
    integer lvl
    real int
    real next
    boolean spell
    string sfx
    string as
    integer func
    real csd
       
    static method create takes integer ut, unit u, real cx, real cy, real tx, real ty, real period, real r, real moved, real maxd, real dmg, integer aid, string as, integer lvl, real int, string sfx, integer func returns Arrow
        local integer index
        local item    indexItem
        local integer i
        local Arrow d = Arrow.allocate()
        set d.t = NewTimer()
        set d.u = u
        set d.cd = Start
        set d.csd = 0
        set d.maxd = maxd
        set d.angle = AngleXY(cx,cy,tx,ty)
        set d.dmg = dmg
        set d.r = r
        set d.d = CreateUnit(GetOwningPlayer(u),ut, PolarProjectionX (cx, Start, d.angle), PolarProjectionY (cy, Start, d.angle) , Rad2Deg(d.angle))
        set d.plx = GetUnitX(d.d)
        set d.ply = GetUnitY(d.d)
        call GroupAddUnit(Arrows,d.d)
        call SetUnitMoveSpeed(d.d, moved)
        set d.bases = moved
        set index = 0
            loop
                exitwhen index &gt; 5
                set indexItem = UnitItemInSlot(u, index)
                    if indexItem != null then
                        if GetItemTypeId(indexItem) == &#039;I00G&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1, 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d,&#039;I00V&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I00J&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d,&#039;I00X&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I00F&#039; or GetItemTypeId(indexItem) == &#039;I00M&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 17 then
                                    call UnitAddItemById(d.d, &#039;I00W&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I007&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 10 then
                                    call UnitAddItemById(d.d, &#039;I007&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I006&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 15 then
                                    call UnitAddItemById(d.d, &#039;I006&#039;)
                                endif
                        endif
                        if GetItemTypeId(indexItem) == &#039;I008&#039; then
                            set i = GetRandomInt(1 , 100)
                                if i &lt;= 20 then
                                    call UnitAddItemById(d.d, &#039;I008&#039;)
                                endif
                        endif
                    endif
                set index = index + 1
            endloop
        set indexItem = null
        set d.moved = moved
        set Caster = u
            if aid != 0 and sfx != null then
                set d.spell = true
                    if aid != 0 then
                        set d.aid = aid
                        set d.as = as
                        set d.int = int
                        set d.next = int
                        set d.a = CreateUnit(GetOwningPlayer(u),&#039;h000&#039;,cx,cy,0)
                        call UnitAddAbility(d.a,aid)
                        call SetUnitAbilityLevel(d.a,aid,lvl)
                        call UnitApplyTimedLife(d.a,&#039;BTLF&#039;,60)
                    else
                        set d.aid = 0
                    endif
                    if sfx != null then
                        set d.sfx = sfx
                    else
                        set d.sfx = null
                    endif
            else
                set d.spell = false
            endif
                if func == 1 then
                    if GetUnitState( u, UNIT_STATE_MANA) &gt;= 1 then
                        call SetUnitState( u, UNIT_STATE_MANA, GetUnitState( u, UNIT_STATE_MANA) - 1)
                        call SetUnitMoveSpeed(d.d, moved - 7)
                        set d.bases = moved - 7
                        set d.func = func
                    else
                        set d.func = 0
                    endif
                else 
                    set d.func = 0
                endif
        call SetUnitPathing(d.d,false)
        call SetUnitScale(d.d,Scale,Scale,Scale)
        call SetUnitFlyHeight(d.d,Fly_height,0.00)
        set Arrow[d.d] = d        
        call TimerStart(d.t,period,true,function Arrow.Execute)
        call SetCSData(d.t,d)
        
        return d
    endmethod
    
    static method Execute takes nothing returns nothing
        local real x
        local real y
        local real x2
        local real y2
        local real a
        local unit u
        local group g = CreateGroup()
        local real dist
        local real rdist
        local Arrow d = GetCSData(GetExpiredTimer())
        
        set x = GetUnitX(d.d)
        set y = GetUnitY(d.d)
        
        set rdist = DistanceXY( x, y, d.plx, d.ply)
            if d.moved &lt; d.bases then
                call SetUnitMoveSpeed(d.d, GetUnitMoveSpeed(d.d) + 0.3)
            endif
        set d.cd = d.cd + rdist
        set d.plx = x
        set d.ply = y
        
            if d.cd &gt;= d.maxd or d.d == null then
                call d.destroy()
                set u = null
                call DestroyGroup(g)
                set g = null
                return
            endif

  
        
        set d.angle =  Deg2Rad(GetUnitFacing(d.d))
        set x = PolarProjectionX(x, d.moved, d.angle)
        set y = PolarProjectionY(y, d.moved, d.angle)
        
            if x &gt; GMaxX or x &lt; GMinX or y &gt; GMaxY or y &lt; GMinY then
                call d.destroy()
                call RemoveUnit(d.d)
                set d.d = null
                set u = null
                call DestroyGroup(g)
                set g = null
                
                return
            endif
            
        call SetUnitX(d.d, x)
        call SetUnitY(d.d, y)
        call GroupEnumUnitsInRange(g,x,y,d.r,Condition(function FilterIsEnemyAlive))
            
            loop
                set u = FirstOfGroup(g)
                exitwhen u == null
                    if not(IsUnitInGroup(u,d.g)) then
                        call GroupAddUnit(d.g,u)
                        call UnitDamageTarget(d.d,u,d.dmg,false,false,A,D,null)
                        
                    endif 
                call GroupRemoveUnit(g,u)                
            endloop
        call DestroyGroup(g)
        set g = null

            if GetUnitState(d.d, UNIT_STATE_LIFE) &gt; .405 then
                if d.spell == true then
                    set d.csd = d.csd + rdist
                        if d.csd &gt;= d.int then
                            set d.csd = 0
                                if d.aid != 0 then
                                    call IssuePointOrder(d.a, d.as, x, y)
                                endif
                                if d.sfx != null then
                                    call DestroyEffect(AddSpecialEffect( d.sfx, x, y ))
                                endif
                        endif
                endif
            endif
            if d.func == 1 then
                set g = CreateGroup()
                call GroupEnumUnitsInRange(g,x,y,600,Condition(function FilterIsEnemyAlive))
                call GroupAddGroupAdv(Arrows, g)
                loop
                    set u = FirstOfGroup(g)
                    if u == d.d then
                        call GroupRemoveUnit(g,u)
                        set u = FirstOfGroup(g)
                    endif
                    exitwhen u == null
                    set x2 = GetUnitX(u)
                    set y2 = GetUnitY(u)
                    set dist = DistanceXY(x,y,x2,y2)
                        if dist &lt;= 600 then
                            set a = AngleXY(x2,y2,x,y)
                                if 400/dist &lt;= dist then
                                    set x2 = SafeX(PolarProjectionX(x2,(13200/dist*d.moved),a))
                                    set y2 = SafeY(PolarProjectionY(y2,(13200/dist*d.moved),a))
                                endif
                            call SetUnitX(u,x2)
                            call SetUnitY(u,y2)
                        endif
                    call GroupRemoveUnit(g,u)
                endloop
                call DestroyGroup(g)
                set g = null
            endif                                   
    endmethod
    
    method onDestroy takes nothing returns nothing
        call RemoveUnit(.d)
        call RemoveUnit(.a)
        set .u = null
        set .d = null
        set .a = null
        call GroupRemoveUnit(Arrows,.d)
        call GroupClear(.g)
        call DestroyGroup(.g)
        set .g = null
        call ReleaseTimer(.t)
        set .spell = false
        set .func = 0

    endmethod
endstruct

public function SA takes integer ut, unit u, real cx, real cy, real tx, real ty, real period, real r, real moved, real maxd, real dmg, integer aid, string as, integer lvl, real int, string sfx, integer func returns nothing    
    call Arrow.create(ut,u,cx,cy,tx,ty,period,r,moved,maxd,dmg,aid,as,lvl,int,sfx,func)
endfunction  

endlibrary


this no longer has anything to do with the GS scope, this is CAS working by itself and its bugging, after 2 perfect uses, it bugs and keeps double freeing type Arrow. when i try using the library, the arrow is instantly removed.
 

Builder Bob

Live free or don't
Reaction score
249
double free means you're trying to destroy a struct instance that has already been destroyed, or has not yet been allocated.

This means that the problem must be in one of your places where you destroy your struct.

JASS:
(...)
            if d.cd &gt;= d.maxd or d.d == null then
                call d.destroy()
                set u = null
                call DestroyGroup(g)
                set g = null
                return
            endif


JASS:
(...)
            if x &gt; GMaxX or x &lt; GMinX or y &gt; GMaxY or y &lt; GMinY then
                call d.destroy()
                call RemoveUnit(d.d)
                set d.d = null
                set u = null
                call DestroyGroup(g)
                set g = null
                
                return
            endif


Check if all the variables related to the destruction are what you expect them to be. I couldn't find anything by just looking at the code, so I hope you'll get some irregularities from your debugging messages.

Edit:After destroying the struct d you should not use d anymore. Put these lines before the destruction if you really want to keep them there. (it is done in the onDestroy method as well.)
JASS:
call RemoveUnit(d.d)
set d.d = null
 

cleeezzz

The Undead Ranger.
Reaction score
268
is it possible its trying to destroy the wrong instance of the struct? this wasn't a problem before adding pui
 
General chit-chat
Help Users
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      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