Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - samppaa

Pages: [1]
1
Network / Client server system help
« on: July 18, 2010, 03:21:43 pm »
So I got a system where the clients send their position to the server when they move and the server adds them to vector and should deliver the vector of all players back to player. So the sending system is ready, but how do I actually return the vector should I use udp sockets to send it back or something? Thanks in advance

2
Network / sf:Packet << operator overload problem
« on: July 02, 2010, 02:29:12 am »
I am in position now where I have to return the player vector to client so it can draw other people so would I need to do a new thread again dedicated to receive information from server? Any ways? Sorry for double post as I am posting from my iphone editin doesn't work. Thanks in advance.

3
Network / sf:Packet << operator overload problem
« on: July 01, 2010, 08:23:43 pm »
I actually tried with a one thread and it works pretty well, but I guess I still try non-blocking sockets, but does it make lag if it constantly checks where the other players are, it's the only solution I can make up.. Hope it doesn't lag.

This is what I got now:

player.h
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>

class player
{

private:
std::string charName;
float health;
float mana;
float X;
float Y;
int level;
float expirience;
bool isAlive;
sf::Sprite model;
const sf::Input & keyboard;
sf::RenderWindow & window1;
void update();

public:
player(sf::RenderWindow & window);
sf::Sprite & getModel();
void move();
float & getX();
float & getY();
float & getHealth();
float & getMana();
bool & getStatus();
int & getLevel();
float & getExpirience();
std::string & getName();

friend sf::Packet & operator << (sf::Packet & Packet, player& p);
friend sf::Packet & operator >> (sf::Packet & Packet, player& p);
};

sf::Packet & operator << (sf::Packet & Packet, player& p);
sf::Packet & operator >> (sf::Packet & Packet, player& p);


player.cpp
Code: [Select]

#include "player.h"


player::player(sf::RenderWindow & window):
health(100), mana(100), X(20), Y(20), level(1), expirience(0), isAlive(true), keyboard(window.GetInput()), window1(window), charName("Character")
{
model.SetColor(sf::Color(0, 123, 123, 128));
model.SetPosition(X,Y);
model.SetScaleX(40.f);
model.SetScaleY(40.f);
}

sf::Sprite & player::getModel()
{
return model;
}

void player::update()
{
model.SetPosition(X,Y);
window1.Draw(model);
}

void player::move()
{
if (keyboard.IsKeyDown(sf::Key::Down))
Y += 0.1f;

else if(keyboard.IsKeyDown(sf::Key::Up))
Y -= 0.1f;

else if(keyboard.IsKeyDown(sf::Key::Right))
X += 0.1f;

else if(keyboard.IsKeyDown(sf::Key::Left))
X -= 0.1f;

this->update();
}

float & player::getExpirience()
{
return expirience;
}

float & player::getX()
{
return X;
}

float & player::getY()
{
return Y;
}

float & player::getHealth()
{
return health;
}

float & player::getMana()
{
return mana;
}

bool & player::getStatus()
{
return isAlive;
}

int & player::getLevel()
{
return level;
}

std::string & player::getName()
{
return charName;
}


// Pakettien omat ylikuormitetut operaattorit


sf::Packet & operator << (sf::Packet & Packet, player& p)
{
return Packet << p.X << p.Y;
//return Packet << p.getExpirience() << p.getExpirience() << p.getLevel() << p.getMana() /*p.getModel()*/ << p.getStatus() << p.getX() << p.getY();
}

sf::Packet & operator >> (sf::Packet & Packet, player& p)
{
return Packet >> p.X >> p.Y;
}



client main.cpp
Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include "playerHandler.h"


int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Samulin RPG");
std::cout << "Opening the main window for game! \n";
player p(App);
playerHandler handler(p);
handler.Launch();



    // Start game loop
    while (&App)
    {

        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

p.move();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}



client side playerHandler.h
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <vector>
#include "player.h"

class playerHandler : public sf::Thread
{
private:
std::vector<player> allPlayers;
player & myPlayer;

virtual void Run();


public:
playerHandler(player & p);
void updateMyPlayer();
};


client side playerHandler.cpp
Code: [Select]

#include "playerHandler.h"

playerHandler::playerHandler(player & p)
:myPlayer(p)
{

}

void playerHandler::updateMyPlayer()
{
sf::IPAddress serverAddress("127.0.0.1");

sf::SocketTCP socket;

socket.Connect(1000, serverAddress);

bool connected = true; // Testing only, will add checkings later..

while(connected)
{
sf::Packet packet;
packet << myPlayer;
connected = (socket.Send(packet) == sf::Socket::Done);
}

socket.Close();
}

void playerHandler::Run()
{
this->updateMyPlayer();
}



serverside playerHandler.h
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include <vector>
#include "../RPG/player.h"

class playerHandler
{
private:
sf::SelectorTCP selector;
std::vector<player> players;

public:
playerHandler();
void checkForConnections();

};


playerhandler.cpp serverside
Code: [Select]

#include "playerHandler.h"

playerHandler::playerHandler()
{
}

void playerHandler::checkForConnections()
{
std::cout << "Checking for connections! \n";
sf::SelectorTCP selector;

sf::SocketTCP listener;

if(!listener.Listen(1000))
{
std::cout << "Can't listen the port! \n";
}

selector.Add(listener);

while(true)
{
unsigned int NbSockets = selector.Wait();

for(unsigned int i = 0; i < NbSockets; i++)
{
sf::SocketTCP socket = selector.GetSocketReady(i);

if (socket == listener)
{
sf::IPAddress address;
sf::SocketTCP client;
listener.Accept(client, &address);
std::cout << "Client connected ! (" << address << ")" << std::endl;

selector.Add(client);
}
else
{
sf::Packet packet;
if(socket.Receive(packet) == sf::Socket::Done)
{
float X;
float Y;
packet >> X >> Y;
std::cout << "X = " << X << " Y = " << Y << "\n";
}
else
{
selector.Remove(socket);
}
}
}
}
}






server main loop
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include "playerHandler.h"


int main()
{
playerHandler handler;
int i = 0;
std::cout << "Server started! \n";
handler.checkForConnections();

std::cin.get();
}


Currently it only receives and sends socket but doesn't sort the players in vector, I guess I'll try to do it today. This is really nice learning btw. Thanks!

4
Network / sf:Packet << operator overload problem
« on: July 01, 2010, 07:44:29 pm »
Ok I solved it, thanks, but I got another problem, in the game loop my client has to get all the other players info from the server and draw them so they are in sync, but when it's doing it I can't move my char or anything.. Should I like put the character retriever thing into different thread or something.. I am so new to these things

5
Network / sf:Packet << operator overload problem
« on: July 01, 2010, 05:00:40 pm »
I love this forum! Thanks so much now I got only one error which is related to sf::Packet & operator >> (sf::Packet & Packet, player& p):

It says
Code: [Select]

1>------ Build started: Project: Server, Configuration: Release Win32 ------
2>------ Build started: Project: RPG, Configuration: Release Win32 ------
1>Compiling...
2>Compiling...
2>playerHandler.cpp
1>playerHandler.cpp
1>main.cpp
2>player.cpp
2>.\player.cpp(97) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
2>        c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33): or       'sf::Packet &operator >>(sf::Packet &,player &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
2>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
2>        while trying to match the argument list '(sf::Packet, float)'
2>main.cpp
1>Linking...
2>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\Release\BuildLog.htm"
2>RPG - 1 error(s), 0 warning(s)
1>Generating code
1>Finished generating code
1>Embedding manifest...
1>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\Server\Release\BuildLog.htm"
1>Server - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The other overloaded function works fine, but the last one seems to make problems :S

Here is the code
player.h
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>

class player
{
private:
std::string charName;
float health;
float mana;
float X;
float Y;
int level;
float expirience;
bool isAlive;
sf::Sprite model;
const sf::Input & keyboard;
sf::RenderWindow & window1;
void update();

public:
player(sf::RenderWindow & window);
sf::Sprite getModel();
void move();
float getX();
float getY();
float getHealth();
float getMana();
bool getStatus();
int getLevel();
float getExpirience();
};

sf::Packet & operator << (sf::Packet & Packet, player& p);
sf::Packet & operator >> (sf::Packet & Packet, player& p);


player.cpp
Code: [Select]

#include "player.h"


player::player(sf::RenderWindow & window):
health(100), mana(100), X(20), Y(20), level(1), expirience(0), isAlive(true), keyboard(window.GetInput()), window1(window), charName("Character")
{
model.SetColor(sf::Color(0, 123, 123, 128));
model.SetPosition(X,Y);
model.SetScaleX(40.f);
model.SetScaleY(40.f);
}

sf::Sprite player::getModel()
{
return model;
}

void player::update()
{
model.SetPosition(X,Y);
window1.Draw(model);
}

void player::move()
{
if (keyboard.IsKeyDown(sf::Key::Down))
{
Y += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Up))
{
Y -= 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Right))
{
X += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Left))
{
X -= 0.1f;
this->update();
}
}

float player::getExpirience()
{
return expirience;
}

float player::getX()
{
return X;
}

float player::getY()
{
return Y;
}

float player::getHealth()
{
return health;
}

float player::getMana()
{
return mana;
}

bool player::getStatus()
{
return isAlive;
}

int player::getLevel()
{
return level;
}


// Pakettien omat ylikuormitetut operaattorit

sf::Packet & operator << (sf::Packet & Packet, player& p)
{
return Packet << p.getHealth();
//return Packet << p.getExpirience() << p.getExpirience() << p.getLevel() << p.getMana() /*p.getModel()*/ << p.getStatus() << p.getX() << p.getY();
}

sf::Packet & operator >> (sf::Packet & Packet, player& p)
{
return Packet >> p.getHealth();
}


6
Network / sf:Packet << operator overload problem
« on: July 01, 2010, 04:27:43 pm »
I declared them, but now I get this:
Code: [Select]

1>------ Build started: Project: RPG, Configuration: Release Win32 ------
1>Compiling...
2>------ Build started: Project: Server, Configuration: Release Win32 ------
2>Compiling...
2>playerHandler.cpp
1>playerHandler.cpp
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(32) : error C2804: binary 'operator <<' has too many parameters
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33) : error C2804: binary 'operator >>' has too many parameters
1>.\playerHandler.cpp(16) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\playerHandler.cpp(14): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>player.cpp
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(32) : error C2804: binary 'operator <<' has too many parameters
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33) : error C2804: binary 'operator >>' has too many parameters
1>.\player.cpp(97) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\player.cpp(95): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>main.cpp
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(32) : error C2804: binary 'operator <<' has too many parameters
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(33) : error C2804: binary 'operator >>' has too many parameters
2>main.cpp
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(32) : error C2804: binary 'operator <<' has too many parameters
1>c:\users\samuli\documents\visual studio 2008\projects\rpg\rpg\player.h(33) : error C2804: binary 'operator >>' has too many parameters
1>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\Release\BuildLog.htm"
1>RPG - 8 error(s), 0 warning(s)
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(32) : error C2804: binary 'operator <<' has too many parameters
2>c:\users\samuli\documents\visual studio 2008\projects\rpg\server\../RPG/player.h(33) : error C2804: binary 'operator >>' has too many parameters
2>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\Server\Release\BuildLog.htm"
2>Server - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 2 failed, 0 up-to-date, 0 skipped ==========


I have struggled with this some time now, doesn't make sense for me :(

7
Network / sf:Packet << operator overload problem
« on: July 01, 2010, 01:23:59 pm »
Hey, I was reading a tutorial on SFML site what says you can customize that packet thing to handle your own classes. I did add the operators there, I have defined those operators inside my player class and it still gives error?

Code is here
main.cpp
Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include "player.h"

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Samulin RPG");
std::cout << "Opening the main window for game! \n";
player p(App);


    // Start game loop
    while (&App)
    {

        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear();

p.move();

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


player.h
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>

class player
{
private:
std::string charName;
float health;
float mana;
float X;
float Y;
int level;
float expirience;
bool isAlive;
sf::Sprite model;
const sf::Input & keyboard;
sf::RenderWindow & window1;
void update();

public:
player(sf::RenderWindow & window);
sf::Sprite getModel();
void move();
float getX();
float getY();
float getHealth();
float getMana();
bool getStatus();
int getLevel();
float getExpirience();
};


player.cpp
Code: [Select]

#include "player.h"


player::player(sf::RenderWindow & window):
health(100), mana(100), X(20), Y(20), level(1), expirience(0), isAlive(true), keyboard(window.GetInput()), window1(window), charName("Character")
{
model.SetColor(sf::Color(0, 123, 123, 128));
model.SetPosition(X,Y);
model.SetScaleX(40.f);
model.SetScaleY(40.f);
}

sf::Sprite player::getModel()
{
return model;
}

void player::update()
{
model.SetPosition(X,Y);
window1.Draw(model);
}

void player::move()
{
if (keyboard.IsKeyDown(sf::Key::Down))
{
Y += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Up))
{
Y -= 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Right))
{
X += 0.1f;
this->update();
}

else if(keyboard.IsKeyDown(sf::Key::Left))
{
X -= 0.1f;
this->update();
}
}

float player::getExpirience()
{
return expirience;
}

float player::getX()
{
return X;
}

float player::getY()
{
return Y;
}

float player::getHealth()
{
return health;
}

float player::getMana()
{
return mana;
}

bool player::getStatus()
{
return isAlive;
}

int player::getLevel()
{
return level;
}


// Pakettien omat ylikuormitetut operaattorit

sf::Packet & operator << (sf::Packet & Packet, player& p)
{
return Packet << p.getHealth();
//return Packet << p.getExpirience() << p.getExpirience() << p.getLevel() << p.getMana() /*p.getModel()*/ << p.getStatus() << p.getX() << p.getY();
}

sf::Packet & operator >> (sf::Packet & Packet, player& p)
{
return Packet >> p.getHealth();
//return Packet >> p.getExpirience() >> p.getExpirience() >> p.getLevel() >> p.getMana() /*p.getModel()*/ >> p.getStatus() >> p.getX() >> p.getY();
}



playerHandler.h
Code: [Select]

#include <iostream>
#include <SFML/Network.hpp>
#include <vector>
#include "player.h"

class playerHandler
{
private:
std::vector<player> allPlayers;

public:
playerHandler();
void updateMyPlayer(player Player1);
};


playerHandler.cpp
Code: [Select]

#include "playerHandler.h"

playerHandler::playerHandler()
{

}



void playerHandler::updateMyPlayer(player Player1)
{
sf::IPAddress serverAddress = sf::IPAddress::LocalHost;

sf::SocketTCP socket;

bool connected = true; // Testing only, will add checkings later..

while(connected)
{
sf::Packet packet;
packet << Player1;
connected = (socket.Send(packet) == sf::Socket::Done);
}

socket.Close();
}



The error I get is
Code: [Select]

1>------ Build started: Project: RPG, Configuration: Release Win32 ------
1>Compiling...
1>playerHandler.cpp
1>.\playerHandler.cpp(21) : error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(226): could be 'std::ostream &sf::operator <<(std::ostream &,const sf::IPAddress &)' [found using argument-dependent lookup]
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(128): or       'sf::Packet &sf::Packet::operator <<(bool)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(129): or       'sf::Packet &sf::Packet::operator <<(sf::Int8)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(130): or       'sf::Packet &sf::Packet::operator <<(sf::Uint8)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(131): or       'sf::Packet &sf::Packet::operator <<(sf::Int16)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(132): or       'sf::Packet &sf::Packet::operator <<(sf::Uint16)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(133): or       'sf::Packet &sf::Packet::operator <<(sf::Int32)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(134): or       'sf::Packet &sf::Packet::operator <<(sf::Uint32)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(135): or       'sf::Packet &sf::Packet::operator <<(float)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(136): or       'sf::Packet &sf::Packet::operator <<(double)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(137): or       'sf::Packet &sf::Packet::operator <<(const char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(138): or       'sf::Packet &sf::Packet::operator <<(const std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(139): or       'sf::Packet &sf::Packet::operator <<(const wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(140): or       'sf::Packet &sf::Packet::operator <<(const std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, player)'
1>player.cpp
1>.\player.cpp(97) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\player.cpp(95): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>main.cpp
1>.\main.cpp(46) : error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'sf::Packet' (or there is no acceptable conversion)
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/IPAddress.hpp(215): could be 'std::istream &sf::operator >>(std::istream &,sf::IPAddress &)' [found using argument-dependent lookup]
1>        .\main.cpp(44): or       'sf::Packet &operator >>(sf::Packet &,player &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(110): or       'sf::Packet &sf::Packet::operator >>(bool &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(111): or       'sf::Packet &sf::Packet::operator >>(sf::Int8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(112): or       'sf::Packet &sf::Packet::operator >>(sf::Uint8 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(113): or       'sf::Packet &sf::Packet::operator >>(sf::Int16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(114): or       'sf::Packet &sf::Packet::operator >>(sf::Uint16 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(115): or       'sf::Packet &sf::Packet::operator >>(sf::Int32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(116): or       'sf::Packet &sf::Packet::operator >>(sf::Uint32 &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(117): or       'sf::Packet &sf::Packet::operator >>(float &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(118): or       'sf::Packet &sf::Packet::operator >>(double &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(119): or       'sf::Packet &sf::Packet::operator >>(char *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(120): or       'sf::Packet &sf::Packet::operator >>(std::string &)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(121): or       'sf::Packet &sf::Packet::operator >>(wchar_t *)'
1>        C:\Users\Samuli\Desktop\SFML\SFML-1.6\include\SFML/Network/Packet.hpp(122): or       'sf::Packet &sf::Packet::operator >>(std::wstring &)'
1>        while trying to match the argument list '(sf::Packet, float)'
1>Build log was saved at "file://c:\Users\Samuli\Documents\Visual Studio 2008\Projects\RPG\RPG\Release\BuildLog.htm"
1>RPG - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========


So where should I actually put those operator overloads? As I put them inside my class and it doesn't seem to work..

8
Network / Sending own object from client to server
« on: June 29, 2010, 12:13:49 pm »
Sorry meant to put player test; but anyways is there tutorial for making it a packet?

9
Network / Sending own object from client to server
« on: June 29, 2010, 01:48:27 am »
As I was reading the networking tutorial, here we send text to server

Code: [Select]
// Create the UDP socket
sf::SocketUDP Socket;

// Create bytes to send
char Buffer[] = "Hi guys !";

// Send data to "192.168.0.2" on port 4567
if (Socket.Send(Buffer, sizeof(Buffer), "192.168.0.2", 4567) != sf::Socket::Done)
{
    // Error...
}

So could I send object of my own to there like

// Create the UDP socket
sf::SocketUDP Socket;

// Create bytes to send
player * test;

// Send data to "192.168.0.2" on port 4567
if (Socket.Send(test, sizeof(Buffer), "192.168.0.2", 4567) != sf::Socket::Done)
{
    // Error...
}

So could I do like that?

Pages: [1]