MatiasMunk

Members
  • Content count

    7
  • Joined

  • Last visited

Community Reputation

114 Neutral

About MatiasMunk

  • Rank
    Newbie
  1. [Enet] Multiple Client connections

    Just so I'm not misunderstanding; So I should put create the "peer" as an array and save client information into an empty index, then consistently check up on events and add updates to the client information as they do events in the game (Which I of course will be handling server side to prevent any client hacks). Am I right or nah? ^^
  2. I'm developing a client/server for a simple game I'm making, but I have no clue on how to handle several clients? I've read a few threads here and there with the same issue, but no "solve" was posted when solved. Here we have the server #include <iostream> #include <enet/enet.h> int main(int argc, char** argv) { ENetAddress address; ENetHost* server; if (enet_initialize() != 0) { fprintf(stderr, "Failed to initialize ENet!\n"); exit(-1); } // Bind the server to default localhost // Specified host address can be specified by // enet_address_set_host(&address, "x.x.x.x"); address.host = ENET_HOST_ANY; // Bind the server to port 1234 address.port = 8484; server = enet_host_create(&address /* the address to bind the server host to */, 32 /* allow up to 32 clients and/or outgoing connections */, 2 /* allow up to 2 channels to be used, 0 and 1 */, 0 /* assume any amount of incoming bandwidth */, 0 /* assume any amount of outgoing bandwidth */); if (server == NULL) { fprintf(stderr, "An error occurred while trying to create an ENet server host.\n"); exit(EXIT_FAILURE); } else { std::cout << "MSC++Server has started" << std::endl << "Listening on IP: " << address.host << " Port: " << address.port << "." << std::endl; } ENetEvent event; while (true) { /* Wait up to 1000 milliseconds for an event. */ while (enet_host_service(server, &event, 1000) > 0) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: printf("New connection from %x:%u.\n", event.peer->address.host, event.peer->address.port); /* Store any relevant client information here. */ event.peer->data = "Client"; break; case ENET_EVENT_TYPE_RECEIVE: printf("A packet of length %u containing %s was received from %s on channel %u.\n", (unsigned int)event.packet->dataLength, event.packet->data, event.peer->data, event.channelID); /* Clean up the packet now that we're done using it. */ enet_packet_destroy(event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: printf("%s disconnected.\n", event.peer->data); /* Reset the peer's client information. */ event.peer->data = NULL; } } } atexit(enet_deinitialize); } This particular code especially case ENET_EVENT_TYPE_CONNECT: printf("New connection from %x:%u.\n", event.peer->address.host, event.peer->address.port); /* Store any relevant client information here. */ event.peer->data = "Client"; break; I have a "peer" or client if you will, with some data. This connection is a single connection that can be established to the server, meaning if I instance a new client, the first client will have it's data overwritten. How do I overcome this issue?
  3. [C++/A5] OOP Code

    Because the code is not even done Randy. I am planning to add more to it, and it will just become a mess on the long-term. Because I would like to take the time and learn OOP, as I've heard it makes life easy. Also I plan on having people helping me with code in the future, as a team.   Thank you Rip-off, I'll try and recreate what I tried before, and post it here :)
  4. Hey, how would one best go by making this code Object Oriented? I have tried a bit here and there, but I keep getting 'not defined in this scope' errors etc.. Here's my code :)   http://pastebin.com/vMHCTBu9   Any help is very much appreciated :)
  5. Classes and Objects

      So I should be creating an attackMonster class and make a function that I can call in main like attackMonster(pig) and it would attack the pig?
  6. Classes and Objects

      Oh yes, I see. I will start doing this. However, like Alvaro said "* Don't repeat yourself. There are 4 nearly-identical blocks in the code that handles attacking a monster. Those should only be one block that can work with any of the monsters. This would be easier to do if the monsters were contained in an array instead of being 4 separate variables."   How can I go by doing this?
  7. Classes and Objects

    Hello, first I would like to thank you, for your advice on the variable names and positions.   I don't really know how to work arrays, I can't seem to get it working, but perhaps you or someone on here could give me a general idea on how to place it all in one block instead of 4?   Yeah, I've heard that Includes.h is a bad idea, it was also just something I added to make it a little easier while practising ;)   About the setGold, at the time of creation I couldn't put my tongue on the word "add" for some reason ;P (I'm Danish) Changed it to addGold. Thank you;)         Thank you so much for the advice.   I would think that the function bool isAlive is really useful, but how would I go by adding it to the program? Perhaps in the monster class? Or what would be better?   Also what exactly do you mean by constructors?  
  8. Classes and Objects

    Thank you so much for the advice.   I would think that the function bool isAlive is really useful, but how would I go by adding it to the program? Perhaps in the monster class? Or what would be better?   Also what exactly do you mean by constructors?
  9. Hey, I'm all new to classes, and I've done the best I could.   How could I go by making this code better?   Main.cpp: http://pastebin.com/JX2HPXtG   itemClass.cpp: http://pastebin.com/2qZDZtit   itemClass.h: http://pastebin.com/Hhd9PziW   monsterClass.cpp: http://pastebin.com/ZVJ2KDbV   monsterClass.h: http://pastebin.com/3wbXkb61   playerClass.cpp: http://pastebin.com/cdhFhyA4   playerClass.h: http://pastebin.com/Tk1B5aJr   Any help is much appreciated !   Regards, Matias.