Total noob here!

Im totally new to programing so blueprints seems like a good way to get a grasp on building logic. Ive done fairly well so fare and have done this exact thing 2 other times in this “game” im building just to learn. anyway i pull a random and then loop over an array comparing values. when the random is <= to the value in the array it should break and set the index in the variable “win index”. i have it working fine in 2 other blueprints with admittedly much smaller arrays but set up the same way. what am i missing?

Hey,

Couple questions / comments:

  1. I’m assuming you’ve confirmed you’ve put values in your array?
  2. You’re sure at some point the random number you pull is definitely lower than or equal to something in there?
  3. I would probably set the winning index in your true case before you break from the loop.
  4. Your return value isn’t hooked up and is always returning 0 as your win index. Is this intended?

sorry for the late reply. yes the max value for the random is the max value in the array so it is always less than on of the values. also the return not being hooked up is because i wanted to see if setting a variable worked better. it doesn’t but it makes the event graph more clean so i kept it. i didn’t of setting the index before the break and will try that now however could you explain why it makes a difference? not arguing in any way just trying to understand.

thanks again!!

ok ive set the win index before the break but nothing change. let me give you some context. im trying to simulate a small “pay table” like a slot in game. maybe im just doing it wrong… my array has 69 values spread from 400000 to 55887 so this logic is meant to pull a random from 0-55887 then loop through to find the lowest value in the array thats greater than the random and return the index. i then in a later function use that index to pull from another array. the later functions are working as intended. its just this bit that has gone nuts lol. am i even approaching this right? oh and the values in this array are in order.

No worries!

So I looked at how Unreal is implementing their foreach loops, and your use of the index after the complete is fine. To avoid getting too technical, I wasn’t certain of what the scope of the variables in the internal function / macro was. Because you aren’t declaring that index variable, I was anticipating it to work similar to a for loop in written c++ logic. I see looking at their implementation of the for loop they also do the same thing where the last executed index persists after the scope of the loop has ended. That’s neat to know!

Anyways, after reviewing I can maybe see a couple of other things / provide you with some suggestions.

  1. The string you’re printing is the array element value, and but you’re capturing the index. Is this correct?
  2. What value are you currently getting for the index? I should have asked this earlier, but I assumed you were getting a 0 or the max count of the array every time.
  3. What happens if instead of using a random int you use a literal int that you know would hit that condition. Do you get the same result?
  4. Have you cut and pasted the same logic from your other locations just to see if you have things wired up as you had previously?
  5. You could always try to add a breakpoint to the forleach loop and step through to see the values at runtime:

yes im printing the value to of both the random and the array so i can see that its working (or in this case not so much) the problem is that its all over the place. it returns the correct index for the value that is chooses but the problem is it simply chooses the wrong one. not all the time mind you. yes i get mostly index 0 but thats by design. it will often stop early saying that for instance 500000 is less than 400000 but other times it will go the other way and loop too far… i have not seen it reach the end of the array but it seems like it will skip indexes somehow. im thinking im going to try and rebuild it in 4.2 and see what happens. the current build is in 5.3. does that seem like a reasonable thing to try?

I see. Ok so I took your screenshot and re-wrote in my own project to step threw it.

Remember how earlier I mentioned the “scope” of the foreach loop index variable? Well, this is maybe a good example of why I didn’t expect it to retain the value of the last executed index AFTER the foreach loop “completed”.

So stepping through your code, the first thing I do is a pull a random int from 0-558877 (as you have) and print the value. You can see here the value I had was 44294.
image

Continuing on, the next thing I do is go to set my “Local Random” value with the output of the random value, exactly as you had it. I didn’t really think about this earlier until I was writing the code. Then I understood before even running it aha. Pulling another line off this pin doesn’t maintain the value from the first execution. It actually executes it again. So you can see here, when I hit my set value, I’m actually setting it as a new random int. Not the one I printed to the screen moments before. My new value that will be checked against my Win Weights array is 231315. A very different number.

it so happened to be both values were lower than my very first value in the array. So in this specific case I did, both would have ended on index 0, value 400,000 as denoted by the dark blue. But you can see the light blue value was my original, NOT the value I set as Local Random aka the one I was comparing the array. If that value was say 400,001 I’d be expecting index 0 based on the print value, but I’d actually see an index of 1.
image

Please review this and let me know if that fixes your issue.

2 Likes

you are awesome! this explains everything. ill rewrite my code but now that you have explained this makes perfect sense. thanks so much!!

tried it and it works! kinda feel dumb now but thanks so much for the help

1 Like

I’m glad to hear it worked!

Don’t feel dumb. You could write code every day for your entire life and still have days where this stuff happens. Especially just learning, there’s a lot to keep track of. You’re already doing better than most by going to a help source to figure it out rather than just giving up aha.

Keep at it, and good luck with your project! You’re welcome to PM me if you have any more issues.

1 Like