Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    ref does nothing when feeding array elements to a function

    Bug Reporting (FUZE 4 Nintendo Switch)
    ref
    7
    19
    850
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Willpowered
      Willpowered Fuze Team last edited by

      Yup, that's what I'm working on! I've got ref working on my end right now for everything except strings. I still have to test the OP's code with my adjustments, they're doing something I wouldn't have expected.

      1 Reply Last reply Reply Quote 3
      • vinicity
        vinicity F last edited by

        Great! Will it work even if you change the type of the referenced variable inside the called function?

        1 Reply Last reply Reply Quote 0
        • Willpowered
          Willpowered Fuze Team last edited by

          Good question. I'll put it on my list of things to test!

          1 Reply Last reply Reply Quote 1
          • Willpowered
            Willpowered Fuze Team last edited by Willpowered

            Following up on this:

            The issue is related to the core of how ref works internally, and while ref is currently being worked on, we've determined that this is unfortunately not something we can easily change for the next patch. The limitation in this case is that the ref keyword must be used with a defined non-member, non-element variable. A defined variable is a variable that you've declared and assigned to, which isn't a member or element of an array, structure, sprite, etc.

            In the example in the top post, incByRef(a) works because it's passing a, which matches our definition of a defined non-member/non-element variable. incByRef(b[0]) is not currently valid because while b is a defined variable, b[0] doesn't match the criteria (it's an element of b). The workaround currently is to write an incArrayElementsByRef(ref my_array) function that accepts the array b and operates on its elements.

            We will, however, look at expanding the ref keyword's capabilities in the future. For the next patch we'll clarify this limitation of the ref keyword in the documentation.

            1 Reply Last reply Reply Quote 2
            • R
              romain337 last edited by romain337

              Hi!
              in setTimer

              function increment(ref x)
                x += 1
              return void
              
              function doSomething()
                var test = 0
                setTimer(1, 5, increment(test))
                loop
                  clear()
                  printAt(0,0, test)
                  update()
                repeat
              return void
              
              doSomething()
              

              The function is called but test is not "ref" incremented. I noticed this behavior when experimentating with timer. It could be a bug ?
              EDIT* it seem that this will be fixed in the next release as raw type don't work when used with ref keyword if I understand.

              1 Reply Last reply Reply Quote 0
              • Willpowered
                Willpowered Fuze Team last edited by

                That's correct- in version 2.15.0 ref only works with arrays and structures. We're working on expanding it to all variable types for the next patch!

                Z-Mann 1 Reply Last reply Reply Quote 1
                • Z-Mann
                  Z-Mann @Willpowered last edited by

                  @Willpowered Thanks for the explanation! If that is now how the feature is specified, I at least know what to expect. That also explains why 3.0.0 broke this code:

                  function incByRef(v)
                      v.x = v.x + 1
                  return
                  
                  a = [.x = 1]
                  b = [.a = a]
                  
                  print("b.a.x=", b.a.x, "\n")
                  incByRef(b.a) // not a defined variable! won't do a thing.
                  print("b.a.x=", b.a.x, "\n")
                  

                  In 2.1.5, it used to increment b.a.x. Oh well, specs are specs, and at least now things are consistent. I'll be ditching complicated nested data structures I'm used to from other languages and use arrays of primitives and simple structs from now on, that's what the Cool Kids call Data Oriented Design. Many things are even easier to express that way.

                  1 Reply Last reply Reply Quote 2
                  • Willpowered
                    Willpowered Fuze Team last edited by

                    We're looking at allowing ref to work on structure members and array elements. I'll keep you posted!

                    1 Reply Last reply Reply Quote 3
                    • PickleCatStars
                      PickleCatStars F last edited by

                      Will that let us do ‘struct.thing = ref otherstruct.otherthing’?

                      1 Reply Last reply Reply Quote 0
                      • PB____
                        PB____ last edited by PB____

                        I think it means that you would be able to do something like this:

                        function increase(ref data)
                            data.value += 1
                        return void
                        
                        example = [
                            .member = [
                                .value = 1
                            ]
                        ]
                        print(example.member.value, "\n")
                        increase(example.member)
                        print(example.member.value) // currently this value is not updated
                        update()
                        sleep(10)
                        
                        1 Reply Last reply Reply Quote 1
                        • First post
                          Last post