How to create a class for unity that can be used for every script? - unity3d

I'm building a game in Unity which has some buyable skills in it. The bought skills are going to be stored in an external save file. Then being converted to a class called PlayerData. I want to make this class can be accessable from other scripts without typing the same class again in that script.
Is there a way to make like a global class? That can be accessed from other scripts?

I'm not sure exactly what you mean by
without typing the same class again in that script.
But if you mean you can't access the class in other scripts, maybe you defined the class in another class?
Or do you mean you can't access methods/variables directly without stating "of which instance of the class", in that case, you can use static class:
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

Related

Unity C# compile c# static class script from file

for my game I have a class which takes care of some of the functions activated by items and mission completions etc.. It's a simple static class that has a list of Actions, nothing fancy, it doesn't even inherit from Mono. The question : I want people to theoretically add functions on a web page dynamically... So first I need to figure out how to load and compile a C# class from at least a text file.
So, how do I? I tried using someone in the CSharp class, but unity said it's not recognized.
And BTW I need to be able to access the classes variables in other scripts, like Class.functions0 for example.

How to access SAPUI5 component from utility functions?

I would like to access my SAPUI5 app component using this.getOwnerComponent() from a utility function, for example formatter or a class function, this works from controllers but in a utility or formatter function placed in another folder doesn't work. I do not want to use sap.ui.getCore(), is there any other way to do it?
I ended up with below two choices:
1. Create the utility functiona as a singleton class and pass the component one time with a setComponent method.
2. Use event bus to fire events and get things done by component that can be done only within the component like accessing oData resources
You could pass the controller instance to your utility class in the constructor, and then access its owner component. You could also try injecting the component directly. Unfortunately, I am not sure if any of these is the preferred way in UI5 development.
That would couple the utility function to the component logic though, which I do not think is a good idea, you probably should pass only what is required for the utility function to do its' job.

How does importing work in swift?

If i have a function, struct, enum or class in another file/nested folder
how do i import it to use in another file?
I'm a javascript dev and i've decided to pick up swift, so i'm still trying to wrap my head around this.
NOTE: I'm not building ios/macOS apps. I'm trying to build linux cli software.
If this is the same target - you don't import anything, you should be able to use it without problems.
If you have separate targets (e.g. a Framework for a library) you should do import ModuleName to get access to the code.
By default things are internal which means you can access them in the whole module. You can't if they're private or fileprivate
write "var main = Main()" any where you like to use it, then use main.getArgs
// by writing that you define instance of the struct

Is there a shortcut to copy a method to the interface?

I rather like the "Extract Interface..." refactoring function when working on a class, but it only allows you to extract to a new interface. I was wondering if there was a similar option that allowed the user to copy a method(s) to an interface(s) that the current class already implements.
It's something that would save me a lot of time but despite thinking I may have done something like that before, I can't find any reference to such a function. Does anyone know if it even exists?
This refactoring is called "pull up method." You can move a method to a superclass, or add its declaration to any of the interfaces a class implements.

Using only part of a class from a different target

I just created a new target for the Lite version of my app. The Lite app only uses part of a base class that I have in the main app, ie it won't need to use an option that requires it to import 4 or 5 files.
My question is, from a design perspective, what is the best way to handle this so that my Lite version can only use the part of the class that it needs? Obviously, one solution is I just import those 4 unnecessary files into Lite build phase, and just use the whole class (even the parts it doesn't need). This seems inefficient though. I know I can do an ifndef to block those files from being imported if the Lite version is running, but how do I block out the code in the class from also not being picked up by the compiler?
Would a better way just be to have my Lite version create a subclass of the Base class that only uses the options it needs? But then I believe, would I still need to import those unnecessary files?
Just a bit confused about this, first time I've ever created another target that utilizes code from the main target. Any help appreciate thanks.
Put the common/lite functionality in a super class. Heavy functionality in the sub-class.
As another answer points out, you can handle this by putting the lite functionality in a subclass and the full functionality in a superclass.
Another option is to use a single class, and add the full functionality in an Objective-C category. Essentially, you can define methods in the category to supplement – or replace – methods in the base implementation.
Unlike a subclass, however, methods defined in a category can't invoke super to get the base class's functionality. super still refers to the base class's superclass, whether that's NSObject, UIDocument, or what have you – not the implementation without the category.
The advantage is that you only have one class name, so the code which instantiates your class (or classes) doesn't need to use something like #ifdef to switch classes and #includes depending on whether you're building the lite or full version.