Autor Tema: string contents or string pointer?  (Leído 844 veces)

cmunoz

  • Newbie
  • *
  • Mensajes: 42
  • Karma: 0
string contents or string pointer?
« en: Enero 08, 2016, 12:29:36 am »
Happy new year everyone!
I'm still hard at work on my game, particularly the server stuff.
To make everything user friendly, I've made it so that the user can insert their own username and preferred ip address into text files in the same directory as the dcb file. This way they can set up their own server if they want to play a friend. The dcb has no problem reading from the text files and loading the ip address or the user names, but passing the user name to the other side seems to be tricky.

Each client loads their own username file and populates the variable "string username" with their own username. What seems to be happening, is that the name of the variable "username" is being passed through the server, and not the contents of that string itself. I've found that if I override the contents of the string by manually assigning a string of text to each client's username variable, that the strings are passed through the server just fine.

In the example below, as is, user one loads its username which is Miss_One, and user two is Mr_Two. In the Miss_One client, it tells us that the opponent is also called Miss_One because it is looking at its own username variable, which means the server is telling the client to look at username, not passing a string value for p2name.

If you uncomment lines 60 and 63, regarding mario and luigi, the clients are given those names respectively. Now when you launch it, the names Mario and Luigi are passed through the server, and everything works properly. Trouble is, it's no longer using the name that the user typed in that handy text file.

So the problem must lie within the way I'm initially assigning the contents of the text file to the string username:

Código: [Seleccionar]
username=substr(fgets(fopen("./username1.txt",O_READ)),0,10);
player[BNP_ID].p1name=username;


seems to be sending the message to the other client to look at their own variable "username" instead of sending the string value of username to populate the type on the server. Like sending the name of the string, rather than the contents of the string?

I have tried passing username to a different string variable inbetween, but it produced the same results.
I also tried adding the string with another string, but also produced the same results.
I had no luck using pointers to the string variable username.

I must be missing something here. Thank you to anyone who has even read this far! I'll definitely update if I find a solution!

Código: [Seleccionar]

/*Solár Cliénte - Christof Muñoz*/
import "bennuplaynet"
import "mod_draw"
import "mod_file"
import "mod_joy"
import "mod_key"
import "mod_map"
import "mod_mouse"
import "mod_proc"
import "mod_rand"
import "mod_screen"
import "mod_sound"
import "mod_string"
import "mod_text"
import "mod_time"
import "mod_ttf"
import "mod_video"
import "mod_wm"


const
MaxConnections = 1;
end;


type _netreceive
byte working_x;
string p1name;
string p2name;
end


global
string serverip;
string username;
_netreceive player[MaxConnections];
end


process main()
begin
set_mode(320,240,16);
set_fps(48,0);
serverip=fgets(fopen("./server_ip.txt",O_READ));
BNP_MAXCONN=MaxConnections;
BNP_CONNECT(serverip,&player,sizeof(_netreceive));
BNP_PORT=2794;
BNP_TIMEOUT=10000;


if(BNP_ID==1)
username=substr(fgets(fopen("./username1.txt",O_READ)),0,10);
move_window(0,505);
set_text_color(rgb(255,180,0));
drawing_color(rgb(255,180,0));
elseif(BNP_ID==2)
username=substr(fgets(fopen("./username2.txt",O_READ)),0,10);
move_window(325,505);
set_text_color(rgb(0,153,255));
drawing_color(rgb(0,153,255));
end


////////////////////OVERRIDE USERNAME/////////////////
if(BNP_ID==1) //
//username="Mario"; //
player[BNP_ID].p1name=username; //
elseif(BNP_ID==2) //
//username="Luigi"; //
player[BNP_ID].p2name=username; //
end //
//////////////////////////////////////////////////////


repeat
delete_text(0);
delete_draw(0);


draw_rect(4,6,101,13);
draw_line(player[0].working_x*2+3,6,player[0].working_x*2+4,13);
draw_line(player[0].working_x*2+4,6,player[0].working_x*2+3,13);
draw_line(0,20,640,20);

write(0,160,10,4,"Solár! 0.2");
write(0,280,10,4,"fps: "+fps);

write(0,10,30,3,"BNP_PORT=" + (BNP_PORT));
write(0,10,40,3,"BNP_ID=" + (BNP_ID));

write(0,10,80,3,"p1name=" + (player[0].p1name));
write(0,10,90,3,"p2name=" + (player[0].p2name));
write(0,10,100,3,"username=" + (username));

write(0,mouse.x,mouse.y,4,"+");
frame;
until(key(_esc)||exit_status)
bnp_disconnect();
let_me_alone();
end


All files needed to test this out are in the zip file.
@thistof

SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12540
  • Karma: 370
Re:string contents or string pointer?
« Respuesta #1 en: Enero 11, 2016, 03:32:48 am »
string pointer isn't safe
Download Lastest BennuGD Release: http://www.bennugd.org/node/2