Posted By: draculaFactory
Wierd invalid pointer. - 09/01/10 22:41
The code below causes a crash when I click on my inventory panel and move it, the crash occurs during the "sort_panels" function, the panel pointer that is passed into the function becomes invalid somehow... Oddly enough, to test this, I put "reset(panel,SHOW);" just before "if(temp_panel.layer > panel.layer && temp_panel != panel)" to test and the function did hide the correct panel, meaning that the pointer is not actually invalid.
Here's the clencher: I went through the entire code line by line and discovered that the crash was actually being caused by the "welcome_text" element at the top of the script; this text is not the original text that I was using, I pasted this text from the manual for testing to see if a text element was causing the problem. Sure enough, when I comment out "welcome_text", there is no crash when I click on a panel, and everything moves and sorts just fine. The text element is not being referenced by any code in this script.
What gives?
Code:
// inventory art BMAP* backpack_bmp = "backpack_panel.tga"; BMAP* items_bmp = "item_icons.tga"; BMAP* item_x48_bmp = "item_button_x48.tga"; // container variable arrays var slot_owner[500]; var slot_number[500]; var item_slot_id[500]; var icon_bmp_x[500]; var icon_bmp_y[500]; var item_mdl_id[500]; var item_stack[500]; var drop_sound[500]; // pointers used for the 6 inventory pack slots PANEL* pack_slot1; PANEL* pack_slot2; PANEL* pack_slot3; PANEL* pack_slot4; PANEL* pack_slot5; PANEL* pack_slot6; // helps determine the clicked slot number var panel_slot_start = 0; STRING* welcome_str = "Welcome"; TEXT* welcome_text = { layer = 1; pos_x = 10; pos_y = 10; string (welcome_str,"this is","a","TEXT"); flags = CENTER_X | TRANSLUCENT | SHOW; } function sort_panels(PANEL* panel) { var list_no = 0; var sss = 0; PANEL* temp_panel; while(list_no < total_panels) { temp_panel = ptr_for_handle(panel_list[list_no]); if(temp_panel.layer > panel.layer && temp_panel != panel) { layer_sort(temp_panel,temp_panel.layer - 1); } list_no += 1; } layer_sort(panel,total_panels); } //moves a panel with mouse function move_panel(PANEL* panel) { //local variables VECTOR panel_vec; VECTOR pointer_vec; VECTOR panel_offset; vec_set(panel_vec.x,vector(panel.pos_x,panel.pos_y,0)); //get initial mouse position pointer_vec.x = mouse_pos.x; pointer_vec.y = mouse_pos.y; sort_panels(panel); // while mouse held, loop while(mouse_left == 1) { panel_offset.x = mouse_pos.x - pointer_vec.x; panel_offset.y = mouse_pos.y - pointer_vec.y; panel.pos_x = panel_vec.x + panel_offset.x; panel.pos_y = panel_vec.y + panel_offset.y; // restrict panel positions to on-screen panel.pos_x = clamp(panel.pos_x,0,screen_size.x - panel.size_x); panel.pos_y = clamp(panel.pos_y,0,screen_size.y - panel.size_y); wait(1); } } // creates an icon bmp cutout from the slot number function get_icon_cutout(var slot_no) { BMAP* icon_bmp; STRING* icon_str = "item_icons.tga"; str_cat(icon_str,"#"); str_cat(icon_str,str_for_num(NULL,icon_bmp_x[slot_no])); str_cat(icon_str,"#"); str_cat(icon_str,str_for_num(NULL,icon_bmp_y[slot_no])); str_cat(icon_str,"#48#48"); icon_bmp = bmap_create(icon_str); return(icon_bmp); } // inventory panel click event function inv_panel_click(PANEL* inv_panel) { var format; var pixel; // lock and read panel background to determine if it was actually clicked format = bmap_lock(inv_panel.bmap,0); pixel = pixel_for_bmap(inv_panel.bmap,mouse_pos.x - inv_panel.pos_x,mouse_pos.y - inv_panel.pos_y); bmap_unlock(inv_panel.bmap); // if panel was clicked if(pixel != 0) { // if cursor is empty if(item_slot_id[0] == 0) { move_panel(inv_panel); } // bring this panel to front sort_panels(inv_panel); } } // fills the inv panel with an amount of inventory slots function create_slots(PANEL* inv_panel, var rows, var cols, var start_x, var start_y, var start_slot) { var rows_created = 0; var cols_created = 0; var x_pos; var y_pos; var slot_step; x_pos = start_x; y_pos = start_y; slot_step = start_slot; while(rows_created < rows) { while(cols_created < cols) { pan_setwindow(inv_panel,0,x_pos,y_pos,48,48,items_bmp,icon_bmp_x[slot_step],icon_bmp_y[slot_step]); pan_setbutton(inv_panel,0,0,x_pos,y_pos,item_x48_bmp,item_x48_bmp,item_x48_bmp,item_x48_bmp,NULL,NULL,NULL); x_pos += 48; slot_step ++; cols_created ++; wait(1); } y_pos += 48; x_pos = start_x; cols_created = 0; rows_created ++; wait(1); } } // creates the pack panel function create_inv_panel(var start_slot, var x_pos, var y_pos) { PANEL* inv_panel = pan_create("bmap = backpack_bmp; event = inv_panel_click;",1); inv_panel.pos_x = x_pos; inv_panel.pos_y = y_pos; create_slots(inv_panel,4,8,64,168,start_slot); register_panel(inv_panel); layer_sort(inv_panel,2); return(inv_panel); } // starts the inventory system function inv_startup() { BMAP* drag_icon; pack_slot1 = create_inv_panel(16,0,32); set(pack_slot1,SHOW); pack_slot2 = create_inv_panel(49,128,48); set(pack_slot2,SHOW); while(1) { if(key_1 == 1) { snd_play(select_snd,vol_effects,0); toggle(pack_slot1,SHOW); while(key_1 == 1){wait(1);} } // determine starting slot number for inventory mouse functions if(mouse_panel == pack_slot1){panel_slot_start = 16;} if(mouse_panel == pack_slot2){panel_slot_start = 49;} if(mouse_panel == pack_slot3){panel_slot_start = 81;} if(mouse_panel == pack_slot4){panel_slot_start = 113;} if(mouse_panel == pack_slot5){panel_slot_start = 145;} if(mouse_panel == pack_slot6){panel_slot_start = 177;} // mouse icon if(slot_number[0] != 0) { // create the icon cutout referencing the cursor slot drag_icon = get_icon_cutout(0); // draw the icon under the mouse cursor draw_quad(drag_icon,vector(mouse_pos.x - 24,mouse_pos.y - 24,0),NULL,vector(48,48,0),NULL,NULL,100,0); } wait(1); } }
Here's the clencher: I went through the entire code line by line and discovered that the crash was actually being caused by the "welcome_text" element at the top of the script; this text is not the original text that I was using, I pasted this text from the manual for testing to see if a text element was causing the problem. Sure enough, when I comment out "welcome_text", there is no crash when I click on a panel, and everything moves and sorts just fine. The text element is not being referenced by any code in this script.
What gives?