Ok, I played a bit with the code and the very first thing I found is a bug xP

Modify the function 'inv_init' to solve it:
Code:
function inv_init()
{
	// Create a instance of the floating item struct and set the global_floating_item_ptr to it
	
	FloatingItem* floating_item = new(FloatingItem);
	floating_item->item = NULL;	
	inv_floating_flag = 0; // set global floating flag
	global_floating_item_ptr = floating_item;  
	
	on_click_callback_function_ptr = NULL;
	on_float_end_callback_function_ptr = NULL;
	on_close_bag_callback_function_ptr = NULL;
	on_open_bag_callback_function_ptr = NULL;
}



Another thing I found is that you can't insert items into an inventory that is not opened. Modify the functions 'inv_add_slot_to_bag' and 'inv_insert_item_into_bag' to solve it. I also added a slot_on_click callback to 'inv_insert_item_into_bag' that is really needed.

Code:
function inv_add_slot_to_bag(Bag* bag, int slot_id, int group_id, STRING* default_image, int rel_x_pos, int rel_y_pos)
{
	Slot* new_slot;
	new_slot = new(Slot);

	// Set the position for the new slot
	
	new_slot->rel_x_pos = rel_x_pos;
	new_slot->rel_y_pos = rel_y_pos;	
	new_slot->background_image = default_image;
	new_slot->item = NULL;
	new_slot->id = slot_id;
	new_slot->group_id = group_id;
	new_slot->bag_id = bag->id;	
	new_slot->slot_panel = NULL;	// <---- NEW LINE
	
	bag->slots[bag->_slot_count] = new_slot;
	
	bag->_slot_count += 1;
	
}

function inv_insert_item_into_bag ( Bag* bag, Item* item )
{
	int i;
	for(i=0;i < bag->_slot_count; i++)
	{
		if (bag->slots[i] != NULL)
		{
			if (bag->slots[i]->item == NULL)
			{
				if ((bag->slots[i]->group_id == item->group_id) || (bag->slots[i]->group_id == 0))
				{
					bag->slots[i]->item = item;
					if ( bag->slots[i]->slot_panel )				
						bag->slots[i]->slot_panel->bmap = bmap_create(item->inventory_icon);
					if (on_click_callback_function_ptr) on_click_callback_function_ptr(INV_ITEM_PLACED,bag->id,bag->slots[i]->id,item->id,NULL);
					return 1;
				}
			}
		}
	}
	
	return 0;
}



These little changes let you run the following code without errors and you will be able to change the active weapon using a slot of the inventory:
Code:
#include <acknex.h>
#include "inventory.c"

#define PRAGMA_PATH "%EXE_DIR%\\templates\\sounds"
#define PRAGMA_PATH "%EXE_DIR%\\templates\\images"
#define PRAGMA_PATH "%EXE_DIR%\\templates\\models"

#define MAIN_BAG_ID        1
#define ACTIV_WPN_SLOT_ID  0

#define SNIPERGUN          0
#define SHOTGUN            1
#define MACHINEGUN         2

SOUND *sndReload = "reload.wav";

ENTITY *active_weapon = NULL;

function slot_was_clicked ( int occurrence, int bag_id, int slot_id, int placed_item_id, int removed_item_id ) 
{
   if ( ( occurrence == INV_ITEM_REMOVED ) || ( occurrence == INV_ITEM_SWAPPED ) )
   {
		if (bag_id == MAIN_BAG_ID)
		{
			if (slot_id == ACTIV_WPN_SLOT_ID)
			{
				ent_remove ( active_weapon );
				active_weapon = NULL;
			}
		}   
   }
	if ( ( occurrence == INV_ITEM_PLACED ) || ( occurrence == INV_ITEM_SWAPPED ) )
	{
		if (bag_id == MAIN_BAG_ID)
		{
			if (slot_id == ACTIV_WPN_SLOT_ID)
			{
				snd_play ( sndReload, 100, 0 );
				switch ( placed_item_id )
				{
					case SNIPERGUN:  active_weapon = ent_create ( "snipergun.mdl", nullvector, NULL );  break;
					case SHOTGUN:    active_weapon = ent_create ( "shotgun.mdl", nullvector, NULL );    break;
					case MACHINEGUN: active_weapon = ent_create ( "machinegun.mdl", nullvector, NULL ); break;
				}
			}
		}   
	}
}

function main()
{
	mouse_mode = 4;
   inv_init();
   wait(1);
	level_load ( "" );
	camera->x = -150;
   Item *gun1 = inv_create_item ( SNIPERGUN, 1, "icon2.pcx", "icon2.pcx" );
   Item *gun2 = inv_create_item ( SHOTGUN, 1, "icon3.pcx", "icon3.pcx" );
   Item *gun3 = inv_create_item ( MACHINEGUN, 1, "icon4.pcx", "icon4.pcx" );

   Bag *bag = inv_create_bag ( MAIN_BAG_ID,"bluebar.pcx");
   inv_add_slot_to_bag ( bag, ACTIV_WPN_SLOT_ID, 1, "flash2.tga", 0, 0 );
   inv_add_slot_to_bag(bag,1,0,"flash2.tga",0,120);
   inv_add_slot_to_bag(bag,2,0,"flash2.tga",0,180);
   inv_add_slot_to_bag(bag,3,0,"flash2.tga",0,240);
   
   set_on_click_callback ( "slot_was_clicked" );
   
   inv_insert_item_into_bag ( bag, gun1 );
   inv_insert_item_into_bag ( bag, gun2 );
   inv_insert_item_into_bag ( bag, gun3 );
   
   inv_open_bag ( bag, 0, 0 );
   
   while ( !key_esc )
   {
   	if ( active_weapon != NULL )
   		active_weapon->pan += 5*time_step;
   	wait(1);
	}
   
   sys_exit ( NULL );
}



Salud!

Last edited by txesmi; 01/04/14 21:49.