Difference between revisions of "Core.asyncCall"

From GiderosMobile
(----)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
__NOTOC__
 
'''Available since:''' Gideros 2016.06<br/>
 
'''Available since:''' Gideros 2016.06<br/>
 
'''Class:''' [[Core]]<br/>
 
'''Class:''' [[Core]]<br/>
  
 
=== Description ===
 
=== Description ===
Launch function on separate thread as background task.
+
Launches a function on a separate thread as a background task.
 +
<syntaxhighlight lang="lua">
 +
Core.asyncCall(task,parameters)
 +
</syntaxhighlight>
 +
 
 +
Background threads are only executed when the main thread is not running.
 +
 
 +
=== Parameters ===
 +
'''task''': (function) function to run in background<br/>
 +
'''parameters''': (multiple) multiple parameters to pass to function '''optional'''<br/>
  
Background threads are only executed when main thread is not running.
+
----
  
<source lang="lua">
+
'''Available since:''' Gideros 2025.3<br/>
Core.asyncCall(task,parameters)
+
'''Class:''' [[Core]]<br/>
</source>
+
 
 +
=== Description ===
 +
In Gideros 2025.3, Core.asyncCall has been modified to allow disabling the autoyield function on creation (previously it was always on). To do so, it now accepts an optional boolean as first argument to tell if auto yielding should be enabled or not. If present, the function to call and its arguments are passed in parameters 2 and beyond.
 +
<syntaxhighlight lang="lua">
 +
Core.asyncCall(autoyield,task,parameters)
 +
</syntaxhighlight>
 +
 
 +
Background threads are only executed when the main thread is not running.
  
 
=== Parameters ===
 
=== Parameters ===
'''task''': (function) function to run in background <br/>
+
'''autoyield''': (boolean) enable/disable autoyield function on creation, '''default, autoYield = true'''<br/>
 +
'''task''': (function) function to run in background<br/>
 
'''parameters''': (multiple) multiple parameters to pass to function '''optional'''<br/>
 
'''parameters''': (multiple) multiple parameters to pass to function '''optional'''<br/>
  
 
----
 
----
{{Special:PrefixIndex/Core}}
+
 
<br/>
+
=== Examples ===
 +
<syntaxhighlight lang="lua">
 +
local txt1_noads = TextField.new(nil, "no ads")
 +
local txt2_plusincome = TextField.new(nil, "plus income")
 +
local btn_OK = TextField.new(nil, "OK")
 +
-- position
 +
txt1_noads:setPosition(64, 64*1)
 +
txt2_plusincome:setPosition(64, 64*3)
 +
btn_OK:setPosition(64, 64*5)
 +
-- scale
 +
local FontScale = 4
 +
local tempFontScale = 8
 +
txt1_noads:setScale(FontScale)
 +
txt2_plusincome:setScale(FontScale)
 +
btn_OK:setScale(FontScale)
 +
-- order
 +
stage:addChild(txt1_noads)
 +
stage:addChild(txt2_plusincome)
 +
stage:addChild(btn_OK)
 +
 
 +
local function scaleTextsAndBtn()
 +
Core.yield(2)
 +
txt1_noads:setScale(tempFontScale)
 +
Core.yield(1)
 +
txt1_noads:setScale(FontScale)
 +
 
 +
Core.yield(1)
 +
txt2_plusincome:setScale(tempFontScale)
 +
Core.yield(1)
 +
txt2_plusincome:setScale(FontScale)
 +
 
 +
Core.yield(1)
 +
btn_OK:setScale(tempFontScale)
 +
Core.yield(0.5)
 +
btn_OK:setScale(FontScale)
 +
end
 +
 +
Core.asyncCall(scaleTextsAndBtn)
 +
</syntaxhighlight>
 +
 
 +
'''asyncCall used in a Class and with parameters'''
 +
<syntaxhighlight lang="lua">
 +
if self.imgui:button("VALIDATE") then
 +
-- self:gotoScene("levelX")
 +
Core.asyncCall(LevelX.gotoScene, self, "levelX") -- or self.gotoScene, ...
 +
end
 +
...
 +
 
 +
-- change scene
 +
function LevelX:gotoScene(xscene)
 +
scenemanager:changeScene(
 +
xscene, 1,
 +
transitions[math.random(1, #transitions)],
 +
easings[math.random(1, #easings)]
 +
)
 +
end
 +
</syntaxhighlight>
 +
 
 +
=== See also ===
 +
'''[[Core.yield]]'''
 +
 
 +
{{Core}}

Latest revision as of 00:09, 26 March 2025

Available since: Gideros 2016.06
Class: Core

Description

Launches a function on a separate thread as a background task.

Core.asyncCall(task,parameters)

Background threads are only executed when the main thread is not running.

Parameters

task: (function) function to run in background
parameters: (multiple) multiple parameters to pass to function optional


Available since: Gideros 2025.3
Class: Core

Description

In Gideros 2025.3, Core.asyncCall has been modified to allow disabling the autoyield function on creation (previously it was always on). To do so, it now accepts an optional boolean as first argument to tell if auto yielding should be enabled or not. If present, the function to call and its arguments are passed in parameters 2 and beyond.

Core.asyncCall(autoyield,task,parameters)

Background threads are only executed when the main thread is not running.

Parameters

autoyield: (boolean) enable/disable autoyield function on creation, default, autoYield = true
task: (function) function to run in background
parameters: (multiple) multiple parameters to pass to function optional


Examples

local txt1_noads = TextField.new(nil, "no ads")
local txt2_plusincome = TextField.new(nil, "plus income")
local btn_OK = TextField.new(nil, "OK")
-- position
txt1_noads:setPosition(64, 64*1)
txt2_plusincome:setPosition(64, 64*3)
btn_OK:setPosition(64, 64*5)
-- scale
local FontScale = 4
local tempFontScale = 8
txt1_noads:setScale(FontScale)
txt2_plusincome:setScale(FontScale)
btn_OK:setScale(FontScale)
-- order
stage:addChild(txt1_noads)
stage:addChild(txt2_plusincome)
stage:addChild(btn_OK)

local function scaleTextsAndBtn()
	Core.yield(2)
	txt1_noads:setScale(tempFontScale)
	Core.yield(1)
	txt1_noads:setScale(FontScale)

	Core.yield(1)
	txt2_plusincome:setScale(tempFontScale)
	Core.yield(1)
	txt2_plusincome:setScale(FontScale)

	Core.yield(1)
	btn_OK:setScale(tempFontScale)
	Core.yield(0.5)
	btn_OK:setScale(FontScale)
end
 
Core.asyncCall(scaleTextsAndBtn)

asyncCall used in a Class and with parameters

	if self.imgui:button("VALIDATE") then
--		self:gotoScene("levelX")
		Core.asyncCall(LevelX.gotoScene, self, "levelX") -- or self.gotoScene, ...
	end
...

-- change scene
function LevelX:gotoScene(xscene)
	scenemanager:changeScene(
		xscene, 1,
		transitions[math.random(1, #transitions)],
		easings[math.random(1, #easings)]
	)
end

See also

Core.yield