Napisałem z miesiąc temu skrypt, który może się przydać do odczytu plansz.
Najpierw, żeby później łatwiej mi go było opisać, pokażę przykładową treść pliku z planszą:
create_object Bohater
x: 15
y: 30
on_create: show_message( 'Siemka, jestem bohaterem gry!' )
create_object Bloczek
x: 30
y: 50
on_draw: draw_rectangle( x, y, x + 50, y + 50, false )
on_step: x += 3 - random( 6 )
No więc widać już o co chodzi. Za pomocą create_object tworzymy nowy obiekt, a później używamy poszczególnych parametrów:
x - pozycja horyzontalna
y - pozycja wertykalna
on_create - kod, który ma wykonać od razu, gdy się stworzy
on_step - kod, który wykonuje, co step
on_draw - kod rysowania
żaden z nich nie jest obowiązkowy.
Wady owego sposobu to także zalety, m. in. całkiem łatwo edytować plansze. Skrypt GMS_add_site wygląda tak:
GML
var __file, __fstring, __line, __list, __class, __x, __y, __create, __step, __draw, __temp, __act;
__class = '';
__x = 0;
__y = 0;
__create = '';
__step = '';
__draw = '';
__temp = 0;
__fstring = argument[ 0 ];
__list = ds_list_create();
__file = file_text_open_read( __fstring );
while( !file_text_eof( __file ) )
{
__line = file_text_read_string( __file );
file_text_readln( __file );
if ( ( string_copy( __line, 1, 2 ) == '//' ) || ( __line == '' ) )
continue;
ds_list_add( __list, __line );
}
for( i = 0; i < ds_list_size( __list ); i += 1; )
{
__act = ds_list_find_value( __list, i );
switch( GMS_line_argument( __act, 0 ) )
{
case 'x:':
if ( __class != '' )
__x = real( GMS_line_argument( __act, 1 ) );
else
GMS_error( 'Fatalny błąd! Proba nadania wartosci obiektowi nieistniejacemu!' );
break;
case 'y:':
if ( __class != '' )
__y = real( GMS_line_argument( __act, 1 ) );
else
GMS_error( 'Fatalny błąd! Proba nadania wartosci obiektowi nieistniejacemu!' );
break;
case 'on_create:':
if ( __class != '' )
__create = string_copy( __act, string_length( 'on_create: ' ) + 1, string_length( __act ) - string_length( 'on_create: ' ) + 1 );
else
GMS_error( 'Fatalny błąd! Proba nadania wartosci obiektowi nieistniejacemu!' );
break;
case 'on_step:':
if ( __class != '' )
__step = string_copy( __act, string_length( 'on_step: ' ) + 1, string_length( __act ) - string_length( 'on_step: ' ) + 1 );
else
GMS_error( 'Fatalny błąd! Proba nadania wartosci obiektowi nieistniejacemu!' );
break;
case 'on_draw:':
if ( __class != '' )
__draw = string_copy( __act, string_length( 'on_draw: ' ) + 1, string_length( __act ) - string_length( 'on_draw: ' ) + 1 );
else
GMS_error( 'Fatalny błąd! Proba nadania wartosci obiektowi nieistniejacemu!' );
break;
}
if ( ( GMS_line_argument( __act, 0 ) == 'create_object' ) || ( i == ds_list_size( __list ) - 1 ) )
{
if ( object_exists( __class ) )
{
if ( __class != '' )
{
execute_string( '(' + string( id ) + ').__temp = instance_create( ' + string( __x ) + ', ' + string( __y ) + ', ' + __class + ' );' );
object_event_add( __temp, ev_create, 0, __create );
object_event_add( __temp, ev_step, 0, __step );
object_event_add( __temp, ev_draw, 0, __draw );
}
}
__class = GMS_line_argument( __act, 1 );
__x = 0;
__y = 0;
__create = '';
__step = '';
__draw = '';
}
}
Jeśli jesteście leniami lub tak wam się podoba, skrypt GMS_error może wyglądać nawet tak :P :
GML
show_message( argument[ 0 ] )
GMS_line_argument zaś powinien wyglądać mniej więcej tak:
GML
var __l, __a, __i, __c, __n, __s;
__s = argument[ 0 ];
__n = argument[ 1 ];
__l = ds_list_create();
__a = '';
for( __i = 0; __i < string_length( __s ); __i += 1; )
{
__c = string_char_at( __s, __i + 1 );
if ( __c == ' ' )
{
ds_list_add( __l, __a );
__a = '';
}
else
{
__a += __c;
if ( __i == string_length( __s ) - 1 )
{
ds_list_add( __l, __a );
}
}
}
return ds_list_find_value( __l, __n );
Tworzenie tymczasowej listy może wydawać się głupie, ale gdybyśmy edytowali na własne potrzeby funkcję, może okazać się przydatna :) .
Pozdrawiam,
Pental.