Search Unity

Physics2D.LinecastNonAlloc vS Physics2D.Linecast

Discussion in '2D' started by popeye1, Jan 19, 2015.

  1. popeye1

    popeye1

    Joined:
    Dec 19, 2014
    Posts:
    28
    According to the API LinecastNonAlloc can avoid overhead cost of allocating memory for the
    RaycastHit2D array. But I still have to declare and allocate my own RaycastHit2D (used as argument Physics2D.LinecastNonAlloc ) to array when I use this function so I don't see / understand the benefit ?
     
  2. Deleted User

    Deleted User

    Guest

    I think the benefit is for when you already know your RaycastHit2D, so in that case instead of reallocating it again you just use the non allocating function, I don't think it has any other use.
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The benefit is that it is not allocating data which is feeding the garbage collector. Hint: it's always better.
     
  4. SteamPunkPro2

    SteamPunkPro2

    Joined:
    Jun 8, 2013
    Posts:
    29
    I think what you are misisng is, the array should be allocated outside of the function call, and kept around for the life of the object that is doing the searching, so it can 'recycle' the array :)
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Well it just populates it up until array length, instead of doing allocations with every call. I wish all physics tests including sweeps had this option since it's not fun to see your title slow down due to core loop allocations.
     
  6. SteamPunkPro2

    SteamPunkPro2

    Joined:
    Jun 8, 2013
    Posts:
    29

    very much agreed, honestly I was shocked when I first started using unity that they didnt do this, and very happy when I saw the new 2D calls have it as an option!
     
    hippocoder likes this.
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    And @Unity ... how about all the other calls, guys? don't stop now.
     
  8. SteamPunkPro2

    SteamPunkPro2

    Joined:
    Jun 8, 2013
    Posts:
    29
    Something else I'd like to see, that is included in almost every other game engine is an ignore parameter for these functions as well, so you can ignore one specific collider, without having to have the whole list marshaled over to c# and checked.
     
  9. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Are any of the above two requests listed in the wish list thingie? If so what are they called?
    I'll add my vote if they are since it sounds like those are two very worthy optimizers.
     
  10. popeye1

    popeye1

    Joined:
    Dec 19, 2014
    Posts:
    28
    OK great.

    I have one class that are handling grounding. Can I have raycasthits2d[] as public / private variable in this class and initiate the size in the awake() function? (tried it but shows null when I use it in other class methods in this class later on)