ahkget ahkassign help?

Post AHK_H specific scripts & libraries and discuss the usage and development of HotKeyIt's fork/branch
kgdeem
Posts: 2
Joined: 17 Jul 2018, 11:45

ahkget ahkassign help?

12 Jan 2019, 17:43

myhandle.ahkassign("variable","stringvalue")

cool.

but how to % myarray[stringvalue] or %stringvalue% in this code?

i cannot figure how?


also, parentscript.ahk can send and recieve vars to childscript.ahk via ahkget/ahkassign... cool. but how can childscipt.ahk send/rec back to parentscript? and how can it send/rec to siblingscript.ahk? thanks.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ahkget ahkassign help?

12 Jan 2019, 19:58

Code: Select all

; the main script is the 'Parent', as per ur terminology
ParentsMsg := "Parent's message is 'ooga booga'"

ChildScript =
(LTrim %`
	#Persistent
	ChildsMsg := "Child's message is 'sample text'"
)
ChildThread := AhkThread(ChildScript)

SiblingScript =
(LTrim %`
	#Persistent
	SiblingsMsg := "Sibling's message is 'lorem ipsum'"
)
SiblingThread := AhkThread(SiblingScript)

; parent reads var from child
ChildsMsg := ChildThread.AhkGetVar.ChildsMsg
MsgBox % "parent reads var from child`n`n" ChildsMsg

; parent sets var in child
ChildThread.AhkAssign("msg", "Child says hi")
ChildThread.AhkExec("MsgBox % ""parent sets var in child``n``n"" msg")

; child reads var from parent
ChildThread.AhkExec("
(LTrim %`
	ParentThread := AhkExported() ; handle to main thread
	ParentsMsg := ParentThread.AhkGetVar.ParentsMsg
	MsgBox % ""child reads var from parent`n`n"" ParentsMsg
)")

; child sets var in parent
ChildThread.AhkExec("
(LTrim %`
	ParentThread.AhkAssign(""msg"", ""Parent says hi"")
	ParentThread.AhkExec(""MsgBox % """"child sets var in parent``n``n"""" msg"")
)")

; child reads var in sibling
ChildThread.AhkAssign("SiblingThreadPtr", &SiblingThread "") ; ptr need to get in the script somehow. how u achieve this is up to u(assign, cmd line arg, criticalobject, plain pasting it in the middle of the script string)
ChildThread.AhkExec("
(LTrim %`
	SiblingThread := Object(SiblingThreadPtr) ; or use criticalobject
	SiblingsMsg := SiblingThread.AhkGetVar.SiblingsMsg
	MsgBox % ""child reads var in sibling`n`n"" SiblingsMsg
)")

; child sets var in sibling
ChildThread.AhkExec("
(LTrim %
	SiblingThread.AhkAssign(""msg"", ""Sibling says hi"")
	SiblingThread.AhkExec(""MsgBox % """"child sets var in sibling````n````n"""" msg"")
)")
run from v1, run far and away
kgdeem
Posts: 2
Joined: 17 Jul 2018, 11:45

Re: ahkget ahkassign help?

13 Jan 2019, 12:25

thankyou swagfriend. this is the tutorial the internet needed, but seemingly didnt have.

what terminology would you use? local/sub? master/slave? primary/secondary?

also (if youve seen it) in eugen's video with joe... he was saying that he didnt like using this code, that he preferred to use objects for this purpose. However the webcam connection was lagging hard when he was covering how to use objects and the subject got glossed over too much for my understanding. To revisit that idea, how would this be done using objects instead of ahkget/assign? ..would a sqllite wrapper be an even better solution so all 3 can just feed and pull vars from a db?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: ahkget ahkassign help?

13 Jan 2019, 14:29

Code: Select all

; v2

ChildScript := "
(
	#Persistent

	PredefinedChildVar := "I am child"

	MsgBox A_Args[1] A_Args[2] A_Args[3] ; print cmd line args

	PredefinedChildFunction() {
		MsgBox "You called the predefined child function"
	}

	adder(a, b) {
		return a + b
	}

	uppercase(Array) {
		for i in Array
			Array[i] := Format("{:U}", Array[i])
	}
)"

ChildThread := ThreadObj(ChildScript, "Child says hi") ; Parent passes cmd line args(needs to be string) and creates a thread(Child)

MsgBox ChildThread.PredefinedChildVar ; Parent reads var from Child

ChildThread.ParentSetVar := "I am parent" ; Parent sets var in Child

ChildThread.Exec("MsgBox ParentSetVar") ; Parent executes command in Child, Child prints var in Child

; this crashes for some reason
; ChildThread.PostCall("PredefinedChildFunction") ; Parent calls func in Child, dont wait for return

MsgBox ChildThread.Call("adder", 1, 2) ; Parent calls func in Child, wait for return, Parent gets and prints result

Arr := ["h", "e", "l", "l", "o"] ; regular array
ParentCritArr := CriticalObject(Arr) ; thread safe obj
for each, char in ParentCritArr
	res .= char
MsgBox "array before: " res ; array before: hello

ChildThread.Exec(Format("
(
	; call CriticalObject() on the address of an object wrapped by CriticalObject() to get the object back.
	; how u manage to pass this address is up to u
	ChildCritArr := CriticalObject({})

	uppercase(ChildCritArr) ; do something with the critical object
)", &ParentCritArr))

res := ""
for each, char in ParentCritArr
	res .= char
MsgBox "array after: " res ; array after: HELLO

Return to “AutoHotkey_H”

Who is online

Users browsing this forum: No registered users and 22 guests