View Revision: Basic C plus plus guide
Ok so i have seen many people which do not understand this so i decided to write a small guide. Im writing a guide about a few signs which can be very important in c++ [On EOserv] The signs i will explain are:
- The == Sign
- The = Sign
- The std::string for a config?
- The int for a config?
- The brackets?
- the ; Break line
The == Sign?
== Is a basic sign which is very important. The == Checks a function which is being used alot. A small example of the "=="
if(this->player->character->clas == 1) { this->player->character->str += 5; }
As you see i used the == as a check. The == in this line will check if a player is class 1. (Peasant) If that is true than it will continue and read the next line. So if the check is correct That means that you are class peasant than you will get 5 strength. This can be used everywhere. But dont get confused.. if(this->player->character->clas = 1){this->player->character->str +=5:} Look at the second line. That is NOT the same as == It will give you errors. The == CHECKS something but the = does NOT.
The = Sign?
The = Sign is different than the == Sign. The = sign The "=" Makes something come true like a wish =P I will give you an example:
if (this->player->character->usage < 1) { this->player->character->clas = 1; }
This is what happens: IF the player's usage is less than 1 than it will make the player's class 1. So the = basicly makes something. I can use it in all kind of ways. So this will make clear that the = can NEVER be used as check but it makes something true.
Some examples:
->home = "HomeTown"; ->clas = 1; ->title = "The great hollow"; ->name = "Helzfire";
std::string for a config?
This might be very confusing for the most people but it is alot easier than you think. This can be very important on EOserv. Imagine that you are coding for an Endless Online server and the owner asks you: Can you makeaserver message for me? Being read from a config. Than you would say alright i will do that. But than is the question: How? A string saves a certain amount of text. Sounds easy right? For example:
Wesley says: Hello. That will be saved in a string. But how will you use that in a config? Here we go:
this->player->character->ServerMsg(static_cast<std::string>(this->server->world->message_config["ServerMsg"]));
This is a ServerMsg being read from the message_config. It will work out like this:
EOserv/Data/Message.ini ServerMsg = Hello world!
As you see there is a <std::string> after the static_cast. The std::string will make it read an amount of text. Which is NOT being used for ints (Numbers) Before i knew this i always failed in making configs... So it is pretty easy you just have to remind: If you makeaconfig for like a serverMsg or a #rules command or w/e use <std::string> and not: <int>
The int for a config?
The int is used for numbers. You could say: ID's.. For example you have got a config: (Of course this is just an example) The itemID 1 and the Amount do have ints behind it. So this means that it works for numbers. the int can NEVER be used for messages. Nor can you use std::string for this.
- Gambling ##
ItemID = 1 Amount = 100
Here is a code that requires an int:
int Reward; Reward = static_cast<int>(this->server->world->test_config[util::to_string(i+1) + ".Reward"]);
The <int> Will let you know that you must use numbers and not a piece of text. You can say this:
Reward = 5412328508984
If i would make it:
Static_cast<std::string>
It would be:
Reward = Hello my name is hollow
So the int is only being used for numbers in a config. Here is a good example.
Imagine that this is my cook system:
if(this->player->character->HasItem(5))
{
this->player->character->AddItem(7, 1);
PacketBuilder builder(PACKET_ITEM, PACKET_GET);
builder.AddShort(0);
builder.AddShort(7);
builder.AddThree(1);
builder.AddChar(this->player->character->weight);
builder.AddChar(this->player->character->maxweight);
this->player->client->SendBuilder(builder);
}
If you have ID 5 it will cook ID 7 with an amount of 1. But how can we turn it into a config? Well that is not so hard :D! First we look at the "HasItem" As you see it does have ID 5 well why dont we call it.... CookID? So it will look like this:
if(this->player->character->HasItem(CookID))
if(this->player->character->HasItem(CookID))So now we addacookLocation. But We have 2 make it read CookID. So we add above the location an int called: CookID. Makes sense? I have added: int CookID; IF you want to add more ints you dont have 2 make: int CookID; intAmount; int Reward; int Reward;</pre> all the time you can just make: int CookID, Amount, Reward; Simple as hell:D Now we will use the <int> First we add what we want to read the int from so we just add: CookID = Than ofcourse thestatic_cast<int> Because it will read an amount ofnumbers. And than we will add the config itself. Now it is time for: [util::to_string(i+1) + And of course we add the name how we will call it. But how will we do that? Easy.. You just add 2 brackets [ and ] and add " and " so it will looklike [" "] Now we add the name between the 2 brackets so itwill be: ["CookID"] BUT because their is already a [ we just add .Name"] BUT because it says (i+1) It means that it will be 1name 2name so we add a . so it will look like: 1.name [1.CookID] And than we add the ending);
int CookID; for(int i = 0 ; i < static_cast<int>(this->server->world->cooking_config["CookLocations"]) ; i++)
{ CookID = CookID = static_cast<int> CookID = static_cast<int>(this->server->world->cooking_config CookID = static_cast<int>(this->server->world->cooking_config[util::to_string(i+1) + CookID = static_cast<int>(this->server->world->cooking_config[util::to_string(i+1) + ".CookID"] CookID = static_cast<int>(this->server->world->cooking_config[util::to_string(i+1) + ".CookID"]);
Than we go ahead with the AddItem part. The AddItem works like this: (1,1) So it will add ID 1 and an int (Amount) Of 1! so what will we do? We take the: this->player->character->AddItem(7,1) and makeit:this->player->character->AddItem(RewardID,Amount) So now it will add an amount and rewardID. But now we must also add something at the int so We are adding extra ints so we also add that at the int CookID; Than we also add the RewardID = static_cast ect.. and the Amount = static_cast ect.. so it will look like this:
int CookID, RewardID, Amount;
for(int i = 0 ; i < static_cast<int>(this->server->world->cooking_config["CookLocations"]) ; i++) {
CookID = static_cast<int>(this->server->world->cooking_config[util::to_string(i+1) + ".CookID"]);
RewardID = static_cast<int>(this->server->world->cooking_config[util::to_string(i+1) + ".RewardID"]);
Amount = static_cast<int>(this->server->world->cooking_config[util::to_string(i+1) + ".Amount"]);
Isn't this nice? We are almost done! Now we just make the Addshorts instead of this:
builder.AddShort(7);
builder.AddThree(1);
this:
builder.AddShort(RewardID);
builder.AddThree(Amount);
So it will look like this:
if(this->player->character->HasItem(CookID))
{
this->player->character->AddItem(RewardID, Amount);
PacketBuilder builder(PACKET_ITEM, PACKET_GET);
builder.AddShort(0);
builder.AddShort(RewardID);
builder.AddThree(Amount);
builder.AddChar(this->player->character->weight);
builder.AddChar(this->player->character->maxweight);
this->player->client->SendBuilder(builder);
}
Brackets?
I will explain how brackets work. There are 4 kind of brackets that are used. They are used in ALL codes. Here are 4 brackets:
() are for function parameters or if clauses [] are for array access {} are for code blocks <> are for bigger than smaller than and for template classes
( - )
if(this->player->character->HasItem(CookID) Now u will need an ending bracket because you cannot let it open so it will look like:
if(this->player->character->HasItem(CookID))
[ - ]
[] Can also be used for configs with pieces of text ["TestArray"] Also like the () It will need a [ before ] and a ] after [
{ - }
The { } are like a book. A page in your book. A page in your book does have all kind of pieces of text. But all pieces of text do have a different story line. 1 Piece talks about that john is walking home. And than john will go to bed. the part "John goes to bed" is different than john is walkinghome so you add a few spaces and start a new story. {} Works the same. something that has the same piece of data Goes in the bracket. For example:
if(this->player->character->name == "hollow") { code code code code code code code code code code
code code code code code code code code code code
code code code code code code code code code code
code code code code code code code code code code }
Than you start a new story and add a new bracket.
else if(this->player->character->name == "differentstory") {
code code code code code code code code code code
code code code code code code code code code code
code code code code code code code code code code
code code code code code code code code code code }
< - >
The <> Declares something. It reads something you give in a certain way, Basicly in this line:
Reward = static_cast<int>(this->server->world->cooking_config[util::to_string(i+1) + ".Reward"]);
You tell the static_cast what type you want it to read. In this case it is the int. And the <> Makes that possible.
The " ; " Break line
If you want to end a code you will have to use a break line the ';' is used for that. As you see i have added a ; to end the lines. BUT I dont want to break the first line:
if(this->player->character) {
Because i want to continue the code and make something true.
Example:
if(this->player->character) { this->player->character->admin = HGM; this->player->character->hp += 10; this->player->character->race = 3; }