SOLVED error message

Methudek

Member
I followed this tutorial:
and i have done but the program after running the project and I click on the square he will show me this:

___________________________________________
############################################################################################
ERROR in
action number 1
of Draw Event
for object textbox:

Push :: Execution Error - Variable Index [1] out of range [1] - -1.char(100036,1)
at gml_Object_textbox_Draw_0 (line 61) - var _current_txt_w = string_width( _txt_up_to_char ) - string_width( char[c, p] );
############################################################################################
gml_Object_textbox_Draw_0 (line 61)
 

Attachments

TsukaYuriko

☄️
Forum Staff
Moderator
You're trying to access the 1st element of an array that has a size of 1, which means the only element you can access is the 0th element (because arrays are 0-based).
 

Methudek

Member
so what should i do
code:
accept_key = keyboard_check_pressed(vk_space)

textbox_x = camera_get_view_x( view_camera[0] );
textbox_y = camera_get_view_y( view_camera[0] ) + 144;

//----------------------------setup------------------------------//
if setup == false
{

setup = true
draw_set_font(global.font_main);
draw_set_valign(fa_top);
draw_set_halign(fa_left);

//loop through the pages
for(var p = 0; p < page_number; p++)
{

//find how many characters are on each page and store that number in the "text_length" array
text_length[p] = string_length(text[p]);

//get the x position for the textbox
//no character (center the textbox)
text_x_offset[p] = 44;

//setting individual character and finding where the lines of text should break
for (var c = 0; c < text_length[p]; c++)
{

var _char_pos = c+1;

//store individual characters in the "char" array
char[c, p] = string_char_at(text[p], _char_pos);

//get current width of the line
var _txt_up_to_char = string_copy( text[p], 1, _char_pos );
var _current_txt_w = string_width( _txt_up_to_char ) - string_width( char[c, p] );

//get the last free space
if char[c, p] == " " { last_free_space = _char_pos+1 };

//get the line breaks
if _current_txt_w - line_break_offset[p] > line_width
{
line_break_pos[ line_break_num[p] , p ] = last_free_space;
line_break_num[p]++;
var _txt_up_to_last_space = string_copy( text[p], 1, last_free_space );
var _last_free_space_string = string_char_at( text[p], last_free_space );
line_break_offset[p] = string_width( _txt_up_to_last_space ) - string_width( _last_free_space_string );
}

//getting each characters coordinates
for (var c = 0; c < text_length[p]; c++)
{

var _char_pos = c+1;
var _txt_x = textbox_x + text_x_offset[p] + border;
var _txt_y = textbox_y + border;
there is a problem
//get current width of the line
var _txt_up_to_char = string_copy( text[p], 1, _char_pos );
var _current_txt_w = string_width( _txt_up_to_char ) - string_width( char[c, p] );
var _txt_line = 0;

//compesate for string breaks
for (var lb = 0; lb < line_break_num[p]; lb++)
{
//if the current looping character is after a line break
if _char_pos >= line_break_pos[lb, p]
{
var _str_copy = string_copy( text[p], line_break_pos[lb, p], _char_pos-line_break_pos[lb, p] );
_current_txt_w = string_width( _str_copy );

//record the "line" this character should be on
_txt_line = lb+1;// +1 since lb start at 0
}

}

//add to the x and y coordinates base on the new info
char_x[c, p] = _txt_x + _current_txt_w;
char_y[c, p] = _txt_y + _txt_line*line_sep;


}

}

}

}




//------------------typing the text------------------------------//
if draw_char < text_length[page]
{
draw_char += text_spd;
draw_char = clamp(draw_char, 0, text_length[page]);
}


//------------------------flip through pages---------------------//
if accept_key
{
//if typing is done
if draw_char == text_length[page]
{
//next page
if page < page_number-1
{
page++;
draw_char = 0;
}
//destroy textbox
else
{
//link text for options
if option_number > 0{
create_textbox(option_link_id[option_pos]);
}
instance_destroy();
}

}
//if not done typing
else
{

draw_char = text_length[page];

}
}





//------------------------draw the textbox----------------------//
var _txtb_x = textbox_x + text_x_offset[page];
var _txtb_y = textbox_y;
txtb_img += txtb_img_spd;
txtb_spr_w = sprite_get_width(txtb_spr);
txtb_spr_h = sprite_get_height(txtb_spr);
//back of the textbox
draw_sprite_ext(txtb_spr, txtb_img, _txtb_x, _txtb_y, textbox_width/txtb_spr_w, textbox_height/txtb_spr_h, 0, c_white, 1);

//---------------------options-----------------------------------//
if draw_char == text_length[page] && page == page_number - 1
{

//select options
option_pos += keyboard_check_pressed(vk_down) - keyboard_check_pressed(vk_up);
option_pos = clamp(option_pos, 0, option_number-1);

//draw the options
var _op_space = 16;
var _op_bord = 4;
for (var op = 0; op < option_number; op++;)
{
//the option box
var _o_w = string_width(option[op]) + _op_bord*2;
draw_sprite_ext(txtb_spr, txtb_img, _txtb_x + 16, _txtb_y - _op_space*option_number + _op_space*op, _o_w/txtb_spr_w, (_op_space-1)/txtb_spr_h, 0, c_white, 1)

//the arrow
if option_pos == op
{
draw_sprite(textbox_arrow, 0, _txtb_x, _txtb_y - _op_space*option_number + _op_space*op);
}

//the text option
draw_text( _txtb_x + 16 + _op_bord, _txtb_y - _op_space*option_number + _op_space*op + 2, option[op]);

}

}

//draw the text
for(var c = 0; c < draw_char; c++)
{

//draw text
draw_text( char_x[c, page], char_y[c, page], char[c, page] )

}
 

chamaeleon

Member
It is possible I'm misreading due to post having non-existent indentation, but
GML:
        //setting individual character and finding where the lines of text should break
        for (var c = 0; c < text_length[p]; c++)
        {
            ...
            //getting each characters coordinates
            for (var c = 0; c < text_length[p]; c++)
            {
                ...
            }
Not a good idea to use the same variable for two nested loops. Fine if one loop follows the other, but not nested.
 

TheouAegis

Member
Personally, I think the array should be char[p,c] rather than char[c,p]. Pretty sure the current setup is exceedingly gluttonous with memory.
 

Methudek

Member
guys I set the char[c, p] to char[p, c] and I have this error:


___________________________________________
############################################################################################
ERROR in
action number 1
of Draw Event
for object textbox:

Variable Index [1] out of range [1]
at gml_Object_textbox_Draw_0 (line 61) - var _current_txt_w = string_width( _txt_up_to_char ) - string_width( char[p,c] );
############################################################################################
gml_Object_textbox_Draw_0 (line 61)
 

chamaeleon

Member
guys I set the char[c, p] to char[p, c] and I have this error:


___________________________________________
############################################################################################
ERROR in
action number 1
of Draw Event
for object textbox:

Variable Index [1] out of range [1]
at gml_Object_textbox_Draw_0 (line 61) - var _current_txt_w = string_width( _txt_up_to_char ) - string_width( char[p,c] );
############################################################################################
gml_Object_textbox_Draw_0 (line 61)
(Assuming you have changed all code to reflect the fact that you have now switched the use of c and p for your array in all the code)

The Variable Index [1] out of range [1] part of the message is telling you that you are trying to access index 1 (Index [1]) in array of size 1 (out of range [1]). If an array has size 1, only index 0 is valid, as the indexing of arrays start at zero. So, the conclusion is that your code has only put one element into an array that you wish to access the second element of.

You need to use the debugger or show_debug_message() to determine what part of your code does not put as many elements into the array as you would expect and why it does not (ideally by outputting various pieces of data telling you what values relevant variables has, what text is being worked on, the result of relevant operations, etc.).
 
You don't set the index to 0. The problem is that one of your loops thinks the array index is larger than it is. You have to find out why that is happening and correct that. Perhaps the array should be larger and you aren't adding an entry you should be. Or perhaps the array is at the right size and the way you are maintaining a record of the size of the array is wrong. Perhaps you are deleting an entry incorrectly at some point. You should use the debugger to figure out what is happening internally in your game and once you've built a mental model of what is going on, you should compare it to what you want to happen. Then the problem should reveal itself.
 
Top