Strange object behavior with dynamic name Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Strange object behavior with dynamic name

17 May 2021, 13:11

How is Test_Object visible when we call it with a dynamic name of %name%_Object and not visible when we call it directly like Test_Object. It even breaks if we call both in a row. I expect the dynamic call to also fail but it doesnt for some reason. Bug or intended?

Code: Select all

#SingleInstance Force

Build_Object()

return

Build_Object()
{
	global
	
	Test_Object := { Test1: "Red", Test2: "Yellow"}
}

^t::
Test_Object()
return

Test_Object()
{ 
	name := "Test"
	
	msgbox, % %name%_Object.Test1 ; will display "Red" ?? how
	;~ msgbox, % Test_Object.Test1  ; no output
}


this is the "common source of confusion" isnt it... i feel silly
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Strange object behavior with dynamic name

17 May 2021, 13:54

You have a problem with your premise. Neither works in your function as you currently have it because of variable scope. If you make the function global, then both work.
User avatar
mikeyww
Posts: 26890
Joined: 09 Sep 2014, 18:38

Re: Strange object behavior with dynamic name

17 May 2021, 13:57

I found this interesting because the following displays, "Red".

Code: Select all

Build_Object(), Test_Object()

Build_Object()
{
	global
	Test_Object := { Test1: "Red", Test2: "Yellow"}
}

Test_Object()
{ 
	name := "Test"
	msgbox, % %name%_Object.Test1
}
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Strange object behavior with dynamic name

17 May 2021, 13:58

No i understand that. I don't want the object to be global however in the example I posted I am getting "Red" in the message box. Running Version 1.1.33.02. So am I not crazy?
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

Re: Strange object behavior with dynamic name

17 May 2021, 14:00

Same behavior on regular variables as well:

Code: Select all

Build_Object(), Test_Object()

Build_Object()
{
	global
	Test_Var := "Hello"
}

Test_Object()
{ 
	name := "Test"
	msgbox, % %name%_Var
}
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Strange object behavior with dynamic name

17 May 2021, 14:02

Both MsgBoxes display "Red":

Code: Select all

#SingleInstance Force

Build_Object()

return

Build_Object()
{
	global
	
	Test_Object := { Test1: "Red", Test2: "Yellow"}
}

^t::
Test_Object()
return

Test_Object()
{ 
	global
	name := "Test"
	
	msgbox, % %name%_Object.Test1
	msgbox, % Test_Object.Test1
}
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Strange object behavior with dynamic name  Topic is solved

17 May 2021, 14:04

More_about_locals_and_globals wrote:Within a function (unless force-local mode is in effect), any dynamic variable reference such as Array%i% always resolves to a local variable unless no variable of that name exists, in which case a global is used if it exists.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: cjsmile999 and 356 guests