Modulo (Remainder) method for your waypoints

In case anyone was wondering what Rick was talking about when he mentioned modulo arithmetic, here’s what that is: if you remember when you first learned division in grade school, and if it didn’t divide evenly, you had what’s called a “remainder” - the remainder is what is the outcome of a modulo operation (I’ve only ever heard it called “modulo” in the domain of programming - if this exists anywhere else, feel free to point it out!)

The modulo operation is represented with the percent symbol (%). And the reason it works so well here with the waypoint loop is that, as Rick pointed out, you want to go back to waypoint index 0 after reaching the last one.

So given index i of the current waypoint, if you were to divide i+1 by the total number of waypoints, once you reached the end, your division operation would give you 1 with a remainder of 0; otherwise, you would just get 0 with the remainder still being i+1. And conveniently enough, we can reuse transform.childCount to get that total number of waypoints.

So you can use the if statement, as Rick shows, and that will work. But if you want a more concise operation, you can simply put this into your GetNextIndex() function:

return (i + 1) % transform.childCount;

Come to think of it, you may not even need a separate method in that case - you could just put that in the for loop and have the PatrolPath script be that much cleaner.

Hope that clarifies things for anyone who’s interested! :grin:

3 Likes

you could also use

                int j = (i < transform.childCount-1) ? i+1 : 0;

While you can do it this way, I think it serves more to obfuscate what is done there and why.

At least make it symmetrical by having the condition similar to the assignment side:

int j = (i+1 < transform.childCount) ? i+1 : 0;

lol when I used to work in teaching CS to high school kids, grade 7ers specifically, I did my absolute best to clear the modulo concept as hard as I possibly can…

Gosh it was hard for them to understand it without a bit of mathematics on the board, and then the distrcating ones would ask me why are we dealing with maths now… :confused:

Cue the old joke “nobody told me there would be math!”

In programming (especially game programming), both mathematics (up to and including Trigonometry and Calculus) and symbolic logic are fundamental to both creating code and understanding it.

lol they used to go wild on me, and every 5 minutes when I ask them “you guys got this point?” (and I know they didn’t, and it was NOT because I wasn’t explaining it clearly), they’ll say yes and when I ask them “OK explain it.”, they’d go silent (and the worst part is, I used to explain everything from the core of the concept of my knowledge)

Eventually they replaced me with some lady that my ex-students keep telling me she has no clue what she’s talking about… (and my old supervisor is a traditional arab, so delivering fundamental concepts for him in English was a serious challenge)

Talk about a class that won’t listen… Good short-term memories though

Anyway, let me go investigate why my patrol state speed isn’t alligned properly

Privacy & Terms