Page 1 of 1

Coding server files for games like CSS or Gmod

Posted: Sat Oct 25, 2008 9:06 pm
by NeonNinja♦
Well i was wondering if C++ can code server files for multiplayer games such as Counter Strike Source, Or Garry's mod. I was hoping when i get experienced enough i would try this (as a side hobby) and would this be included in the Sam's learn C++ in 21days book? Also would it include how to code an engine

--Again sorry for the newbish questions, im kind of excited because i am learning the basis of C++ and i'm just thinking ahead :D

Also i was hoping to know What header files were or (.h) Because i use Microsoft Visual C++ 2008 express edition :D

Thanks for your time and responses
-Neon Ninja♦

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 11:13 am
by MarauderIIC
Server files like... the part that decides what to send to a client? If so, yeah, C/++ is often used for both server-side and client-side software. Edit: If you're talking server-side mod .dll files, which is how Quake 2 does it (haven't played any counter-strike, tbh), C++ can be compiled to be a .dll file too.

As for header files:
Say that you have a class (defines an object, like a microwave)
If you have more than one .cpp file, and they all need to do various things, like one .cpp file has microwave.on and another .cpp file has microwave.explode, then they both need to know the definition of the object (what a 'microwave' is). So you put "what a microwave is" in a header file and do a #include "microwave.h" in each of your .cpp files that needs to know what a microwave is.

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 11:41 am
by NeonNinja♦
Well i mean like when you play Css you join a server, and i knew someone who used to do that and when i get good i wanted to try it, just wanted to know if it was possible. Thanks for replying :D

EDIT: im completely new to this but soon i will be good.... Hopefully with sam's help :D I didn't even know you can make more files like that and also with that microwave example all your doing is defining what the microwave is? and if i were to code server files would i have to define for every model/environment?

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 1:33 pm
by MarauderIIC
You can code a program that receives signals from counterstrike clients and sends signals back, yes. It'd be rather complex. Did I answer that question, yet?

Yeah, I'm just defining what a microwave is. No, you don't have to use objects in your programming. If you do use objects, it would be like so...

microwave.h

Code: Select all

class Microwave {
private:
    bool isOn;
    int itemsInMicrowave;
    bool isOpen;
    int timeLeft;
public:
    void Close();
    void InsertItems(int numItems); //open microwave and insert items
    void Finish();  //open microwave and remove all items
    void SetTime(int time);
    void PushStart();

    void UpdateStatus(); //call this once per second

    int GetTimeLeft();
};
microwave.cpp / defines all the functions we prototyped in microwave.h

Code: Select all

#include "microwave.h"
void Microwave::Close() {
    isOpen = false;
}

void Microwave::InsertItems(int numItems) {
    isOpen = true;
    itemsInMicrowave = numItems;
}

void Microwave::Finish() {
    isOn = false;
    itemsInMicrowave = 0;
    isOpen = true;
}

void Microwave::SetTime() {
    timeLeft = time;
}

void Microwave::PushStart() {
    if (isOpen == false)
        isOn = true;
}

void Microwave::UpdateStatus() {
   timeLeft--;
   if (timeLeft == 0)
       isOn = false;
}

int Microwave::GetTimeLeft() {
    return timeLeft;
}
main.cpp / uses a microwave / all that it and anything like this needs to know is what the parameters for the fns are, the 'linker step' will make them refer to the correct fns that are defined in microwave.cpp, so include microwave.h, not microwave.cpp

Code: Select all

#include "microwave.h"
int main() {
    Microwave microwave;
    microwave.InsertItems(5);
    microwave.Close();
    microwave.PushStart();
    while (microwave.GetTimeLeft() != 0) {
        Sleep(1000); //time in milliseconds, this is not a standard function
        microwave.UpdateStatus();
    }
    microwave.Finish();
    return 0; //returning from main means end of program
}
edit- If you did not use objects (that is, you would use the headers for function prototypes that many .cpp files would use, if you used them), this is called procedural programming... I think. If you do it this way you might as well use C instead of C++.

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 1:35 pm
by NeonNinja♦
Oh i see what your saying! I'm coding to define the server info to send to he client? as well as the client receives the server's info? Btw, Thanks alot your being very helpful :D

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 1:37 pm
by MarauderIIC
Yeah your code would receive information sent over a network (your computer will receive this information and pass it to any program listening for it), and then your code would process this information and send an appropriate response. So instead of a Microwave you might define a Packet. :)

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 1:40 pm
by NeonNinja♦
Ahh it soounds so hard, but im sure when i buy that book and start learning it will come around to being moderately hard :D Thanks again for your help :D

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 1:41 pm
by NeonNinja♦
And sorry for the double post but After i make the (main.cpp) file, would i then make a header?

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 2:29 pm
by dandymcgee
I think a CS server is a great goal, but you should probably do something a lot more simple to get the basic idea of what's going on. Then slowly build you're way up to creating your own server.

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 2:37 pm
by NeonNinja♦
hmm ok Thanks :D

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 8:09 pm
by MarauderIIC
NeonNinja♦ wrote:And sorry for the double post but After i make the (main.cpp) file, would i then make a header?
Doesn't matter what order you make the files in.

Re: Coding server files for games like CSS or Gmod

Posted: Sun Oct 26, 2008 8:19 pm
by NeonNinja♦
Oh :D Thanks for the helpful advice :mrgreen: Im on my way!