Passing/declaring global associative array to/in function Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dzverry
Posts: 25
Joined: 27 Mar 2021, 00:40

Passing/declaring global associative array to/in function

Post by Dzverry » 17 Mar 2024, 02:59

Hello fellow AutoHotkeyers, it's me again with yet another question...

Code: Select all

counters := {cats: 3, dogs: 5, ducks: 7}

petwars()

petwars()
{
	if ( counters["ducks"] > counters["dogs"] + counters["cats"] )
	{
		MsgBox, Ducks rule!
	}
	else
	{
		if  counters["dogs"] > counters["cats"]
			MsgBox, You're a dog person
	 	else
		 	MsgBox, You're a cat person
	}
}
It's a very dumbed down way of trying to explain what I'm trying to achieve with this but I think it gets the point across. So, how could I pass this associative array, a specific value or declare it global so it's values can be accessed inside a function? I only need to read them, not edit.
Bonus points if that can be done in a timer function because I would be calling it from inside a timer, but I can get around that if it isn't possible. :D

As per usual, thanks in advance!

Rohwedder
Posts: 7659
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Passing/declaring global associative array to/in function  Topic is solved

Post by Rohwedder » 17 Mar 2024, 04:19

Hallo,
try:

Code: Select all

counters := {cats: 3, dogs: 5, ducks: 7}

petwars()

petwars()
{
	Global counters
	if ( counters["ducks"] > counters["dogs"] + counters["cats"] )
	{
		MsgBox, Ducks rule!
	}
	else
	{
		if  counters["dogs"] > counters["cats"]
			MsgBox, You're a dog person
	 	else
		 	MsgBox, You're a cat person
	}
}
or:

Code: Select all

#Persistent
counters := {cats: 3, dogs: 5, ducks: 7}
pet := Func("petwars")
SetTimer,% pet, 1000

petwars()
{
	Global counters
	SoundBeep, 4000, 20
	if ( counters.ducks > counters.dogs + counters.cats )
		ToolTip, Ducks rule!
	else if  counters.dogs > counters.cats
		ToolTip, You're a dog person
	else ToolTip, You're a cat person
}

Dzverry
Posts: 25
Joined: 27 Mar 2021, 00:40

Re: Passing/declaring global associative array to/in function

Post by Dzverry » 17 Mar 2024, 21:14

Rohwedder wrote:
17 Mar 2024, 04:19
Hallo,
try:

Code: Select all

counters := {cats: 3, dogs: 5, ducks: 7}

petwars()

petwars()
{
	Global counters
	if ( counters["ducks"] > counters["dogs"] + counters["cats"] )
	{
		MsgBox, Ducks rule!
	}
	else
	{
		if  counters["dogs"] > counters["cats"]
			MsgBox, You're a dog person
	 	else
		 	MsgBox, You're a cat person
	}
}
or:

Code: Select all

#Persistent
counters := {cats: 3, dogs: 5, ducks: 7}
pet := Func("petwars")
SetTimer,% pet, 1000

petwars()
{
	Global counters
	SoundBeep, 4000, 20
	if ( counters.ducks > counters.dogs + counters.cats )
		ToolTip, Ducks rule!
	else if  counters.dogs > counters.cats
		ToolTip, You're a dog person
	else ToolTip, You're a cat person
}
Funny, I tried the first answer you provided before I posted this, however, it did not seem to work and now it does, lol.
Well, thanks for the magic dust you sent my way to make it work :D

Post Reply

Return to “Ask for Help (v1)”