I can't find how to simply multiply an Array's values by a single multipler (double every value of an Array for example). The only way I found is to get every single value by index, multiply them individually then rebuild an array, like such:
This is so large, takes a lot of space and is a bit ugly. I can't find any function to multiply an array, is it hidden in another class, like Vector, or has a different name? I can't believe this is the best way of doing this.
As George mentioned in the comments, you can move this code into a function (or macro) to tidy up your main graph and to allow sections of code to be easily reused and maintained. Your example requires knowing exactly how many elements are in your array as you have to grab each element individually and multiply them all one at a time. However, there will be times when your array is generated at runtime and you don't know exactly how many elements it will contain. Not to mention the time and space it will take to do that for a large array.
As George also mentioned, you can use loops to achieve the same result. This will not only simplify your graph, but will allow you to modify arrays without knowing their size in advance. I wanted to post an answer to give you (and others) an example of using loops and functions to achieve this.
The function below takes an array of float
named 'Array Input' and a single float
named 'Multiplier' as inputs. It then uses a For Each
loop to loop through each float
in the array and multiply its value by the provided 'Multiplier'. The original array is modified using Set Array Elem
(Elem is short for Element), where Target Array
is the array to be modified, Index
is the array index of the element to be modified and Item
is the new value to set. The return isn't really necessary in this case, but if you want to do anything after your loop has finished, you can connect it to the Completed
pin.
To test this is working you could loop through the values of the array before and after using the function as shown below.
Alternatively, you could leave the original array intact and get your function to return a new array as shown below. You could just use the Add
function to add each item to your new array, but in this example I've resized the array to the length of the 'Array Input' and then used Set Array Elem
as before. The reason for this is because it will increase performance, particularly with larger arrays. The Add
function will have to resize the array every time you add an element, but the method below only resizes the array once.
The OutputArray
variable is a local variable as you can see on the left. This means that the variable is only available inside the current function and cannot be accessed on the main graph of the blueprint. Instead, you can see that the return node is now used to return the new array once the loop has completed. This can be used as follows from your main event graph.
You can also see this time I have used a variable to specify the multiplier value, whereas the first time I chose to enter it manually in the function node.