diff --git a/objects/obj_catspeak_examples/CleanUp_0.gml b/objects/obj_catspeak_examples/CleanUp_0.gml index c076b140..f1ebf0b9 100644 --- a/objects/obj_catspeak_examples/CleanUp_0.gml +++ b/objects/obj_catspeak_examples/CleanUp_0.gml @@ -1,3 +1,3 @@ /// @desc Clean up Catspeak sessions. -catspeak_session_destroy(jsonSession); -catspeak_session_destroy(configSession); \ No newline at end of file +catspeak_session_destroy(configSession); +catspeak_session_destroy(processingSession); \ No newline at end of file diff --git a/objects/obj_catspeak_examples/Create_0.gml b/objects/obj_catspeak_examples/Create_0.gml index 477ae794..c4024999 100644 --- a/objects/obj_catspeak_examples/Create_0.gml +++ b/objects/obj_catspeak_examples/Create_0.gml @@ -1,6 +1,8 @@ /// @desc Initialise examples. frameStartTime = 0; show_debug_overlay(true); +catspeak_set_frame_allocation(0.5); // compute for 50% of the frame if possible +catspeak_set_frame_threshold(0.75); // do not surpass 75% of the current frame // JSON session jsonObj = catspeak_ext_json_parse(@' { @@ -39,19 +41,34 @@ catspeak_session_add_function(configSession, "world", function(_value) { worldData = _value; }); catspeak_session_set_source(configSession, @' -player { - .name : "Angie" - .x : -12 - .y : 8 -} + player { + .name : "Angie" + .x : -12 + .y : 8 + } -world { - .height : 1000 - .width : 500 - .background_colour : 0x00FF00 -} + world { + .height : 1000 + .width : 500 + .background_colour : 0x00FF00 + } '); catspeak_session_create_process(configSession, function(_) { show_message("player data:\n" + string(playerData)); show_message("world data:\n" + string(worldData)); +}); +// heavy processing example +processingSession = catspeak_session_create(); +catspeak_ext_session_add_gml_operators(configSession); +catspeak_session_set_source(configSession, @' + set countdown 15000 + while countdown { + if (countdown % 5000 == 0) { + print countdown + } + set countdown : countdown - 1 + } +'); +catspeak_session_create_process(configSession, function(_) { + show_message("countdown complete!"); }); \ No newline at end of file diff --git a/scripts/scr_catspeak/scr_catspeak.gml b/scripts/scr_catspeak/scr_catspeak.gml index 9ef53576..5019e603 100644 --- a/scripts/scr_catspeak/scr_catspeak.gml +++ b/scripts/scr_catspeak/scr_catspeak.gml @@ -17,7 +17,8 @@ function __catspeak_manager() { runtimeProcessID : 0, errorScript : undefined, maxIterations : -1, - frameAllocation : 0.5 // compute for 50% of frame time + frameAllocation : 0.5, // compute for 50% of frame time + frameThreshold : 1 // do not surpass 1 frame when processing }; return catspeak; } @@ -69,7 +70,7 @@ function __catspeak_next_runtime_process() { function catspeak_update(_frame_start) { var catspeak = __catspeak_manager(); // update active processes - var frame_time = game_get_speed(gamespeed_microseconds); + var frame_time = catspeak.frameThreshold * game_get_speed(gamespeed_microseconds); var time_limit = min(_frame_start + frame_time, get_timer() + catspeak.frameAllocation * frame_time); var runtime_processes = catspeak.runtimeProcesses; @@ -128,6 +129,20 @@ function catspeak_set_max_iterations(_iteration_count) { catspeak.maxIterations = is_numeric(_iteration_count) && _iteration_count >= 0 ? _iteration_count : -1; } +/// @desc Sets the maximum percentage of a game frame to spend processing. +/// @param {real} amount The amount in the range 0-1. +function catspeak_set_frame_allocation(_amount) { + var catspeak = __catspeak_manager(); + catspeak.frameAllocation = is_numeric(_amount) ? clamp(_amount, 0, 1) : 0.5; +} + +/// @desc Sets the threshold for processing in the current frame. Processing will not continue beyond this point. +/// @param {real} amount The amount in the range 0-1. +function catspeak_set_frame_threshold(_amount) { + var catspeak = __catspeak_manager(); + catspeak.frameThreshold = is_numeric(_amount) ? clamp(_amount, 0, 1) : 1.0; +} + /// @desc Creates a new Catspeak session and returns its ID. function catspeak_session_create() { var catspeak = __catspeak_manager();