5E Character Create Playlist
  1. #1
    Minty23185Fresh's Avatar
    Join Date
    Dec 2015
    Location
    Goldstone, CA, USA
    Posts
    1,211
    Blog Entries
    29

    Are variables passed by reference? by value? or it depends?

    I am modifying the 5E ActionManager2 functions encodeAdvantage(rRoll, bADV, bDIS) and decodeAdvantage(rRoll) for my extension.

    I have noticed that for changes made to the values of the arguments within the functions, sometimes the new values are reflected back in the calling function (as if by reference) and sometimes not (as if by value)! What the heck?

    Is seems as thought the simple boolean variables bADV and bDIS are passed by value, since changes are not reflected back to the caller.

    Whereas any changes made to the values in the table variable rRoll, do get reflected back to the caller, indicating passed by reference.

    Do I have this about right? Is there more to this?

    (I always search the forums for this stuff before posting, and never seem to find the answers, I hope this hasn't been answered already. Thanks in advance for your help.)
    Current Projects:
    Always...
    Community Contributions:
    Extensions: Bardic Inspiration, Druid Wild Shapes, Local Dice Tower, Library Field Filters
    Tutorial Blog Series: "A Neophyte Tackles (coding) the FG Extension".

  2. #2
    All variables are technically passed by value, but anything other than a basic type is really passing a Lua table value pointer.

    So, when you edit a complex variable, you end up editing the actual members, because the table variable points to a table object in memory.

    If you need a deep copy (as opposed to the standard shallow copy), I wrote a function called UtilityManager.copyDeep() to do that.

    Cheers,
    JPG

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
STAR TREK 2d20

Log in

Log in