Jump to content


Photo

[Possible bug] increment variable in script for loop


Best Answer rwkay, 22 February 2016 - 03:12 PM

You want to declare i as var i; and then it would be local to the script and not an instance variable that would be shared between all the scripts that are executed on the same instance

 

see http://docs.yoyogame... variables.html

 

Russell

Go to the full post


  • This topic is locked This topic is locked
1 reply to this topic

#1 Dmajster

Dmajster

    GMC Member

  • GMC Member
  • 747 posts
  • Version:GM:Studio

Posted 22 February 2016 - 03:06 PM

I had an odd problem today that stopped me sending server-side instance positions back to the  client-side players.

 

Only the first socket would receive the data and the others wouldn't. I use this technique to send the data.

    for( i = 0 ; i < ds_list_size( clientList ) ; i++ )
    {
        show_debug_message( string( ds_list_size( clientList ) ) + " INDEX: " + string( i ) + " VALUE: " + string( ds_list_find_value( clientList, i ) ) );
        server_update_userPositons( global.serverBuffer, ds_list_find_value( clientList, i ), clientList, clientObjects );
    }

This just goes trough the list of all connected sockets and sends each of them the positions of dummy instances for each player.

 

Here is the code for the server_update_userPositions()

///server_update_userPositons( buffer, targetSocket, list, objects )

buffer = argument[0];
socket = argument[1];
list = argument[2];
objects = argument[3];

buffer_seek( buffer, buffer_seek_start, 0 );
buffer_write( buffer, buffer_u8, NTW_USERPOSITIONS );
buffer_write( buffer, buffer_u8, ds_list_size( list ) );

for( i = 0 ; i < ds_list_size( list ) ; i++ )
{
    var object = ds_map_find_value( objects, ds_list_find_value( list, i ) ).playerObject;
    buffer_write( buffer, buffer_u8, ds_list_find_value( list, i ) );
    buffer_write( buffer, buffer_u16, object.x );
    buffer_write( buffer, buffer_u16, object.y );
    //show_debug_message( "socket: " + string( ds_list_find_value( list, i ) ) );
}

show_debug_message( "Sending client positions to socket: " + string( socket ) );

network_send_packet( socket, buffer, buffer_tell( buffer ) );

now the really odd thing is, If i tried to run the code as is shown above, it wouldn't have worked. The reason is that the two for loops seem to conflict, sort of like nested for loops where one of them increments the others variable. To make this work i had to change the scripts for loop increment variable to some other name and it started working. is this a bug or a feature I'm unaware of?

 

EDIT: LINK TO PROJECT: https://drive.google...STcwdUtsUEJxQUk


Edited by Dmajster, 22 February 2016 - 03:10 PM.

  • 0

#2 rwkay

rwkay

    YoYo Games CTO

  • YoYo Games Staff
  • 2937 posts
  • Version:Unknown

Posted 22 February 2016 - 03:12 PM   Best Answer

You want to declare i as var i; and then it would be local to the script and not an instance variable that would be shared between all the scripts that are executed on the same instance

 

see http://docs.yoyogame... variables.html

 

Russell


  • 1