Hey Guys I have a question that I haven’t been able to find the answer to -
I could really do with implementing for loops in most of my functionality, at the moment I have set up a struct because I thought that would be a good way of grouping variables that I will need multiples of for different types, however most of my for loops I set up don’t seem to work, the variables are read correctly but the set members doesn’t seem to work.
Is this a limitation of structs? I can’t find a good way of making my blueprint cleaner and taking all the duplication code out. If I can’t do the for loop here then I would have to paste this same bit of code another 5 times, and across multiple blueprints too, so then if I need to change one thing it becomes an enormous task! If I can’t use structs is there something else? I chose a struct because the PCG graph can read it which is also what I need.
Your random node is problematic since its a pure node but also SetMembers only works on a Ref not a Copy. So your find node is returning a copy. You can add the altered copy back to the Map though
Adding to what Auran13 said, setting current and last random to members in the struct is kinda odd too. If this is happening inside a function it would probably be better to use integer local vars to remember the last index. Use a default value of -1 for that, and on that second branch, check if current = the random index. Then instead of setting last random as struct member, just set the local var.
Hey thank you for the reply I really appreciate it. If you don’t mind me asking, why is a pure random node problematic?
and thank you, that makes sense, but I’m not sure adding the copy back to the map is going to work, to give a some more details I am working in construction script and I have the struct variables visible and editable in the details panel because I am using this for editor work, not runtime, so ideally would like it to affect the ref so that the changes are visible in the details panel but can also still be changed from there too. I have found a get ref array node but that doesn’t seem to get me any farther.
Hey illspiritx thank you for the reply, I did originally do it as a function with local variables and it did work but it would keep picking the same mesh multiple times again. Using the index makes more sense though, I’ve made another function and that is working, just need to be able to set my ref structs. Thanks!
It occurred to me a while ago that getting random indices during a foreach loop could result of some of them not getting used if previously used indices are being discarded.
As such, if you want to randomize an array of structs (or anything really) and ensure they all get used, you could shuffle the array then use a regular for loop with the last index pinned to array length --1. Like so:
Shuffling an array inside a struct member could get messy tho. Probably have to loop thru the struct array once to shuffle, then loop thru again to get usable results. In my case, I was just getting random icons to throw on widget grid lol.
The random node is problematic because you are calling it twice which will give two different results.
Adding back to the map will work but again your map needs to be a ref not a copy since your map is inside a structure you may need to add the whole map back to the structure. It’s kind of complicated in blueprint but really easy in c++ if you’re familiar.
Too really understand it you may need to step through breakpoints and see what’s happening
Ah yes ok I see that now! Same as the random stream nodes then you have to set a variable and then use that variable got it thank you!
I am still struggling with the rest though, I have tried removing the old and then adding the updated struct back into the map, and then even tried using that to update the original afterwards but I still get no updates in the details panel. Is structs just not the right thing to use? I have put some breakpoints but it doesn’t seem to tell me information about the struct which one it is and if its a ref or copy unless I am missing something.
Ah okay I see so you don’t need to add and remove nodes because the get node means it changes that item in the array for you - This is actually working now, sort of!
Ah that’s really cool, I think because I have such small arrays, there is a good chance it could end the shuffle on the same one that it starts on next time but I might give it a go and see !
Just in case anyone else gets stuck on this - I’ve managed to get it working quite well. I do my loop and update the array with a get ref and also saved the index number, then I’ve used that index number to do a on int switch and set the actual structure variable manually there. So I can do all the loops I want then set it at the end. Hope that makes sense!