Game Development Community


#1
04/27/2010 (5:11 am)
No, not in a single step.

The simplest would be to serialize it (transform it in a string), and send the string by chunk, because of the limit in the size of the TCP packets.

After the initial transmission, you can transmit the updates only if it's not too frequent.

Nicolas Buquet
www.buquet-net.com/cv/
#2
04/27/2010 (5:22 am)
Yes, i had already thought. Asking costs nothing :-D
#3
04/28/2010 (6:16 am)
I solved the problem of sending array of clients with a simple script:

Server Side:
function serverCmdgetInventory(%client)  
{  
   %query = "SELECT * FROM inventory WHERE name = '"@%client.username@"' AND amount > 0";
	%result = $db.Execute(%query);
	%InventoryItemsNum = %result.NumRows();
	//echo ("-----------------------");
	//echo ("Client = " @ %client.username );
	//echo ("Inventory items = " @ %InventoryItemsNum);
	if(%result){
	   for(%rr=%result.firstRow();%rr>0;%rr=%result.nextRow())
	   {
	      %InventoryItem[%rr] = %result.Value("item");
         %InventoryAmount[%rr] = %result.Value("amount");	      
	      
			%query_items = "SELECT * FROM inventoryitems WHERE itemname = '"@%InventoryItem[%rr]@"'";
			%result_items = $db.Execute(%query_items);
			if (%result_items >= 1) {
			   %InventoryShape[%rr] = %result_items.Value("shapefile");
			}
			%result_items.Clear();
			
			//echo ( %InventoryItem[%rr] @ "," @ %InventoryAmount[%rr] @ "," @ %InventoryShape[%rr] );
			%outstrings = %outstrings @ "$" @ %InventoryItem[%rr] @ "," @ %InventoryAmount[%rr] @ "," @ %InventoryShape[%rr];
	   }
	   %result_items.Clear();
	}
	//echo ("-----------------------");
	%outstrings = "$" @ %InventoryItemsNum @ "" @ %outstrings;
	//echo ( %outstrings );
	commandToClient(%client, 'setInventory', %outstrings);
}

Client side:
function clientCmdsetInventory(%val )  
{
   ExplodeNetArray(%val, "$"); 
   for(%i = 0; %i < $explodedarraycount; %i++)
   {
      echo("  * " @ $explodedarray[%i]);
   }
}
function ExplodeNetArray(%value, %delimiter)
{
   %i=0;
   while( "" !$= %value )
   {
      %value = nextToken( %value , "theToken" , %delimiter );
      if (%i == 0) 
      {
         $explodedarraycount = %theToken;
      } else  {
         %x = %i - 1;
         $explodedarray[%x] = %theToken;
      }
      %i++;
   }   
}

Now i was wondering how you can assign a dynamic name to a array, passed as a parameter to the function ExplodeNetArray. eg:

function ExplodeNetArray(%value, %delimiter, %ARRAYNAME)
{
...
}

I do not understand if i can not find what i need from the documentation, or just missing. O__o
#4
04/28/2010 (9:06 am)
You can use the 'eval' function:
%arrayName = "$myGlobalVariable";
%evalStr = %arrayName @ "=" @ %myValue @";";
eval( %evalStr );

After that, you should have the value of the variable %myValue copied into the gobal variable $myGlobalVariable.

Nicolas Buquet
www.buquet-net.com/cv/
#5
04/28/2010 (10:09 am)
Thank you, i solved the great. I share the script, maybe someone can build on or reuse.

Server side:
//----------------------------------------------------------------------------
// Server Inventory message handlers
//----------------------------------------------------------------------------

function serverCmdgetInventory(%client)  
{  
   %query = "SELECT * FROM inventory WHERE name = '"@%client.username@"' AND amount > 0";
	%result = $db.Execute(%query);
	%InventoryItemsNum = %result.NumRows();
	//echo ("-----------------------");
	//echo ("Client = " @ %client.username );
	//echo ("Inventory items = " @ %InventoryItemsNum);
	if(%result){
	   for(%rr=%result.firstRow();%rr>0;%rr=%result.nextRow())
	   {
	      %InventoryItem[%rr] = %result.Value("item");
         %InventoryAmount[%rr] = %result.Value("amount");	      
	      
			%query_items = "SELECT * FROM inventoryitems WHERE itemname = '"@%InventoryItem[%rr]@"'";
			%result_items = $db.Execute(%query_items);
			if (%result_items >= 1) {
			   %InventoryShape[%rr] = %result_items.Value("shapefile");
			}
			%result_items.Clear();
			
			//echo ( %InventoryItem[%rr] @ "," @ %InventoryAmount[%rr] @ "," @ %InventoryShape[%rr] );
			%outstrings = %outstrings @ "$" @ %InventoryItem[%rr] @ "," @ %InventoryAmount[%rr] @ "," @ %InventoryShape[%rr];
	   }
	   %result_items.Clear();
	}
	//echo ("-----------------------");
	%outstrings = "$" @ %InventoryItemsNum @ "" @ %outstrings;
	//echo ( %outstrings );
	commandToClient(%client, 'setInventory', %outstrings);
}
Client side
//----------------------------------------------------------------------------
// Client Inventory message handlers
//----------------------------------------------------------------------------

function clientCmdsetInventory(%val )   
{
   ExplodeNetArray(%val, "$", "items"); 
   for(%i = 0; %i < $itemsCount; %i++)
   {
      %InventoryArray[%i] = $items[%i];
      //echo("  * " @ %InventoryArray[%i]);
   }
   
   for(%i = 0; %i < $itemsCount; %i++)
   {
      %outstring = "," @ $itemsCount @ "," @ %InventoryArray[%i];
      ExplodeNetArray(%outstring, ",", "itemDett");     
      %InventoryItems[%i] = $itemDett[0];
      %InventoryAmounts[%i] = $itemDett[1];
      %InventoryShapes[%i] = $itemDett[2];     
      //echo ( " * " @ %InventoryItems[%i] @ " - " @ %InventoryAmounts[%i] @ " - " @ %InventoryShapes[%i] );
   }
}
function ExplodeNetArray(%value, %delimiter, %name)
{
   %i=0;
   while( "" !$= %value )
   {
      %value = nextToken( %value , "theToken" , %delimiter );
      if (%i == 0) 
      {
         %evalStr = "$" @ %name @ "Count=" @ %theToken @";";
         eval( %evalStr );
      } else  {
         %x = %i - 1;
         %evalStr = "$" @ %name @ "[" @ %x  @ "]="" @ %theToken @"";";
         eval( %evalStr );
      }
      %i++;
   }   
}