Page 2 of 2

Re: Classes in AHK, Basic tutorial.

Posted: 24 Jul 2018, 09:40
by Pall Parker
Thanks for the tutorial!!!

Re: Classes in AHK, Basic tutorial.

Posted: 30 Jul 2018, 15:34
by Galaxis
classes remind me of ARRAYS

Re: Classes in AHK, Basic tutorial.

Posted: 01 Jan 2019, 01:58
by Sabestian Caine
Great tutorial on Classes. :bravo: :bravo: :bravo:

But, still I am not getting how to use classes in simple stuffs?? :roll: :roll: :roll:

Re: Classes in AHK, Basic tutorial.

Posted: 27 Jan 2019, 03:44
by Albireo
Thank's!
I like the simplicity of the lesson of Class above.
I think even, that the description / comments is good when the complexity increases.

However, there are still parts that I do not understand. (eg. this.ID - is there more similar "variables" like this.?)
Searched in AHK help file but did not find anything.

Another thing that I didn't get to operate, as I desire, is the calculator. Wrong windows can be activated.
(for example, Explorer can become active instead of the Calculator - Maybe it is the programscript / Win 10 / Swedish version?)
How can I be sure that the right window is activated (Now it was so - most of time - but I just had luck?)
Perhaps the example had worked better if the instruction WinActivate had been supplemented with which window to be activated?.

Another problem that arose was when the Notepad were opened. Notepad were opened so quickly that it became a wrong header.
I solved this with the following code .:

Code: Select all

...
IfWinNotExist ahk_class Notepad
{	Run Notepad
	WinWait ahk_class Notepad
}
...
Finally, although the example does not handle problems, is it perhaps important?
If a code doesn't work as intended (for various reasons)
For example, if nothing happens, an error management can give tips on what went wrong?

I think the example shows that Class can add details that cannot be done otherwise (eg. __New)
Thanks again! :bravo:

Re: Classes in AHK, Basic tutorial.

Posted: 08 Jun 2019, 03:23
by YoucefHam
samardac wrote:
20 Jan 2015, 12:24
Thank You, :salute:

You make my life a lot easy, before I was just afraid when I see Class in any script, now because of you I am not.
Thank you. :thumbup: :thumbup: :thumbup:

I tried to practice on Class, here what I get, well just basic shooter game. :D

Code: Select all

;############################################################
;#======================= Commands =========================#
;############################################################ ;{
if Not A_IsAdmin AND A_OSVersion <> WIN_XP
{
	Run *RunAs "%A_ScriptFullPath%"
	ExitApp
}
Process, Priority,, High
SetWorkingDir %A_ScriptDir%
#NoEnv
#KeyHistory 0
SendMode Input
CoordMode, ToolTip, client
CoordMode, Mouse, client
CoordMode, Pixel, client
;}

;############################################################
;#======================= Initialise =======================#
;############################################################ ;{
i := 1
me := new Player

W1 := new Weapon, W1.setID("W" i++), W1.setName("M416"), W1.setFireMode(1)
				  W1.setClipSize(30), W1.setClipMagazine(30), W1.setAmmo(160)
				  W1.setFireRate(0.90)

W2 := new Weapon, W2.setID("W" i++), W2.setName("AK47"), W2.setFireMode(1)
				  W2.setClipSize(30), W2.setClipMagazine(30), W2.setAmmo(120)
				  W2.setFireRate(1.20)

W3 := new Weapon, W3.setID("W" i++), W3.setName("GLOCK"), W3.setFireMode(0)
				  W3.setClipSize(15), W3.setClipMagazine(15), W3.setAmmo(90)
				  W3.setFireRate(2.50)

me.setActiveWeapon(W1)
me.setFireMode(me.getActiveWeapon())
;}

;############################################################
;#====================== Main Window =======================#
;############################################################ ;{

Gui, Color, Black
Gui, Font, s10 cWhite q5, Verdana

Gui, Add, Picture, x522 y480 w-1 h90 vfmOSD, AUTO.png
Gui, Add, Picture, x200 y480 w-1 h90 vwOSD, M416.png

Gui, Add, GroupBox, x200 y405 w400 h50 vW, Weapons [1] [2] [3]
Gui, Add, Radio, x222 y429 vW1 gsetActiveWeapon Checked, M416
Gui, Add, Radio, x372 y429 vW2 gsetActiveWeapon, AK47
Gui, Add, Radio, x522 y429 vW3 gsetActiveWeapon, Glock

Gui, Add, GroupBox, x310 y350 w180 h50, Fire Mode [B]
Gui, Add, Radio, x322 y370 gChangeFireMode vFM1 Checked, Auto
Gui, Add, Radio, x412 y370 gChangeFireMode vFM0, Single

Gui, Font, s32 cGreen q5, Verdana
Gui, Add, Text, x200 y350 vClipMagazine, % me.getActiveWeapon().getClipSize()
Gui, Add, Text, x522 y350 vAmmo Right, % me.getActiveWeapon().getAmmo()

Gui, Show, w830 h615, Weapon Simulator
return
;}

;############################################################
;#======================== HotKeys =========================#
;############################################################ ;{
#IfWinActive, Weapon Simulator
;#####################################
*~LButton::
if (me.getActiveWeapon().getFireMode())
{
	Loop
	{
		me.FireWeapon(me.getActiveWeapon())
		Sleep, % (me.getActiveWeapon().getFireRate() * 100)
	}until !GetKeyState("LButton")
}
else
	me.FireWeapon(me.getActiveWeapon())
return
;#####################################
*$r::
me.ReloadWeapon(me.getActiveWeapon())
Sleep, % (me.getActiveWeapon().getFireRate() * 8)
return
;#####################################
*$VK31::
me.setActiveWeapon(W1)
me.setFireMode(me.getActiveWeapon())
return
;#####################################
*$VK32::
me.setActiveWeapon(W2)
me.setFireMode(me.getActiveWeapon())
return
;#####################################
*$VK33::
me.setActiveWeapon(W3)
me.setFireMode(me.getActiveWeapon())
return
;#####################################
*$x::
return
;#####################################
*$b::
me.ChangeFireMode(me.getActiveWeapon())
return
;#####################################
;}

;############################################################
;#======================= Functions ========================#
;############################################################ ;{
class Weapon {
	;~ static WeaponFireMode := 1, ID := W1, Name := "M416", ClipSize := 30, ClipMagazine := 30, Ammo := 160, FireRate := 0.02
	;#####################################
	getID() {
		return this.ID
	}
	setID(newID) {
		this.ID := newID
		return
	}
	;#####################################
	getName() {
		return this.Name
	}
	setName(Name) {
		this.Name := Name
		return
	}
	;#####################################
	getFireMode() {
		return this.WeaponFireMode
	}
	setFireMode(newWeaponFireMode) {
		this.WeaponFireMode := newWeaponFireMode
		return
	}
	;#####################################
	getClipSize() {
		return this.ClipSize
	}
	setClipSize(value) {
		this.ClipSize := value
		return
	}
	;#####################################
	getClipMagazine() {
		return this.ClipMagazine
	}
	setClipMagazine(value) {
		this.ClipMagazine := value
		return
	}
	;#####################################
	getAmmo() {
		return this.Ammo
	}
	setAmmo(value) {
		this.Ammo := value
		return
	}
	;#####################################
	getFireRate() {
		return this.FireRate
	}
	setFireRate(value) {
		this.FireRate := value
		return
	}
}

;#####################################
;#####################################
class Player {
	;~ static ActiveWeapon := W1, ActiveFireMode := 1, obj, ID
	;#####################################
	getID(ActiveWeapon) {
		return ActiveWeapon.getID()
	}
	;#####################################
	setActiveWeapon(ActiveWeapon) {
		this.ActiveWeapon := ActiveWeapon
		GuiControl,, % ActiveWeapon.getID(), 1
		GuiControl,, wOSD, % ActiveWeapon.getName() ".png"
		GuiControl,, ClipMagazine, % ActiveWeapon.getClipMagazine()
		GuiControl,, Ammo, % ActiveWeapon.getAmmo()
		return
	}
	getActiveWeapon() {
		return this.ActiveWeapon
	}
	;#####################################
	getFireMode() {
		return this.ActiveFireMode
	}
	setFireMode(ActiveWeapon) {
		this.ActiveFireMode := ActiveWeapon.getFireMode()
		GuiControl,, % "FM" ActiveWeapon.getFireMode(), 1
		GuiControl,, fmOSD, % (ActiveWeapon.getFireMode() ? "AUTO.png" : "SINGLE.PNG")
		return
	}
	ChangeFireMode(ActiveWeapon) {
		this.ActiveFireMode := ActiveWeapon.getFireMode()
		ActiveWeapon.setFireMode(!this.ActiveFireMode)
		GuiControl,, % "FM" !this.ActiveFireMode, 1
		GuiControl,, fmOSD, % (ActiveWeapon.getFireMode() ? "AUTO.png" : "SINGLE.PNG")
		return
	}
	;#####################################
	FireWeapon(ActiveWeapon) {
		if (ActiveWeapon.getClipMagazine() > 0)
		{
			ActiveWeapon.setClipMagazine(ActiveWeapon.getClipMagazine() - 1)
			GuiControl,, ClipMagazine, % ActiveWeapon.getClipMagazine()
		}
	}
	ReloadWeapon(ActiveWeapon) {
		Sleep, % ActiveWeapon.getFireRate() * 5
		if (ActiveWeapon.getAmmo() > 0)
		{
			dClip := ActiveWeapon.getClipSize() - ActiveWeapon.getClipMagazine()
			if (ActiveWeapon.getAmmo() - dClip > 0 )
			{
				ActiveWeapon.setAmmo(ActiveWeapon.getAmmo() - dClip)
				goto, Step1
			}
			else if (ActiveWeapon.getAmmo() - dClip = 0 )
			{
				ActiveWeapon.setAmmo(0)
				goto, Step1
			}
			else if (ActiveWeapon.getAmmo() - dClip < 0 )
				if (ActiveWeapon.getAmmo() >= 0 )
				{
					ActiveWeapon.setClipMagazine(ActiveWeapon.getClipMagazine()+ActiveWeapon.getAmmo())
					ActiveWeapon.setAmmo(0)
					GuiControl,, Ammo, % ActiveWeapon.getAmmo()
					Sleep, 320
					GuiControl,, ClipMagazine, % ActiveWeapon.getClipMagazine()
					return
				}
			return
			Step1:
			GuiControl,, Ammo, % ActiveWeapon.getAmmo()
			Sleep, 320
			ActiveWeapon.setClipMagazine(ActiveWeapon.getClipMagazine()+dClip)
			GuiControl,, ClipMagazine, % ActiveWeapon.getClipMagazine()
			return
		}
	}
	;#####################################
}
;#####################################
setActiveWeapon:
me.setActiveWeapon(%A_GuiControl%)
me.setFireMode(me.getActiveWeapon())
return
;#####################################
ChangeFireMode:
me.ChangeFireMode(me.getActiveWeapon())
return
;#####################################
GuiClose:
ExitApp
;##################################### ;}
Image

Script + Pictures:
Weapon Simulator.rar
Script + Pictures
(903.37 KiB) Downloaded 32 times