Script Logic - GlitchedSouls/AHK-Guide GitHub Wiki

What is Script Logic?

When I talk about script logic, I'm referring to the logical steps the script must take in order to produce the outcome that you're expecting. A script will only do what you tell it to, it won't think for itself, and it won't produce the outcome you're expecting unless you tell it to.

I still don't know what you mean, can you explain in more detail?

Certainly, AHK gives you multiple tools at your disposal to create anything you want. It can be as simple, or as advanced as you make it, but to create any script, you have to understand the logical steps your need to take.

When I want to create a script, I plan out the steps in simple text format, for example:

  1. Press F1
  2. Left Click at x = 200, y = 200
  3. Sleep for 1 second
  4. Stop

Then, I can use AHK to turn that into a script.

F1::

	MouseClick, Left, 200, 200

	Sleep 1000

	return

I hope that gives a practical example of how you can visualise what I mean when I refer to the 'Script Logic'. Now let's try something a bit more tricky.

I want to click the left mouse button at x = 200, y = 200, but only if I find the colour red with a colour variation of 5 in the area of x1 = 500, y1 = 500, x2 = 650, y2 = 650. If the colour is not found, I want to sleep for 1 second and then recheck if the colour is found again. I want to recheck the colour indefinitely until found, and when the colour is found, I want to stop searching for it.

  1. Loop the following:
  2. PixelSearch Red Colour, x1 = 500, y1 = 500, x2 = 650, y2 = 650 2a. If the colour is found, click left at x = 200, y = 200, finish loop 2b. If the colour is NOT found, sleep 1 second.
  3. Loop back to the top

Before I show you how to do it, why not try it and have a go.

F1::
	Loop {
	PixelSearch, PX, PY, 500, 500, 650, 650, 0xFF0000, 5, RGB Fast
		if (!ErrorLevel) { ; No Error - Colour has been found.
			MouseClick, Left, 200, 200
		}
		else if (ErrorLevel) { ; Error - Colour has not been found.
			Sleep 1000
		}
	}
	return