again, my bad.
I missed this:
"ATTENTION: The client gets its clientid after the connection with the server is established. => the clientid is NOT immediately available after initializing the client! "
(how could I miss it? the ATTENTION is bold AND in capitals...)
Now the clientId is correct.
but the program still doesn't work.
void startupHost()
{
initEvents();
var initResult=0;var initResult2=0;
initResult = enet_init_server(port,maxPlayers,_str(""));
initResult2 = enet_init_client((userData.pstring)[2],port,_str(""));
if(initResult == 1 && initResult2 == 1)
{
logNewMessage("--Server started. Connected as Client.");
wait(15); //wait for clientid to be set
wait(-1);
my_temp[1] = enet_get_clientid();
STRING* playername = str_create("testname");
enet_set_playername(playername);
}
else
{
logNewMessage("--Could not connect. Exiting.");
sys_exit("NULL");
}
}
in main:
wait(-2);
STRING* tmpPlayerNameStr = str_create("");
var result1;
result1 = enet_get_playername(enet_get_clientid(),tmpPlayerNameStr);
if(result1 == -1)beep();
str_cpy((userData.pstring)[0],tmpPlayerNameStr);
It beepes (or beep();es). I tried, as you can see, various waiting times..
it seems to give back an empty string or something in enet_get_playername, because if I copy it into the userData text, the string in the userData text seems to be deleted, resulting in the strings [1], [2] and so on to be moved upwards.
Is there any way to say how long exactly I have to wait untill the clientId is set? I do get the error message "no clientId" if I don't wait 5 frames after the client initialization. just like the manual states. but how long do I have to wait? does it depend on the connection?
What makes it even more confusing is that this:
wait(-2);
STRING* tmpPlayerNameStr = str_create("testname");
enet_set_playername(tmpPlayerNameStr);
str_cpy(tmpPlayerNameStr," ");
var result1;
result1 = enet_get_playername(enet_get_clientid(),tmpPlayerNameStr);
if(result1 == -1)beep();
str_cpy((userData.pstring)[0],tmpPlayerNameStr);
makes it work (all called in one go, inside main, so enet_set_playername is not in the same function as enet_init_client), but when I pass an empty string (str_cpy(tmpPlayerNameStr," "); replaced by str_cpy(tmpPlayerNameStr,"");) it crashes the program.
sorry, I'm confused...O.o
another update: In the chat-demo-tutorial I found that calling wait(4); between initializing a server and a client (on the same machine) is a good thing. I didn't yet because in the manual I found this:
void start_clientserver()
{
//Client-Server Modus:
enet_init_server(2300,4,_str("")); //Initializes the server
enet_init_client(_str("127.0.0.1"),2300,_str("")); //Initializes a client
//which connects over the localhost with the server
}
without wait(4). If I DO wait(4);, then I got a "bad client id in enet_get_playername". Of course, now that I'm typing this I can't recreate that error message, it's back to as it was before: string gets deleted from the text...