Best way to include helper functions in all files?

Discussions about Coding and Scripting
Post Reply
User avatar
Dizzy
Experienced
Posts: 109
Joined: Tue May 21, 2013 3:57 pm
Personal rank: Oaf
Contact:

Best way to include helper functions in all files?

Post by Dizzy »

I'm wondering how other people deal with this problem...

I have a bunch of utility functions (e.g. useful string functions) which I use regularly across all my .uc files.

What's the best way to include these helper functions so that they're accessible from anywhere in a project?
Join the BunnyTrack.net Discord chat server: https://www.bunnytrack.net/discord
ShaiHulud
Adept
Posts: 459
Joined: Sat Dec 22, 2012 6:37 am

Re: Best way to include helper functions in all files?

Post by ShaiHulud »

My brain is interpreting the question in two different ways, and I'm not sure which is correct. The first interpretation - which I think is less likely to be what you're asking - is about the order of the EditPackages list in the UnealTournament.ini file. By which I mean, as long as a package is listed "earlier" in that list than any other packages in which you wish to use it, the compiler will know how to access it when you reference any of its classes.

The second interpretation is more like "how can I use the functions in a class without having to instantiate a whole bunch of objects of that class-type in all of the files in which I want to use those functions". If you can get away with it, I think the Nexgen file NexgenUtils.uc is a good pattern for this - all of its functions are declared static so you can use them without having to create an object instance first. And functions calls to that unit are scattered everywhere in the Nexgen scripts.

Otherwise, I kind of cheated and created a "proxy" class that creates objects of several other useful classes. I create one copy of this proxy class, and then pass it as a variable to every other class that I create. Those other classes store a global variable which refers to this objects, so I can access it at any time - and through it, to the useful objects it contains. It's not very OO, but it works.
nogardilaref
Masterful
Posts: 577
Joined: Tue Jun 20, 2017 1:00 pm
Personal rank: ⚋⚊⚌☰⚞⌖⚟☰⚌⚊⚋

Re: Best way to include helper functions in all files?

Post by nogardilaref »

^ It's your second interpretation. :)

As ShaiHulud said, what you want is an utility class, in other words, a class which has nothing more than static methods which you can call from anywhere directly.
Then to actually use them, you can check this: https://wiki.beyondunreal.com/Legacy:Static_Function
That is, as long they're just really "helper functions" and nothing more than that, and thus only depend exclusively on the given arguments when calling the function, and do not depend on anything else (other than perhaps default values from a configuration, at most).

You did however link to a page which also has operators, and operators are a different story altogether.
Operators must be declared in the same class you're going to use them or a parent class, this is why Object (the parent of all classes) has all the operators declared in it, so it can be used by itself and any child classes.

So this would mean that if you want to define your own operators or use some from that page, you would need to copy & paste them into the classes which are not related to each other (a class extending from Projectile and another extending from Pawn for example, one can never be a parent of the other).
This is because UnrealScript, like most OO languages out there, do not allow multiple inheritance, and it's not the only language with this kind of problems.

For these cases, languages like PHP (for example) created the concept of "traits".
Traits in PHP are just special classes in which you can define both methods and properties, and if you have a class to "use" a trait, what will actually happen is that the code of the trait is copy & pasted into that class.
In other words, is very similar to an "include" in C or C++ for example, but it's a special kind of class.

While in UnrealScript there's no such thing, if you're up to it, you (or someone else?) can perhaps build an external preprocessor (probably in another language) which is able to add a similar functionality, perhaps through a "#include" directive in the code, where by defining something like:

Code: Select all

#include ./common_code.uc
the preprocessor would include the file you're referencing into the actual targeted uc file before compiling it.

This way, should you make a change (like a bug fix) or want to add something new across these classes, it would be easy to do, as you would only need to change in a single file, as the classes which used it would have it automatically copy & pasted right before being compiled.
But only if someone builds such a tool, of course. :)
User avatar
Dizzy
Experienced
Posts: 109
Joined: Tue May 21, 2013 3:57 pm
Personal rank: Oaf
Contact:

Re: Best way to include helper functions in all files?

Post by Dizzy »

Thanks both. Yes, I wanted something like PHP's traits.

NexgenUtils.uc looks like exactly what I had in mind - thanks.

Interestingly I've seen the #include pattern before. This guy has already made his own UScript pre-processor which amongst other things seems to process #include statements:
https://github.com/joeytwiddle/code/blo ... script/jpp
Join the BunnyTrack.net Discord chat server: https://www.bunnytrack.net/discord
Post Reply