struct properties are disposed while they still have a reference
-
During my second ever stream on Twitch I tried creating a linked list using the ref keyword, and I discovered a new bug in FUZE 2.15.0:
I created the elements in my linked list with this function:
function createNode() return [ .value = [], .hasValue = false, .next = [], .hasNext = false ]
and also tried this variant:
function createNode() return ([ .value = [], .hasValue = false, .next = [], .hasNext = false ])
Both would return the struct, but all the values would be undefined. So doing the following would throw an error:
var linkedList = createNode() var ok = linkedList.hasValue var notOk = !linkedList.hasValue //throws error, because you cannot use ! operator on undefined value.
I fixed this by refactoring the createNode value to the following implementation:
function createNode() var result = [ .value = [], .hasValue = false, .next = [], .hasNext = false ] return result
by doing this, I could do
!linkedList.hasValue
as described in the earlier code.Unfortunately I suspect that this bug has been introduced in FUZE 2.15.0.
-
Thanks @PB____ I will investigate tomorrow
-
@PB____ I have tried this in 2.14 and I get the same effect so maybe this has always been a problem?
-
I literally don’t understand what’s going on in this thread. Is there something wrong with structs? Should I not use them?
-
@toxibunny No this is a specific problem with initializing a struct using a function. If you do it anonymously it doesn't seem to persist the values. As long as you assign it to a variable before returning it it works fine
-
Thank you @pianofire for testing this on 2.14, obviously I'm not able to do that myself. It's a relieve to know that this bug isn't new with the release!
I've removed the 2.15 tag from the post :) -
This is an interesting way of making initializations more compact. Is this memory-save? I feel like it creates an integer and assignes a struct to it that takes more memory than an integer and has been defined in another scope.
-
@spikey I'm not sure what you mean by your question.
However in general in FUZE, I should probably have done something like this:
struct list_linkedNode var value int hasValue = false list_linkedNode next = [] // to prevent infinite node creation you need to either assign a value here, or use the var type in stead int hasNext = false endstruct function list_createNode() list_linkedNode result return result
And then the function 'list_createNode' wouldn't even be necessary, unless the values for result are not always the same and not stored in a global variable.
-
@PB____ but does this work? I struggled with using the struct command, because I always ended up with 2 steps: defining the struct type and filling in values instead of just filling in the values, what automatically defined a struct.
To my question: do you use this already in projects? I think of doing it too. But I am cautious with changing my struct handling ;) -
@spikey I've checked to be sure, but yes, I'm using this type of constructor pattern to create new structures based on values provided to the function.
For example in my template code I have a function called
command_input
to create a struct that keeps track of the buttons you've pressed.
However, I get a new version of that struct every frame, so I have not tested if structures created with this pattern survive if they exist for a long time. But if that's not the case, then that's a bug :)