nordi Posted April 10, 2014 Share Posted April 10, 2014 (edited) Hey I Decided to split my code, in multiple files, in order to have a code easier to read. So I made a right click on my project, and selected "create a new class", then created a new class and added some code, Thanks. Edited April 23, 2014 by nordi Link to comment Share on other sites More sharing options...
pedro2555 Posted April 10, 2014 Share Posted April 10, 2014 You're missing the public modifier in class2 declaration. Also 'AFA.functions_from_class_2' is missing the '()' since it is a function.I think that is where your VS errors are. If your class extends (or inherits, better saying) from the Script class, then scripthook will run it as a script. If you don't extend the script class then it is a normal C# class. Going or not for the first or second approach is an implementation decision. Link to comment Share on other sites More sharing options...
nordi Posted April 10, 2014 Author Share Posted April 10, 2014 (edited) You're missing the public modifier in class2 declaration. Also 'AFA.functions_from_class_2' is missing the '()' since it is a function.I think that is where your VS errors are. If your class extends (or inherits, better saying) from the Script class, then scripthook will run it as a script. If you don't extend the script class then it is a normal C# class. Going or not for the first or second approach is an implementation decision. "Also 'AFA.functions_from_class_2' is missing the '()' since it is a function.I think that is where your VS errors are" -> no, I actually only forgot to add () here, but they are already there on my actual code file. "If your class extends (or inherits, better saying) from the Script class, then scripthook will run it as a script. If you don't extend the script class then it is a normal C# class. Going or not for the first or second approach is an implementation decision." -> When I remove the : Script, every GTA related function (Interval, Tick, etc.) on class2 gives me an error ! So what should I do, to have my code in multiple files, but having only ONE script ? Edited April 10, 2014 by nordi Link to comment Share on other sites More sharing options...
pedro2555 Posted April 10, 2014 Share Posted April 10, 2014 (edited) You're missing the public modifier in class2 declaration. Also 'AFA.functions_from_class_2' is missing the '()' since it is a function.I think that is where your VS errors are. If your class extends (or inherits, better saying) from the Script class, then scripthook will run it as a script. If you don't extend the script class then it is a normal C# class. Going or not for the first or second approach is an implementation decision. "Also 'AFA.functions_from_class_2' is missing the '()' since it is a function.I think that is where your VS errors are" -> no, I actually only forgot to add () here, but they are already there on my actual code file. "If your class extends (or inherits, better saying) from the Script class, then scripthook will run it as a script. If you don't extend the script class then it is a normal C# class. Going or not for the first or second approach is an implementation decision." -> When I remove the : Script, every GTA related function (Interval, Tick, etc.) on class2 gives me an error ! So what should I do, to have my code in multiple files, but having only ONE script ? As I said, it is an implementation decision. For taking such a decision you need to understand the implications, and for understanding the implications you need to understand the underlying implementation of the features you are working with. The following code is a very boiled down version of the implementation of the Script class in scripthook.NET: // a public class (acessible from everywhere)public abstract class Script{ // a private variable (only acessible from within this class declaration) private int pInterval; // a protected property (only acessible from a directly derived class) protected int Interval { get { return this.pInterval; } set { this.pInterval = value; } }} The first think you have to understand is class inheritance (I suggest you to take further reading on the subject, it is a simple and powerful concept essential for understanding object oriented languages, yes any of them). Being a public abstract class means two things: public: any code that references this implementation (eg.: using GTA;, at the top of your file) can access it. abstract: you can't directly instantiate this class, for instance, the following code is not possible: Script script1 = new Script(); Abstract classes must be derived by other classes, that is why you start with: class myScript : Script{} This means class myScript inherits everything from the class Script. That is why you can access the property as interval as: myScript.Interval = 100; Interval isn't declared in the myScript class declaration, but instead is inherited from the Script class. Note: You can't have a single class declaration spanning across multiple files, but you can have multiple class declarations in a single file. You have to consider what your objectives are and be fully aware of the language concepts in order to make implementation decision such as those. An example I can give you is an actual project that I'm rewriting at the moment (Ultimate Fuel Script -> you can check the actual code I'm working at https://github.com/pedro2555/gta-iv-mods/tree/mvc/ultimate-fuel-script, it's in a very early stage thought), originally the script consisted on a single class derived from the script class, but due to implementation complexity I decided to move to a different implementation (the MVC - model, view and controller). This way I can separate responsibilities, a good step towards the API implementation I want to achieve, but that's another story. Note that Model, View and Controller are actual classes in the project and all of them inherit the Script class (so I have 3 scripts running). Feel free to post some questions still, this isn't an easy topic for a beginner. Edited April 10, 2014 by pedro2555 nordi 1 Link to comment Share on other sites More sharing options...
pedro2555 Posted April 11, 2014 Share Posted April 11, 2014 Another thing you should consider is whether or not you need to instantiate class two. If the underlying objects represented by the class are many, then it makes sense to instantiate. otherwise probably it does not. Take for instance Ultimate Fuel Script shown above, there is a class called FuelStation, internally there is an instance of that class to represent each fuel station on map (and note that for memory allocations reasons that might not even be the best approach, but thats for another class ). Link to comment Share on other sites More sharing options...
nordi Posted April 11, 2014 Author Share Posted April 11, 2014 Another thing you should consider is whether or not you need to instantiate class two. If the underlying objects represented by the class are many, then it makes sense to instantiate. otherwise probably it does not. Take for instance Ultimate Fuel Script shown above, there is a class called FuelStation, internally there is an instance of that class to represent each fuel station on map (and note that for memory allocations reasons that might not even be the best approach, but thats for another class ). Thanks for the mini tutorial, I get a better picture of how it works, now (especially by looking at how your code structure looks like ) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now