May 14, 2009

How to get rid of that division by 0 error

Hello, and welcome to this blog. This is for all those noobs at GM (Game Maker) who are getting started with coding. The division by zero error is known and hated by all, from Game Fortress to some random guy who uses Drag & Drop.

The division by zero error is the plague of all division problems. It's a rather simple fix, just make sure the number you divide by is never zero. (Accomplished by setting the variable to 1, or something else, in the create event, and not setting it to 0.)

If that is not possible, try this quick fix: (Note, this code was made for those beginning to use code, most people have learned it at one point or another, I'm simply publishing it.)

if [yourvariablehere]=0 //Check whether your denominator is 0.
then exit //This will leave the current chunk of code, be sure to put it at the end.
//alternatively to exit, you can use "then wewt=0".
else wewt=lol/[yourvariablehere] //Do the maths.


The above fix simply bypasses the math if the denominator is zero, you may want to replace exit with a code fix (Such as setting the variable to zero), as exit can cause other problems if used wrong.