Returns of a task started from within another task

Discuss RoboTask here
Post Reply
Lana_K
Posts: 52
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Returns of a task started from within another task

Post by Lana_K »

To me 'Start task' action is very useful when the same sequence of actions is needed repeatedly in the main task. I use this functionality exactly as I would use an external function. I can easily pass several parameters to it which is incredibly convenient. Usually these tasks modify or extract various data which I then need to use in the main task. Unfortunately, by now I figured out only one way to get the results of such external task back into the main task - to write the values into a file by the external task and then read them back into a variable from the main task.

I wonder if there is a more elegant solution to pass the value(s) of variable(s) back without creating any files?

Thank you.

Sincerely,
Lana
Oleg
Site Admin
Posts: 3088
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Returns of a task started from within another task

Post by Oleg »

Task can return some result as string. Just save necessary value to TaskResult variable
Save task result to variable
When the action waits task for finish you can save the task result to variable. The task can return a result. To return some result, simply set the task's variable named TaskResult in the task.
Also you can retrieve the last task result anytime. Use system variable GetTaskResult(<ExtName>)
This is in Start Task chapter
GetTaskResult system variable can accept external name or task ID
Oleg Yershov
Lana_K
Posts: 52
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Re: Returns of a task started from within another task

Post by Lana_K »

Thank you very much. Now I understand what these variables are for. I'll try this method.
Do I understand correctly that there can be only one and only variable TaskResult and in case of any change in its name (like TaskResult1, TaskResult2, etc) its value won't be returned by GetTaskResult?
Oleg
Site Admin
Posts: 3088
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Returns of a task started from within another task

Post by Oleg »

...only variable TaskResult and in case of any change in its name (like TaskResult1, TaskResult2, etc) its value won't be returned by GetTaskResult...
The result of function or task is only one value.
If you want to return several values you can use JSON format. Read here detailed information about this
Also look at my small examples below.

The Multy-result task returns several values in JSON format:

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task2"
Hide=INTEGER|0
ID=INTEGER|1180088085
LogOnAsUser=INTEGER|1
Name=STRING|"Multy-result task"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""json:TaskResult.result1"" with value ""Hello World!!!"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"json:TaskResult.result1"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"json:TaskResult.result1"
varvalue=STRING|"Hello World!!!"

[Actions\Action2]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""json:TaskResult.result2"" with value ""Another result"""
Params=FOLDER

[Actions\Action2\Params]
_rt_variables_produced=STRING|"json:TaskResult.result2"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"json:TaskResult.result2"
varvalue=STRING|"Another result"

[Actions\Action3]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""json:TaskResult.result3"" with value ""Just simple """
Params=FOLDER

[Actions\Action3\Params]
_rt_variables_produced=STRING|"json:TaskResult.result3"
expand=STRING|"0"
linecount=STRING|"2"
varname=STRING|"json:TaskResult.result3"
varvalue=STRING|"Just simple "
varvalue00000001=STRING|"multiline text"
The main task that starts the Multy-result task and gets the result of the task:

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task12"
Hide=INTEGER|0
ID=INTEGER|364797586
LogOnAsUser=INTEGER|1
Name=STRING|"Main task"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER

[Actions\Action1]
ActionID=STRING|"A_TASKS_START"
Enabled=INTEGER|-1
Name=STRING|"Start Task: Multy-result task"
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"res"
abort=STRING|"0"
namevar=STRING|"res"
parametercount=STRING|"0"
required=STRING|"0"
saveresult=STRING|"1"
taskextname=STRING|"Task2"
taskid=STRING|"$4656B715"
taskmode=STRING|"0"
wait=STRING|"1"

[Actions\Action2]
ActionID=STRING|"A_DIALOG_SHOWTEXT"
Enabled=INTEGER|-1
Name=STRING|"Show Text"
Params=FOLDER

[Actions\Action2\Params]
autoclose=STRING|"0"
fromfile=STRING|"1"
line00000000=STRING|"Raw result:"
line00000001=STRING|"{res}"
line00000002=STRING|"*****"
line00000003=STRING|"Result 1: {json:res.result1}"
line00000004=STRING|"Result 2: {json:res.result2}"
line00000005=STRING|"Result 3: {json:res.result3}"
linecount=STRING|"6"
timeout=STRING|"10"
title=STRING|"Results"
Oleg Yershov
Lana_K
Posts: 52
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Re: Returns of a task started from within another task

Post by Lana_K »

Thank you, I'll try your suggestion and codes.
photoevents
Posts: 45
Joined: Mon Jan 01, 2024 11:21 am
Contact:

Re: Returns of a task started from within another task

Post by photoevents »

Hey hey,

What is your experience using tasks versus functions ?
Is it as convenient?
I'm just wondering as these tasks are actually external to the main task.
How do you organise this if using many tasks/functions ?

Thank you for your input,
Christian
Lana_K
Posts: 52
Joined: Tue Jul 14, 2020 7:43 pm
Location: Bristol, UK

Re: Returns of a task started from within another task

Post by Lana_K »

Hi Christian,
Sorry for late reply - did not see your questions.

My problem with writing functions within Robotask tasks is that I am not very good at programming in programming languages. I learned a few, but because I never have time to practise and use my knowledge, when I eventually need it I am unable to write a decent script. Writing Robotask tasks is much simpler and quicker for me.
Sometimes I write big and complex tasks with repeating blocks of steps in them. And rather than copying and pasting same blocks, I make an external task of them and run from within the main task.
Yes, it is very convenient. It increases the readability of the main task and makes updating and troubleshooting it easier.
Way of organizing the tasks depends on the use. I have folders in my Robotask and put external functional tasks either in one dedicated folder if there are many of them, or in the same folder with the main task when this way is more convenient. It does not make much difference to functionality because the task can be called from any location, just convenience while maintaining big number of tasks.

Hope this helps.

Sincerely,
Lana
Post Reply