sending array

Posted By: Reconnoiter

sending array - 03/22/14 15:01

Hey,

it is me spamming the multiplayer boards again, sry grin. Anyway, my game instantly crashes when I send an array (where I save a handle of an entity in) from the server to the client, in the on_server_event. If I just send a var with the handle, instead of an array, I get no crash.

Is it not possible sending arrays with handles?
Posted By: Uhrwerk

Re: sending array - 03/22/14 15:44

Sending arrays should be possible. But you have an error in your code in line 23.
Posted By: Reconnoiter

Re: sending array - 03/22/14 18:54

Line 23? You mean error message or diag or something like that?
Posted By: Uhrwerk

Re: sending array - 03/22/14 19:05

It was a gentle hint that it's kind of pointless to ask for help when not posting your code. How are we supposed to tell you why its not working when you're not showing us what you did?
Posted By: Reconnoiter

Re: sending array - 03/22/14 21:47

I thought that maybe it was not possible or something like that.

Here is the code that saves the handle of players (up to 8 players in theory), I let player 2 (client) wait for testing purposes:

Code:
ENTITY* pointer_player[9] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; 
var player_handle[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // stores the handle to the player
....
....

wait(-3); //wait till client has received player handles
if (connection == 2) while(1) { wait(1); }
   
   //create player
   var i;
   for(i = 1; i < 9; i++)
   {
    if (pointer_player[i] == NULL) { pointer_player[i] = ent_create("MDL_Player.mdl",vector(0,0,100),player_control);  	
    wait(-2); // wait until the handle is ready
    player_handle[i] = handle(pointer_player[i]);
    break;
    }
   }



And here is the code that sends the handle from the server to the client:

Code:
function on_server_event(void *pVarPtr, var id) // automatically assigned to on_server
{
    if (event_type == EVENT_JOIN) {
    var i;
    for(i = 1; i < 9; i++) { 
    if (player_handle[i] != 0) send_var_id(id,player_handle[i]); } // send init handles
    }	
...



I start the server+client (/connection = 3) first, that works fine. Than I join as client after >10 seconds and get an instant crash without an error message.

Thanks for watching.
Posted By: Ch40zzC0d3r

Re: sending array - 03/22/14 22:03

Id love to see the receive code too if theres some.
Also you should use send_data_id instead of sending the shit in a for loop because of overhead and cuz its just simplier, saves lines of code and is even faster^^
Posted By: Uhrwerk

Re: sending array - 03/22/14 22:18

send_var_id expects a pointer to a user defined variable / struct. You're passing an entity pointer to it.

http://www.conitec.net/beta/asend_var.htm !
Posted By: Reconnoiter

Re: sending array - 03/23/14 14:06

Thanks for the comments.

Quote:
Id love to see the receive code too if theres some.
, there is none. I check on the client within a while loop if the player_handle var array is not 0 and if the pointer is null and than retrieve the pointer through ptr_handle. But for testing purposes I disabled it and other functions that manipulate the player pointers.

Quote:
Also you should use send_data_id instead of sending the shit in a for loop because of overhead and cuz its just simplier, saves lines of code and is even faster^^
, sounds like a good idea, thanks laugh.

Quote:
send_var_id expects a pointer to a user defined variable / struct. You're passing an entity pointer to it.
, sry but I don't get this. Am I not sending an array (where the entity handle is saved in) through instead of an entity pointer? And why does it work when I send the handle through a var instead of an array?:

Code:
var player_handle_test = 0;
...
...
pointer_player_test = ent_create("MDL_Player.mdl",vector(0,0,100),player_control);  
wait(-2); // wait until the handle is ready
player_handle_test = handle(pointer_player[i]);
...
...
if (player_handle_test != 0) send_var_id(id,player_handle_test);



So many questions grin
Posted By: xbox

Re: sending array - 03/23/14 14:37

Quote:
Quote:
send_var_id expects a pointer to a user defined variable / struct. You're passing an entity pointer to it.
, sry but I don't get this. Am I not sending an array (where the entity handle is saved in) through instead of an entity pointer? And why does it work when I send the handle through a var instead of an array?:


When passing the variable, it works because "send_var_id expects a pointer to a user defined variable" and your variable is just simply a variable you defined. When you try to pass it via array, "You're passing it an entity pointer" which it does not accept because your array is of type ENTITY*, therefore the crash.
Posted By: Reconnoiter

Re: sending array - 03/23/14 18:14

This is my array that contains the handle:

Code:
var player_handle[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // stores the handle to the player



So have I defined it wrong for send_var_id?

Sry I am kind of newb at this.
Posted By: Uhrwerk

Re: sending array - 03/23/14 18:54

Your array is perfectly fine. But send_var_id expects a pointer to user defined variable. Pointer is the important part here. player_handle[i] is a handle, not a pointer. If you want to pass that specific content you have to pass the address of the variable. For Example &(player_handle[i]). Lite-C takes some of the complexity away for you, by automatically using pointers where appropriate. But this does not work in all cases.
Posted By: Reconnoiter

Re: sending array - 03/24/14 15:01

I first thought that with pointer you meant the entity pointer, I overlooked that the var/array could also have a pointer.

But how do write the send line correctly?, I now have tried the following 2 but those resulted in the instant crash:

Code:
if (player_handle[i] != 0) send_var_id(id,&player_handle[i]);


Code:
if (player_handle[i] != 0) send_var_id(id,&(player_handle[i]));



Thank you for your patience wink
Posted By: Uhrwerk

Re: sending array - 03/24/14 19:39

I didn't try it, when posting, sorry. My guess was that the second version is the correct one. Are you maybe forced to send the array as a whole?
Code:
if (player_handle[i] != 0) send_var_id(id,player_handle);

Posted By: xbox

Re: sending array - 03/24/14 21:05

I don't have any way of testing this, but if you have an array of handles and you need to pass a pointer instead, couldn't you do send_var_id(id, ptr_for_handle(player_handle[i]))
Posted By: Uhrwerk

Re: sending array - 03/24/14 21:26

That would return a pointer to an entity of course...
Posted By: Reconnoiter

Re: sending array - 03/25/14 10:17

Quote:
Are you maybe forced to send the array as a whole?
, bingo, that worked tyty. Now I don't have to use the for loop here grin. Also this means less overhead right?

Quote:
I don't have any way of testing this, but if you have an array of handles and you need to pass a pointer instead, couldn't you do send_var_id(id, ptr_for_handle(player_handle[i]))
, I could be wrong but I have a feeling that sending a handle is more safe (I have it from AUM77).
Posted By: Uhrwerk

Re: sending array - 03/25/14 19:32

You should always and exclusively send handles. Never ever send pointers! The point was just that you have to pass the pointer to send_var_id. That however is not the same as sending the pointer.
© 2024 lite-C Forums