When I try to follow these guidelines or even copy this piece of code I get the following error:
This invocation calls a function that has the ‘decides’ effect, which is not allowed by its context. The ‘decides’ effect indicates that the invocation calls a function that might fail, and so must occur in a failure context that will handle the failure. Some examples of failure contexts are the condition clause of an ‘if’, the left operand of ‘or’, or the clause of the ‘logic’ macro.
What does it do wrong if the documentation shows it is allowed? The only solution I found is to rewrite the array and change the data in a specific position, but it seems illogical that this is the only way to change the value in the array.
So I didn’t know that you could actually specify an index for which to change in an array, I thought it was only for maps for some reason.
But to clarify what @TheRealGwH said, calling set ExampleArray[1] = 77 is failable because the index 1 could not exist yet inside this array, thus the operation would fail and your entire block would crash, (this is why the compiler won’t let you do it) but wrapping it inside a failable context allows you to do that without worrying.
You can use the method the guy provided, with an if statement, but I find this method more readable though :
# Any of these
option{set ExampleArray[1] = 77}
option. set ExampleArray[1] = 77
option:
set ExampleArray[1] = 77
Using the if statement, you’d still be able to manage the case where the assignement failed, so I guess it’s still ok to use anyway
Yeah I know, I’m just saying that it’s not really readable, for me atleast, I’m not used to mutate variables inside if statement, in any language you know…
When using an if, it implicitly suggests to have a then part, from an english language perspective. It’s not because it exists that we have to use it.
Thank you all for your reply. I understand this error and I know that it can be circumvented by putting it in the if statement, but I am wondering why the official documentation provided by epic includes the option not to use the if statement. I was wondering if I was making a logical error somewhere, but as you can see, there is simply an error in the documentation.
You are correct, this is an error in the documentation. We will get that fixed. Thanks for catching that!
Also good explanation by @Ep8Script of using if vs option as a failure context. If you wanted to store the result of a failure expression, option is a good…option! But, if you just want to do the thing in the failure expression and you don’t need to store the result, if might be a better choice.