Difference between revisions of "Self Looping Function with Delay (CMS)"

From The Official Visionaire Studio: Adventure Game Engine Wiki
(Created page with "{| class="ts" style="width:100%" |- ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By |- | Self Looping Function with Delay ||...")
(No difference)

Revision as of 14:18, 29 August 2022

Name Type By
Self Looping Function with Delay Definition AFRLme

This script allows you to iterate through a list of actions/code via a self looping function with optional delay between each iteration. Visionaire Studio 5+ is required to run this script.


Version 1: Reusable Looping Function

This version uses Lua tables/arrays for storing the lines of code you want to iterate through with each loop, as well as the delay value before the loop is recalled.

Instructions

1. Add this script into the script section of the Visionaire Studio editor & set as a defintion type script...


--[[
Self Looping Function with Delay [v3] (29/08/2022)
Written by AFRLme [Lee Clarke]
-- + --
afrlme@outlook.com | https://afrl.me
-- + --
This script is donation optional. In game credit is non-negotiable.
You are free to: ¹ use it in your game(s). ² modify the script.
Do not remove - or edit - this comment block.
--]]

function selfLoop(tbl, inc)
  tbl[inc].f() -- execute function
  -- + --
  if inc < #tbl then -- if incremental value is less than table total entries...
    inc = inc + 1 -- increment value by 1
    setDelay(tbl[inc].d, function() selfLoop(tbl, inc) end) -- reloop function after x delay
  end
end

2. To use this script you need to create an execute a script action part somewhere, or an execution type script in the script section that you can call & then you would insert some code that looks like something along the lines of this...


local t = {

{f = function() print("hello world!") end}, -- first entry, no delay value needed
{f = function() startSound("vispath:audio/example.ogg") end, d = 1000},
{f = function()
  print("multi-line functions are also valid")
  Conditions["the_end"].Value = true
end, d = 1200},
{f = function()
  if Conditions["the_end"].Value then startAction(Actions["fin"]) end
end, d = 800}

}

selfLoop(t, 1) -- init the loop & incremental value


Version 2: Disposable Looping Function

This version requires you to write a bunch of elseif queries inside of the looping function that you will use to determine which lines of code should be executed on each iteration of the loop. You will also need to use the setDelay function to update the incremental variable & recall the function after a specified delay value in milliseconds. Personally I quite like this method as it's much cleaner & easier to understand, however you can only use the function for iterating through one set of specific actions, unlike version 1, which uses Lua arrays/tables to for storing the lines of code in that you want to iterate through.

Instructions

1. Add this script into the script section of the Visionaire Studio editor & set as a defintion type script...


--[[
Self Looping Function with Delay [v1] (29/08/2022)
Written by AFRLme [Lee Clarke]
-- + --
afrlme@outlook.com | https://afrl.me
-- + --
This script is donation optional. In game credit is non-negotiable.
You are free to: ¹ use it in your game(s). ² modify the script.
Do not remove - or edit - this comment block.
--]]

local val = 1 -- init incremental value

function selfLoop()
  if val == 1 then -- check if incremental value equals 1
    print("hello world!") -- some lines of code
    setDelay(2000, function() val = val + 1; selfLoop() end) -- update incremental value & reloop
  elseif val == 2 then
    startSound("vispath:audio/example.ogg")
    setDelay(1000, function() val = val + 1; selfLoop() end)
  elseif val == 3 then
    startAction(Actions["fin"])
  end
end

2. To use this script you need to create an execute a script action part somewhere & then you would insert a line of code that looks like something along the lines of this...


selfLoop()


Resources

Name Description
self_looping_function_w_delay.zip A working example of the script in action. Visionaire Studio 5.1.9.2+ required to run the included .ved file(s).