Notifications
Clear all

[Closed] Passing initialization value to nested struct as a reference?

Im having some trouble using nested structs because I cant access values from the parent struct, and because the values change at runtime I cant just pass the values to the struct on create, because they would become out of date later.


global Temp
(
   struct MainStruct
   (
      MainStructValue = 1,
      NestedStruct =
      (
         struct NestedStruct (
            referenceValue,
            fn TestValue = (print referenceValue)
         )
      ) referenceValue:&MainStructValue,
      
      fn ModifyValue = ( MainStructValue += 1),
      on create do ()
   )
   Temp = MainStruct()
   Temp.ModifyValue()
   Temp.NestedStruct.TestValue()
)

So i had this idea to pass the values to the nested struct by reference. To my surprise it actually compiles, but the problem is its not really working as i would want it

print referenceValue

returns a “Struct member:MainStructValue” which is of type ValueRef. the documentation for ValueRef is pretty much nothing, so i was wondering if anyone has any ideas how I could actually cast this to a usable type? Ive also looked in to the dereferencing documentation with * but i cant seem to get it to work… it just keeps returning “Struct member:MainStructValue”

9 Replies

Works with array. Not sure if it is possible for other value types


global Temp
(
    local xyz
   struct MainStruct
   (
      MainStructValue = #(),
      NestedStruct =
      (
         struct NestedStruct (
            referenceValue,
            fn TestValue = (format "%
" (*referenceValue))
         )
      ) referenceValue:&xyz,

      fn ModifyValue = ( append MainStructValue 1),
      on create do xyz = MainStructValue
   )
   
   Temp = MainStruct()
   Temp.ModifyValue()   
   Temp.NestedStruct.TestValue()
   Temp.ModifyValue()   
   Temp.NestedStruct.TestValue()
   Temp.ModifyValue()   
   Temp.NestedStruct.TestValue()

)

[quote=]#(1)
#(1, 1)
#(1, 1, 1)

Indeed, using an array as a wrapper for the actual values seems to work. Makes the implementation a bit more verbose than I would like but i guess its better than nothing

Any reason not to do it this way?

global Temp
(
    struct NestedStructDef
    (
        owner,
        fn TestValue = print owner.MainStructValue
    )
    struct MainStruct
    (
        MainStructValue = 1,
        NestedStruct = NestedStructDef owner:this,

        fn ModifyValue = MainStructValue += 1,
        on create do ()
    )

    Temp = MainStruct()
    Temp.ModifyValue()
    Temp.NestedStruct.TestValue()
)

If you want to keep your ‘nested struct’ structure, you can use:

Awesome, this is exactly what I was looking for! Im assuming it works for private members too?

1 Reply
(@aaandres)
Joined: 11 months ago

Posts: 0

Nope. It’s still considered as external accessing to private values.