Welcome to AC Web.
Results 1 to 5 of 5
  1. #1
    Light of the Crusade

    Join Date
    Dec 2010
    Location
    www.TheForgottenCrusade.net
    Posts
    1,009

    Question How to know what to #include?


    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    I have never truly understood the Trinity documentation, and I find it super cluttered and all over the place without a linear format that readers are able to follow for coherent and concise understanding of the material it potentially offers.


    Anyway, one of the questions I currently have is how does one know what to "#include" in a newly created .cpp file?
    PS: please do not respond with "experience" as it adds no value to this post, thank you.

    Is there any other way to grab a hold and make sense out of the documentation of trinity and how it relays to C++?
    Im familiar with Cpp, however Emulation Cpp messes me up when it comes to attempting to write code or interpret already written code.

    Thank you for your time,
    Regards,
    Nerc

  2. #2
    Includes are mostly needed if you want to use enums or classes or its members from that file. So it's basically needed everywhere where you want to use stuff that's not defined in your current file.

  3. #3
    weird like that

    Join Date
    Feb 2010
    Location
    http://rochet2.github.io/
    Posts
    5,272
    Lets say I have a function in my script:

    void MyFunc()
    {
    }

    I dont need to include anything since my code does not use anything that is not a part of the base language.
    Now I make the function take in a pointer to a Player, so Player*.

    void MyFunc(Player* player)
    {
    }

    Player is not defined as a part of the language. Now I need to have something that defines what Player is.
    I can either declare that Player is a class:

    class Player;
    void MyFunc(Player* player)
    {
    }

    or I can for example include Player.h, which contains the definition of the class in its entirety (declarations methods of the class etc).

    #include "Player.h"
    void MyFunc(Player* player)
    {
    }

    If I call a method of the class in my function, I need the declaration of that method, so I need to include Player.h.

    #include "Player.h"
    void MyFunc(Player* player)
    {
    player->SetHealth(5);
    }

    If you use an enum, for example TYPE_UNIT, you have to include the file that contains that enum definition.

    So basically you include what you use. If you use Player, Creature, etc in your code, you include the file that contains the definition of that class you use.
    For standard libraries you can usually see online what you need. For example if you look up std::multimap, you can see that it requires you to use #include <map> https://en.cppreference.com/w/cpp/container/multimap

    Some headers can contain other headers. For example Player.h already includes a lot of stuff to make the player class definition work.
    You dont need to include things that are already included in the files you include, but you can.

    Visual Studio 2019 has a feature that can sometimes aid you in what to include. If you write code and you are missing Player.h for example, VS will show an error below Player. by hovering over it and clicking the light bulb you it sometimes suggests to auto include a file that contains the definition. https://i.imgur.com/CuAeAcr.png

    You can often just right click on the part that you are missing/having an error on and click "Go to definition" and VS will find it usually even if you dont have it included yet. https://i.imgur.com/YYiqGNL.png

    Header files are included based on what they contain. So whether you need to include a header file and which one you need depends on what your current code uses and what is already included. You can usually include extra headers and there is no harm other than compiling taking longer.

    I guess the tricky thing is to know what file you need to include if you are missing the definition of something. In general the convention is that Player is in Player.h, Creature is in Creature.h etc., but this is not always true. For example WorldObject and Object are both in Object.h. If the convention fails you, then you basically have to know where the definition is or search for it or let the IDE (or some extension in it?) search for you or look at existing code that uses what you need and see it's includes.
    Last edited by Rochet2; 07-09-2019 at 05:09 PM.

  4. #4
    Quote Originally Posted by Rochet2 View Post
    Lets say I have a function in my script:

    void MyFunc()
    {
    }

    I dont need to include anything since my code does not use anything that is not a part of the base language.
    Now I make the function take in a pointer to a Player, so Player*.

    void MyFunc(Player* player)
    {
    }

    Player is not defined as a part of the language. Now I need to have something that defines what Player is.
    I can either declare that Player is a class:

    class Player;
    void MyFunc(Player* player)
    {
    }

    or I can for example include Player.h, which contains the definition of the class in its entirety (declarations methods of the class etc).

    #include "Player.h"
    void MyFunc(Player* player)
    {
    }

    If I call a method of the class in my function, I need the declaration of that method, so I need to include Player.h.

    #include "Player.h"
    void MyFunc(Player* player)
    {
    player->SetHealth(5);
    }

    If you use an enum, for example TYPE_UNIT, you have to include the file that contains that enum definition.

    So basically you include what you use. If you use Player, Creature, etc in your code, you include the file that contains the definition of that class you use.
    For standard libraries you can usually see online what you need. For example if you look up std::multimap, you can see that it requires you to use #include <map> https://en.cppreference.com/w/cpp/container/multimap

    Some headers can contain other headers. For example Player.h already includes a lot of stuff to make the player class definition work.
    You dont need to include things that are already included in the files you include, but you can.

    Visual Studio 2019 has a feature that can sometimes aid you in what to include. If you write code and you are missing Player.h for example, VS will show an error below Player. by hovering over it and clicking the light bulb you it sometimes suggests to auto include a file that contains the definition. https://i.imgur.com/CuAeAcr.png

    You can often just right click on the part that you are missing/having an error on and click "Go to definition" and VS will find it usually even if you dont have it included yet. https://i.imgur.com/YYiqGNL.png

    Header files are included based on what they contain. So whether you need to include a header file and which one you need depends on what your current code uses and what is already included. You can usually include extra headers and there is no harm other than compiling taking longer.

    I guess the tricky thing is to know what file you need to include if you are missing the definition of something. In general the convention is that Player is in Player.h, Creature is in Creature.h etc., but this is not always true. For example WorldObject and Object are both in Object.h. If the convention fails you, then you basically have to know where the definition is or search for it or let the IDE (or some extension in it?) search for you or look at existing code that uses what you need and see it's includes.
    I think that what he wants it's more like a catalog or something like that with what #include have to future proposes or to know that his function is up to X. but with that tool https://i.imgur.com/YYiqGNL.png probably he won't have issue with that, only with the first point (Create creatives scripts and know what each includes have to offer )

  5. #5
    <NovusCore>

    Join Date
    Jan 2011
    Location
    https://github.com/novuscore/NovusCore
    Posts
    3,438

    REGISTER! (FREE)
    Registered members see less ads
    and also gain access to other great features.
    Sadly the code for the popular emulators are indeed very very... cluttered and outdated, I know they're making changes to modernize it more, but at the very least it'll take a few years.

    My best suggestion for you is to make use of search functionality and "Peek Definition" / "Go to definition", and slowly build up your own knowledge of where things are stored.

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •