katsaii / catspeak-lang

A cross-platform modding language for GameMaker games.
https://www.katsaii.com/catspeak-lang/
MIT License
81 stars 4 forks source link

Dev 3.0.0 - `addGMAsset`, `addGMLFunction`, and `addGMLFunctionBySubstring` #73

Closed tabularelf closed 10 months ago

tabularelf commented 10 months ago

Took me a bit to write these up. From previous discussions on Discord, I've came up with these three small but very helpful functions.

env.addGMAsset("spr_player", "fnt_text", "obj_player", "obj_wall") 
env.addGMLFunction(lengthdir_x, lengthdir_y) 
env.addGMLFunctionBySubstring("array")

env.addGMAsset() is similar to as env.addConstant() but includes extra checks to make sure that it's a valid asset index from a name.

env.addGMLFunction() is similar to env.addFunction(), except it adds in a function index as per the name and index. And prevents methods from being added in directly. This does include built in functions, such as lengthdir_x.

env.addGMLFunctionBySubstring() is similar to env.addFunction(), but scans all user function indexes for each substring that is passed (starting from 100001, meaning that built-in functions are ignored) and adds anything that matches the substring with the first part of the name. So "scribble" would add "scribble", "scribble_* but not "__scribble" or "scr_scribble".

The name for addGMLFunctionBySubstring I'm not really settled on entirely, but I am open to suggestions on what could be better named. (alynne suggested PartialName in place of Substring).

All of these do include test cases. And for .addGMLFunctionBySubstring(), I've included scr_testing_example_functions for the demonstration of what it does/doesn't add in.

tabularelf commented 10 months ago

On one additional note, I'm not sure what would be the best approach for detecting a valid function is passed in, while also not including methods for addGMLFunction. As the others do under CATSPEAK_DEBUG_MODE. Since is_callable includes methods, and script_exists doesn't work for built in functions. I was considering passing in some kind of custom checker function for __catspeak_check_arg, but I figured I should leave that up for discussion.