Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

split classes in multiple file


nordi
 Share

Recommended Posts

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 by nordi
Link to comment
Share on other sites

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

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 by nordi
Link to comment
Share on other sites

 

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 by pedro2555
Link to comment
Share on other sites

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 :D).

Link to comment
Share on other sites

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 :D).

 

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.