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

Add support for defining Dynamic Constants #92

Closed tabularelf closed 9 months ago

tabularelf commented 9 months ago

What is your feature request?

Currently there's no way to to define a constant like it were a constant, but have it at like a function. This ideally just needs to be a single getter with no way of passing in arguments (that's what functions are for!) In my own game, I'm currently exposing constants like

Catspeak.interface.exposeFunction("fps", function() {return fps;});

And in Catspeak I have to do

-- fps()
print(fps);
-- or the shorthand :
print(:fps);

I'm personally not a fan of this approach.

Please describe in detail how you expect this new feature to behave.

If we were to have Catspeak.interface.exposeDynamicConstant(name, function), we could then do

Catspeak.interface.exposeDynamicConstant("fps", function() {return fps;});

Where Catspeak.interface.exposeDynamicConstant is similar to Catspeak.interface.exposeFunction, except that it either attaches isDynamic to the method in some way (maybe via constructor so no one can fiddle with the variable directly) or via an alternative database. And then in Catspeak it's just

print(fps);

This ends up translating as if we were doing fps() within our code, without having to explicitly call it. If #89 gets added, this could be instead be a toggle for allowing dynamic constants.