Team Spartan Cookies & Milk Forums
Global Functions - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: Global Functions (/thread-2412.html)

Pages: 1 2


RE: Global Functions - Skeleton - 11-08-2017

Quote:What are the use-cases that people have for overriding default functions?

That's a good point but isn't your not only applied to use-cases files only?[/quote]


RE: Global Functions - Red The Blue Color - 11-08-2017

(11-08-2017, 05:45 PM)Person8880 Wrote: You should not have a need to ever modify the behaviour of default functions. While it is possible in Lua by default, it is generally discouraged. A developer should never have to worry that a standard function may act differently.

If you have behaviour you want to use then you create your own functions. This is still possible.

Making the environment only protect certain functions is even worse as it's even less obvious to a developer which ones they can and can't change (even though they shouldn't be changing any at all).

What are the use-cases that people have for overriding default functions?

Well a great example would probably be code that you want to apply to already made scripts for example I have a script that overrides commonly used backdoor functions and prints a trace in console so I can see if large scripts I don't feel like digging through has a backdoor anywhere. I have recreated the starfall rendering library just to see what I could do to the basic drawing functions, and I even have an idea to optimize rendering by changing global functions. Sure I probably don't need to change global functions for the idea I have but it loses the ability to be applied to any screen instantly.

I personally don't see the huge issue with overriding global functions, if the reason why it's discouraged is because developers shouldn't have to worry about the functionality changing, i usually never give out chips that would remove or have a noticable impact on default functions so i don't think a developer has to worry about it.

Overriding global functions is just something useful to have and you never know when you might need it.


RE: Global Functions - Kosmos - 11-08-2017

(11-08-2017, 05:45 PM)Person8880 Wrote: What are the use-cases that people have for overriding default functions?
The main reason to alter built-in functions is because you actually want code you didn't write to behave differently. The example I made which causes error to spit out a stack trace instead of just the message is but one, and sadly allowing that does pose the threat that the error function doesn't throw an error (which is obviously bad).
There's also the common practice of overriding the type function to add custom types. There's also some ridiculous examples like getScripts returning a number of virtual files that are being loaded from the web, the place where SF creates it's files, or (something I've been working on) loading encrypted scripts.
As for modifying default functions being discouraged, I do believe that the current 'standard' for libraries is to dump their contents into the global environment. And I did that for quite some time too.
However, I don't think a "but Lua has it" argument leads anywhere, Lua also has the debug library.


RE: Global Functions - Person8880 - 11-08-2017

Well, if you really want the ability to do this I will revert it, but I'm not adding any half measures to protect specific functions.

What I will do instead is add an enforced local variable to every script, _MAINFILE. This is something no other script can change, and will provide you with the guaranteed correct mainfile path. Beyond that you are on your own.

(11-08-2017, 06:50 PM)Red The Blue Color Wrote: I personally don't see the huge issue with overriding global functions, if the reason why it's discouraged is because developers shouldn't have to worry about the functionality changing, i usually never give out chips that would remove or have a noticable impact on default functions so i don't think a developer has to worry about it.
It's not just other people, it's yourself. It is very easy to modify something's behaviour, forget you've done it, then spend hours wondering why something isn't doing what you expect only to finally find that you changed its behaviour and introduced a bug/unexpected side effect.

You should see the standard library as a foundation and build your work on top of it, rather than trying to dig up some of the foundation and redo it, leaving parts all over the place.

(11-08-2017, 07:08 PM)Kosmos Wrote: As for modifying default functions being discouraged, I do believe that the current 'standard' for libraries is to dump their contents into the global environment. And I did that for quite some time too.
This isn't modifying default functions though, this is just adding values to the global table. Which is fine, if a little crude.


RE: Global Functions - Red The Blue Color - 11-08-2017

(11-08-2017, 07:12 PM)Person8880 Wrote: Well, if you really want the ability to do this I will revert it, but I'm not adding any half measures to protect specific functions.

What I will do instead is add an enforced local variable to every script, _MAINFILE. This is something no other script can change, and will provide you with the guaranteed correct mainfile path. Beyond that you are on your own.

(11-08-2017, 06:50 PM)Red The Blue Color Wrote: I personally don't see the huge issue with overriding global functions, if the reason why it's discouraged is because developers shouldn't have to worry about the functionality changing, i usually never give out chips that would remove or have a noticable impact on default functions so i don't think a developer has to worry about it.
It's not just other people, it's yourself. It is very easy to modify something's behaviour, forget you've done it, then spend hours wondering why something isn't doing what you expect only to finally find that you changed its behaviour and introduced a bug/unexpected side effect.

You should see the standard library as a foundation and build your work on top of it, rather than trying to dig up some of the foundation and redo it, leaving parts all over the place.

(11-08-2017, 07:08 PM)Kosmos Wrote: As for modifying default functions being discouraged, I do believe that the current 'standard' for libraries is to dump their contents into the global environment. And I did that for quite some time too.
This isn't modifying default functions though, this is just adding values to the global table. Which is fine, if a little crude.

Thanks for reverting it and sorry for wasting your time in the first place.