Question about destroying structs

drol

New Member
Reaction score
7
Hey guys,

I'm currently wondering if I should destroy a local struct or not.
I'm not all that familiar yet with vJass, so I don't know what to do.

Here is what I have in my map:

Some library
JASS:
globals
    Tower array Towers
endglobals


My trigger:
JASS:
function Trig_Bounty_Actions takes nothing returns nothing

    // getTower grabs the struct matching the unit from the Towers array (see 
    // trigger 2)
    local Tower tower = getTower(GetKillingUnit())

    // editing some values of tower, like:
    set tower.experience = tower.experience + 1

    // to destroy tower or not to destroy tower?


Trigger 2: (not really that intresting)
JASS:
function getTower takes unit u returns Tower
    
        local integer i = 1
        
        loop
            exitwhen i > TowersCounter
            if ( Towers<i>.getUnit() == u ) then
                return Towers<i>
            endif
            set i = i + 1
        endloop
        
        return Tower.create(u,GetOwningPlayer(u),TowerType.create(0,0,0,0,false),0)
    
    endfunction</i></i>


So my question is, should I destroy tower in Trigger1 or not? Does it leak or? And where is the editing saved, is the local just a pointer?

(Btw, advise like; don't use the local just keep calling the getTower(unit) is a bit inefficient I think, since I use the local a lot in the trigger1 code.)

Thanks for helping! Will +rep
 

luorax

Invasion in Duskwood
Reaction score
67
Hopefully I've understood your question. So.
You don't have to destroy tower in Trigger 1. It won't leak, or anything like that. When you call "getTower(GetKillingUnit())", it gives you a pointer to the instance. So you don't have to destroy it, just work with it, and leave it as it is. It won't leak as it's just an integer.
 

drol

New Member
Reaction score
7
Hopefully I've understood your question. So.
You don't have to destroy tower in Trigger 1. It won't leak, or anything like that. When you call "getTower(GetKillingUnit())", it gives you a pointer to the instance. So you don't have to destroy it, just work with it, and leave it as it is. It won't leak as it's just an integer.

You understood me :)
Okay, all structs are integers?
Good to know it doesn't leak if I don't remove the local.

Thanks for your quick response :)
 

dudeim

New Member
Reaction score
22
Well they don't leak but they have an instance limit of around 8200 (8192 I thought) but if you have 1 instance for every tower that should be possible don't think you'll have 1000+ towers anyway:p
 

luorax

Invasion in Duskwood
Reaction score
67
I think his problem came from a real programming language. For example, in C++, when you return something it's copied, and the new copy will be returned. I think he thought that the same happends here, and thought he have to destroy the copy in the end of the function. But no, only the pointer is returned, so you don't have to worry.
 

tooltiperror

Super Moderator
Reaction score
231
Structs not being used should always be destroyed. It's just good practice, in case you suddenly have 10,000 towers being built per game, and only 5,000 at a time or something like that.
 

dudeim

New Member
Reaction score
22
Ofc they need to be destroyed but not at that point in the script it needs to be destroyed when the tower itself gets destroyed otherwise the values stored in the struct are lost (atleast I think they are:p)
 

tooltiperror

Super Moderator
Reaction score
231
No, the values are still there but it can be overwritten, so you are right to wait for the tower to die.
 

drol

New Member
Reaction score
7
Okay thanks a bunch guys, it's all clear for me now :)

I was indeed thinking how this plays out in a real programming language. This answers my questions :)
 
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