Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 1,119 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 6 of 8 1 2 3 4 5 6 7 8
Re: Dropping items from inventory bag [Re: txesmi] #435947
01/16/14 12:36
01/16/14 12:36
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
doble post blush

Anyway you have to hear about the great benefits of using arrays to allocate your data.

If your item pointers are sorted into an array and you fill the inventory item id with its array index you can avoid all those ugly switch-case blocks.

Code:
Item *myItems[3];
#define SNIPERGUN          0 
#define SHOTGUN            1
#define MACHINEGUN         2

//instead of
Item *itemSnipergun = NULL;
Item *itemShotgun = NULL;
Item *itemMachinegun = NULL;



Since the answer of the inventory.c functions is the id of the item, you can build parallel data structs with the data you need to manage the items from the inventory to the scenery and viceversa.

For example, you need the file name of the entities when you drop them from the inventory. You can build a TEXT struct with all your item file names and use the item id to create the entity.

Code:
Item *myItems[3];
TEXT *txtItemModelNames = { strings = 3; }
#define SNIPERGUN          0 
#define SHOTGUN            1
#define MACHINEGUN         2

// When you create each item you fill the filenames,
myItems[SNIPERGUN] = inv_create_item ( SNIPERGUN,  1, "icon2.pcx", "icon2.pcx" );
inv_insert_item_into_bag ( bag, myItems[SNIPERGUN] );
str_cpy ( (txtItemModelNames.pstring)[SNIPERGUN], "snipergun.mdl" );

// So you can create the proper entity with the item id
ENTITY *ent = ent_create ( (txtItemModelNames.pstring)[item_id], vecPos, NULL );

// instead of
if ( item_id == SNIPERGUN )
   ENTITY *ent = ent_create ( "snipergun.mdl", vecPos, NULL );
if ( item_id == SHOTGUN )
   ENTITY *ent = ent_create ( "shotgun.mdl", vecPos, NULL );
if ( item_id == MACHINEGUN )
   ENTITY *ent = ent_create ( "machinegun.mdl", vecPos, NULL );

// or
switch ( item_id )
{
   case SNIPERGUN:
      ENTITY *ent = ent_create ( "snipergun.mdl", vecPos, NULL );
      break;
   case SHOTGUN:
   ...
}



In order to pick an object from the scenery and add it to the inventory it should have the item id stored somewhere. So when you drop an object you save the item id in the newly created entity and when you pick an object you use its saved item id to add it to the inventory.

Code:
// object dropped
ENTITY *ent = ent_create ( (txtItemModelNames.pstring)[item_id], vecPos, NULL );
ent.skill1 = item_id;

// object picked
inv_insert_item_into_bag ( bag, myItems[mouse_ent.skill1] );



Here the example I gived you above using arrays
Code:
#include <acknex.h>
#include <default.c>
#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 PRAGMA_PATH "%EXE_DIR%\\templates\\levels"

#define MAIN_BAG_ID        1
#define ACTIV_WPN_SLOT_ID  0

SOUND *sndReload = "reload.wav";
SOUND *sndDrop = "getready.wav";
SOUND *sndShot = "shot2.wav";

ENTITY *active_weapon = NULL;
Bag *bag = NULL;

Item *myItems[3];
TEXT *txtItemModelNames = { strings = 3; }

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


action active_weapon_action ()
{
	ENTITY *ent = active_weapon;
	if ( ent )
	{
		ent.x = 70;
		ent.y = -20;
		ent.z = -20;
		while ( ent == active_weapon )
			wait(1);
		ent_remove ( ent );
	}
}

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)
			{
				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)
			{
				active_weapon = ent_createlayer ( (txtItemModelNames.pstring)[placed_item_id], SHOW, 1 );
				active_weapon_action ();  
				snd_play ( sndReload, 100, 0 );
			}
		}   
	}
}

ENTITY *create_scenery_object ( var type, VECTOR *vecPos )
{
	ENTITY *ent = ent_create ( (txtItemModelNames.pstring)[type], vecPos, NULL ); 
	ent->skill1 = type;
	snd_play ( sndDrop, 100, 0 );
	return ent;
}

void onMouseLeft ()
{
	if ( mouse_panel )
		return;
	
	if ( inv_is_floating() )
	{
		VECTOR vecTemp;
		vec_set ( vecTemp, mouse_dir3d );
		vec_scale ( vecTemp, 1000 );
		vec_add ( vecTemp, camera.x );
		c_trace ( camera.x, vecTemp, IGNORE_PASSABLE );
		if ( HIT_TARGET )
		{
			vec_set ( vecTemp, hit.x );
			vecTemp.z += 10;
			var item_id = inv_get_floating_icon_item ();
			ENTITY *entObject = create_scenery_object ( item_id, hit.x );
		}
	}
	else if ( mouse_ent )
	{
		ENTITY *ent = mouse_ent;
		inv_insert_item_into_bag ( bag, myItems[mouse_ent.skill1] ); 
		wait(1); // ent_remove needs a wait before calling it from a 3dgs event
		ent_remove ( ent );
	}
	else if ( active_weapon )
	{
		snd_play ( sndShot, 100, 0 );
	}
}

void newLevel ()
{
	level_load ( "" );
	ENTITY *entTerrain = ent_create( "water.hmp", nullvector, NULL );
	entTerrain.flags2 |= UNTOUCHABLE;
	camera.x = -200;
	camera.z = 400;
	camera.tilt = -60;
	inv_run_slot_callback ( bag, ACTIV_WPN_SLOT_ID );
}

function main()
{
	mouse_mode = 4;
   inv_init();
	
   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);
   
   myItems[SNIPERGUN] = inv_create_item ( SNIPERGUN,  1, "icon2.pcx", "icon2.pcx" );
   inv_insert_item_into_bag ( bag, myItems[SNIPERGUN] );
   str_cpy ( (txtItemModelNames.pstring)[SNIPERGUN], "snipergun.mdl" );
   
   myItems[SHOTGUN] = inv_create_item ( SHOTGUN,    1, "icon3.pcx", "icon3.pcx" );
   inv_insert_item_into_bag ( bag, myItems[SHOTGUN] );
   str_cpy ( (txtItemModelNames.pstring)[SHOTGUN], "shotgun.mdl" );

   myItems[MACHINEGUN] = inv_create_item ( MACHINEGUN, 1, "icon4.pcx", "icon4.pcx" );
   inv_insert_item_into_bag ( bag, myItems[MACHINEGUN] );
   str_cpy ( (txtItemModelNames.pstring)[MACHINEGUN], "machinegun.mdl" );

   set_on_click_callback ( "slot_was_clicked" );
   on_mouse_left = onMouseLeft;
   on_space = newLevel;
   
   newLevel ();
   inv_open_bag ( bag, 0, 0 );
   
   while ( !key_esc )
		wait(1);
   
   sys_exit ( NULL );
}



Salud!

Re: Dropping items from inventory bag [Re: txesmi] #436500
01/28/14 05:05
01/28/14 05:05
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
Thank you txesmi.

I tried incorporating some of your code into my program.

So far, this is what my OrcStronghold.c program looks like:

Code:
...

function change_mouse_mode()
{
   mouse_mode += 1;
   mouse_mode %= 2;

   if (mouse_mode == 1)
   {
      mouse_map = cursor_pcx;
   }
   
}

...


// switch to toggle inventory:
void toggleInventory()
{
	// put some comparisons here:
	// f.e. if we are dead, don't allow to toggle inventory:
	if(player.HEALTH <= 0)
	{ 
		return; 
	}
	// toggle var:
	invOpen += 1;
	invOpen %= 2;
	
	// if we've opened the inventory:
	if(invOpen == 1)
	{
		// show the mouse pointer:

		change_mouse_mode();
		mouse_mode = 1;
		
		// PUT IN CODE FOR INVENTORY GUI
		
	   inv_open_bag ( bag, 375, 75 );  // OPEN INVENTORY BAG CODE
	}
	else
	{

	   mouse_mode = 0;
	   inv_close_bag(bag);
   }
}

action Player()
{
   ...

   on_i = toggleInventory; 
   on_m = change_mouse_mode;
	
   set_on_click_callback("inv_run_slot_callback");

   while (my.x)
   {
      ...
   }

   ...
}

function inv_run_slot_callback ( Bag *bag, var slot_id ) 
{
	if ( !on_click_callback_function_ptr )
		return 0;
	
	int i = 0;
	for ( ; i<bag->_slot_count; i++ )
	{
		Slot *slot = bag->slots[i];
		if ( slot->id != slot_id )
			continue;
		Item *item = slot->item;
		if ( !item )
			return 0;
		on_click_callback_function_ptr ( INV_ITEM_PLACED, bag->id, slot->id, item->id, NULL );
		return 1;
	}
	return 0;
}



With this code, the game goes fine, until I bring up my inventory bag by pressing the "i" key. The inventory bag will come up, and I can even arm my player with a weapon using it. However, when I press the "i" key again to close the inventory bag, my player will move like normal for about a second, but then freeze in place.

Do you know what bug in this code might be causing this? I have been trying to figure it out myself.

Last edited by Ruben; 01/28/14 05:06.
Re: Dropping items from inventory bag [Re: Ruben] #436502
01/28/14 05:34
01/28/14 05:34
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
Never mind txesmi. I think I screwed up somewhere. Let me get back to you.

Re: Dropping items from inventory bag [Re: Ruben] #436508
01/28/14 07:17
01/28/14 07:17
Joined: Apr 2007
Posts: 3,751
Canada
WretchedSid Offline
Expert
WretchedSid  Offline
Expert

Joined: Apr 2007
Posts: 3,751
Canada
I'm broke as fuck, but Txsemi, if you give me your paypal address (via PM), I'll send you 10€ for your patience in this thread.


Shitlord by trade and passion. Graphics programmer at Laminar Research.
I write blog posts at feresignum.com
Re: Dropping items from inventory bag [Re: WretchedSid] #436516
01/28/14 09:26
01/28/14 09:26
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Thanks Sid!
Anything goes to increase the spanish gdp and put us out of slavery grin

Re: Dropping items from inventory bag [Re: txesmi] #436564
01/29/14 06:24
01/29/14 06:24
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
Too bad this site does not give me the luxury of deleting posts already made.

Re: Dropping items from inventory bag [Re: Ruben] #436566
01/29/14 06:36
01/29/14 06:36
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
I do applaud and thank you txesmi for being willing to help me with understanding and working with this open source inventory.c code. You seem to be one of the rare few that can understand it well.

Re: Dropping items from inventory bag [Re: Ruben] #436780
02/02/14 00:41
02/02/14 00:41
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
txesmi: I am looking at your sample code, specifically at this function of yours:

Code:
action active_weapon_action ()
{
	ENTITY *ent = active_weapon;
	if ( ent )
	{
		ent.x = 70;
		ent.y = -20;
		ent.z = -20;
		while ( ent == active_weapon )
			wait(1);
		ent_remove ( ent );
	}
}



I am assuming that this function is meant to position a gun in front of the camera, so that the gun points at wherever the player is facing.

I am trying to change this, in that instead of positioning a gun to point wherever the player faces, I would like to place a hand weapon (like a sword or mace) into the hand of the player. I currently have a function that works in making this happen, but it is not really part of a program that uses arrays.

This is the code that I used (pre-arrays) to place the weapon into the hand of the player:

Code:
action attach_weapon() 
{
	set(my,PASSABLE);
	proc_mode = PROC_LATE;
	
	while (player != NULL) 
	{
		vec_for_vertex(temp.x,player,997); //hand palm base: Vector # 987 on player
		vec_for_vertex(temp2.x,player,968); //hand palm tip: Vector # 982 on player
		vec_set(my.x,temp.x);
		vec_diff(temp.x,temp2.x,temp.x);
		vec_to_angle(my.pan,temp.x); 
		vec_set(my.pan,my.pan);
		wait(1);
	}
}



This code worked for me in attaching a hand weapon to the player's hand, to use as a weapon.

Do you know how I would incorporate the logic of my attach_weapon() function to work in your active_weapon_action() function?

Here is what I tried so far:

Code:
action active_weapon_action ()
{
	ENTITY *ent = active_weapon;
	if ( ent )
	{
		set(my,PASSABLE);
	   proc_mode = PROC_LATE;
	
	   while (player != NULL) 
	   {
	   	vec_for_vertex(temp.x,player,997); //hand palm base: Vector # 987
		   vec_for_vertex(temp2.x,player,968); //hand palm tip: Vector # 982
		   vec_set(my.x,temp.x);
		   vec_diff(temp.x,temp2.x,temp.x);
		   vec_to_angle(my.pan,temp.x); 
		   vec_set(my.pan,my.pan);
		   wait(1);
	   }
		while ( ent == active_weapon )
			wait(1);
		ent_remove ( ent );
	}
}


...but it does not seem to be working. It is not adding a weapon to my player's hand when I place a weapon into the active weapon slot.

Any help would be appreciated. Thanks.

Last edited by Ruben; 02/02/14 00:44.
Re: Dropping items from inventory bag [Re: Ruben] #436813
02/02/14 23:03
02/02/14 23:03
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi Ruben,
I don't know where to start. I fear that answering your question and giving you the solution is not really a good solution. It will probably become to a similar problem in very few code lines.

Forget about my code by the moment and look at yours. It is absolutely necessary to understand its content before trying to adapt nothing. Socrates would say that the answer was in you all the time (maieutics) so...

Could you explain each code line contained into your 'attach_weapon' function? Let me read it. Be neat!

If not, could you enumerate, explain and reference each pointer into your 'attach_weapon' function?

If not, do you know what a pointer is?

If not, take the time to read the AUM liteC pointers tutorial (at least twice :D)

With manual and pointers in your circle of acquaintances you should be able to answer the second question and probably the first and yours. If not, I can guide you to find the problem by yourself. It deserves the effort. Otherwise I can not easily explain what is wrong.

Does this help?

pd: peripheral reading: AUM liteC structs tutorial

Last edited by txesmi; 02/02/14 23:18.
Re: Dropping items from inventory bag [Re: txesmi] #436891
02/05/14 06:29
02/05/14 06:29
Joined: Jun 2010
Posts: 590
California
Ruben Offline OP
User
Ruben  Offline OP
User

Joined: Jun 2010
Posts: 590
California
I think that I am not ready to change my whole program to accommodate arrays. It was already working without them, however inefficient it may be. I think my current goal should be to look into learning how to use the callback functions in inventory.c to allow me to drop items from the inventory bag. I believe I may be able to do this with the set_on_float_end_callback() callback function, but I am trying to figure out how.

Any help in that direction would be greatly appreciated. Thanks.

Page 6 of 8 1 2 3 4 5 6 7 8

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1