Why is my While loop crashing my UdonBehaviour Program?

I’m creating a currency system similar to World of Warcraft (Copper, Silver and Gold) so when the player reaches 100 copper, it turns into 1 silver, and so on for silver to gold. I’ve made a little debug log that shows me the player’s level, monster’s level, how much experience was earned and how much money is earned. I’m wanting to translate this system into the debug log but since the trigger is an interact event and not an update event, it isn’t able to do the subtraction part multiple times in a row. My solution to this was going to be a While loop but it seems to crash my program anytime the value goes over what I’ve put. Anyone got any information about this? This is my first time attempting to use a While loop.

You might want to use a for loop for this…

While loops can be funky and never exit for one reason or another, but you kinda already know the answer to your own question.

Simply divide the copper by 100 and use that in your subtraction.

If I for example have 852 copper, I know I either need to use a for loop 8 times, or I can just subtract 800 copper and add 8 silver.

You’re kinda overthinking it :slight_smile:

Right but how do I tell it to subtract specifically 800 copper and not the extra 52? :thinking:

Let me try to explain my system.

I click a button which is simulating me killing a monster.

Based on monster’s level, it gives me experience and money.

The part where it gives me money works and is fine, but the part that logs how much money I earned in THAT kill is the part where I’m kinda struggling. The thing is, the variables I’m using need to be zero for the next kill I do for it to calculate it properly.

This part gives me the player the money, while also setting the temporary money for the log.

This part is a temporarily very sloppy attempt at calculating the logged part.

This part is what calculates the player’s money. It’s trigger is an Update event. It works just fine.

I’ve thought about how I’d use a for loop but I couldn’t think of a way to do it properly. If the result of my math has a decimal and goes over x.5, it’ll round it up which wouldn’t be accurate… I guess I could technically just convert to float and subtract 0.45 and then convert back to an integer so it always rounds to the correct number? It seems very sloppy though.

Edit: Oh one last thing… Dividing the copper by 100 for the Silver is cool and all, but have you thought about the logistics of the Gold side of things? That’s the main issue. Let me explain a bit here. Imagine you got 124,000 Copper from one kill. Now divide that by 10,000 (to calculate the Gold) and you get 12.4. As an integer this will round down to 12 and work fine. Cool. Now what if you got 126,000 Copper from one kill instead. It’d round it up to 13, wouldn’t it? This problem still applies to your example of 852 Copper though. Dividing that by 100 would give me 8.52 which would round up to 9, giving me 9 silver instead of 8. Thinking about it, converting to float and subtracting by 0.45 will actually solve my problem so I’m gonna go set up a for loop with that math and see how it goes.


It seemed like it was going well until this happened…

Here’s the for loops I made.

what if try like this?

moneyInCopper = 80502 #example
tmp = moneyInCopper
gold = 0
silver = 0
copper = 0
while tmp >= 10000
gold = gold + 1;
tmp = tmp - 10000
while tmp >= 100
silver = silver + 1
tmp = tmp - 100
copper = tmp

It seems negative numbers are a common thing with this For loop method. For example in the last screenshot, instead of showing 10 silver and 99 copper, it shows 11 silver and -1 copper. While this is TECHNICALLY correct, it’s not what it’s supposed to be doing at all.

Edit: I figured out the problem… If the number ever happens to be x.5 on the dot, it’ll round up regardless and subtract an extra time. For example, subtracting 0.45 from the float value with a Copper input of 895 will leave you with 8.5 after dividing by 100, meaning it’ll round up anyways and you’ll end up with negative numbers.

I apologize for my stupidity. Found a really cool thing in Mathf called Floor that automatically rounds down no matter what. Crisis averted.

Here you go, a way of doing it in udon graph.
First I convert the copper int to a float, which I then divide by 100.
I use mathf.floor to round it down to the base number (so from 8.xx to just be 8).
Then I convert that back to an int and add that to the silver.
I then multiply it by 100 to get the amount I need to subtract from the copper and update the copper accordingly.

1 Like

Oooo that’s an interesting way of doing it without a For loop. Thanks. Sorry if I seem like a bit of a spaz, I’ve been working on this thing for like 10 hours straight and nothing is more annoying than being on a streak and getting stopped by one tiny little thing for hours.

Welcome to programming :slight_smile:

I do side with ulti though, not in his implementation, but the usage of C#.
You can use C# in VRChat using Udon# and it’ll probably simplify your process a lot.
(As well as making things faster)

But seeing you work, you’re probably still new to programming and learning things, so don’t sweat it if you don’t pick it up now. You can always start learning it later.

1 Like