I
itameio
Guest
lets say i have 'game_end()' stored as a string in a variable, is there anyway for me to call the string from the variable and execute it as the function it is?
if my_variable == 'game_end()'
{
game_end();
}
The next question is, why do you want to store a function or piece of code as a string to execute later? There's likely a better work around then a code parser.
I'm curious if there was something else still around like execute_string(), yeah there is probably better ways of doing but I'm not even sure of what I want to do with it anymore.Wouldn't it be simpler to do something along the lines of this:
PS. I am excited as to what your answer is to @stainedofmind's question.
Wouldn't it be simpler to do something along the lines of this:
or a switch statement if "my_variable" can have different values.Code:if my_variable == 'game_end()' { game_end(); }
= https://marketplace.yoyogames.com/assets/4263/function_execute *vanishes*lets say i have 'game_end()' stored as a string in a variable, is there anyway for me to call the string from the variable and execute it as the function it is?
/// @function function_execute( name, args )
/// @description executes builtin function or user script by name
/// @param {string} name the function name
/// @param {any} args the arguments that will be forwarded to the function
/// @returns {any} the return value of the executed function
function function_execute( name, args ){
static ____builtin_functions = undefined
// this code block will only run once on first usage of function_execute
if( is_undefined( ____builtin_functions ) ){
____builtin_functions = {}
// id range for builtin scripts(functions) is 0 - 100000
for( var _i=0; _i <= 100000; _i++ ){
var _name = script_get_name( _i )
// break loop once we reach the first unused id
if( _name == "<unknown>" ) break
// skip this iteration, prevent error from trying to access an internal function
if( string_char_at( _name, 1 ) == "@" ) continue
____builtin_functions[$_name] = _i
}
}
// determine if name is a user script or a builtin function
var _ind = asset_get_type( name ) == asset_script ? asset_get_index( name ) : ____builtin_functions[$name]
if( is_undefined( _ind ) ) throw ("unknown function or script: " + name)
var _args = array_create( argument_count - 1 )
// get all provided arguments minus the first as a real array
for( var _i=1; _i < argument_count; _i++ ) _args[_i-1] = argument[_i]
return script_execute_ext( _ind, _args )
}
If I understood your problem correctly:TL;DR:I want to use string_copy in adjust_arabic_text but i can't use it in the draw event because it makes the game slower and can't call it from a variable because string_copy wouldn't work.
function string_copy_adjusted(_str, _index, _count, _reverse) {
if (!_reverse) {
return string_copy(_str, _index, _count);
} else {
var _length = string_length(_str);
return string_copy(_str, 1 + _length - _count - (_index - 1), _count);
}
}
string_copy_adjusted("Hello, world!", 1, 5, false)
will return "Hello" (like regular string_copy), while given string_copy_adjusted("Hello, world!", 1, 5, true)
it will return "world!" instead. So if you do the following in Draw event where the text is typed out:var _text = string_copy_adjusted(_str, 1, _shown_characters, global.language == "Arabic");