Destroy Object
#1

I've been working on destroyable object for carepack system but seems like doesen't work. When i shoot with gun in object, it won't destroy. What's the problem ?

Code:
new ArmyCrate[MAX_CRATES];
Code:
for(new i=0; i < MAX_CRATES; i++)
	{
	    ArmyCrate[i] = 50; //This should be like object health i want to destroy
		pCarePackage[i][CarePackagePlane] = pCarePackage[i][CarePackageParachute] = -1;
	}
Code:
public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
{
    if(hittype == BULLET_HIT_TYPE_OBJECT)
	{
    	for(new val = 0; val < MAX_CRATES; val++)
		{
			if(crateexists[val])
			{
			    new obb = ArmyCrate[val];
				if(IsValidDynamicObject(obb))
				{
					new Float:oX, Float:oY, Float:oZ;
					GetDynamicObjectPos(obb,oX, oY, oZ);
					if(IsPlayerInRangeOfPoint(playerid, 5, oX, oY, oZ))
					{
					    ArmyCrate[val] -= 1;
					    if(ArmyCrate[val] > 0)
					    {
							DestroyDynamicObject(obb);
							Delete3DTextLabel(armylabel[val]);
							SendClientMessage(playerid, -1, "You got some weapons from pack.");
							new rand = random(4);
							switch(rand)
							{
								case 0: GivePlayerWeapon(playerid, 29, 300);
								case 1: GivePlayerWeapon(playerid, 32, 300);
								case 2: GivePlayerWeapon(playerid, 37, 400);
								case 3: GivePlayerWeapon(playerid, 35, 1);
							}
							crateexists[val] = false;
							KillTimer(CrateDespawnTimer[val]);
							ArmyCrate[val] = -1;
							break;
	 					}
					}
				}
			}
		}
 	}
Reply
#2

You are using dynamic objects but are comparing for BULLET_HIT_TYPE_OBJECT. Dynamic objects are Player Objects, furthermore the ID is different so using OnPlayerWeaponShot is not the best thing to do.
You could get the Player Object ID of the Streamer Object and compare that with the hitid (and hittype with BULLET_HIT_TYPE_PLAYER_OBJECT respectively), but that is more work than is neccessary.

Use the Streamer callback "OnPlayerShootDynamicObject". It's called only for dynamic objects and you won't need to retrieve the Player Object ID.
Reply
#3

So, I just have to place this code in OnPlayerShootDynamicObject and will work fine, or i need to do more work ?
Reply
#4

Quote:
Originally Posted by Hunud
View Post
So, I just have to place this code in OnPlayerShootDynamicObject and will work fine, or i need to do more work ?
It should work. Of course you'll have to remove the hittype check, but that should be all.
Reply
#5

I tried that now but seems it doesne't work well. I was aiming with gun and shoot at random position and package dissapeared (i was not aiming in object), not even counting 50 health.

Code:
public OnPlayerShootDynamicObject(playerid, weaponid, STREAMER_TAG_OBJECT objectid, Float:x, Float:y, Float:z)
{
	for(new val = 0; val < MAX_CRATES; val++)
	{
		if(crateexists[val])
		{
		    new obb = ArmyCrate[val];
			if(IsValidDynamicObject(obb))
			{
				new Float:oX, Float:oY, Float:oZ;
				GetDynamicObjectPos(obb,oX, oY, oZ);
				if(IsPlayerInRangeOfPoint(playerid, 5, oX, oY, oZ))
				{
				    ArmyCrate[val] -= 1;
				    if(ArmyCrate[val] > 0)
				    {
						DestroyDynamicObject(obb);
						Delete3DTextLabel(armylabel[val]);
						SendClientMessage(playerid, -1, "You have picked up a weapon from the package");
						new rand = random(8);
						switch(rand)
						{
							case 0: GivePlayerWeapon(playerid, 16, 2);
							case 1: GivePlayerWeapon(playerid, 18, 2);
							case 2: GivePlayerWeapon(playerid, 26, 300);
							case 3: GivePlayerWeapon(playerid, 28, 300);
							case 4: GivePlayerWeapon(playerid, 29, 300);
							case 5: GivePlayerWeapon(playerid, 32, 300);
							case 6: GivePlayerWeapon(playerid, 37, 400);
							case 7: GivePlayerWeapon(playerid, 35, 1);
						}
						crateexists[val] = false;
						KillTimer(CrateDespawnTimer[val]);
						ArmyCrate[val] = -1;
						break;
 					}
				}
			}
		}
	}
 	return 1;
}
Reply
#6

Quote:
Originally Posted by Hunud
View Post
I tried that now but seems it doesne't work well. I was aiming with gun and shoot at random position and package dissapeared (i was not aiming in object), not even counting 50 health.

Code:
public OnPlayerShootDynamicObject(playerid, weaponid, STREAMER_TAG_OBJECT objectid, Float:x, Float:y, Float:z)
{
	for(new val = 0; val < MAX_CRATES; val++)
	{
		if(crateexists[val])
		{
		    new obb = ArmyCrate[val];
			if(IsValidDynamicObject(obb))
			{
				new Float:oX, Float:oY, Float:oZ;
				GetDynamicObjectPos(obb,oX, oY, oZ);
				if(IsPlayerInRangeOfPoint(playerid, 5, oX, oY, oZ))
				{
				    ArmyCrate[val] -= 1;
				    if(ArmyCrate[val] > 0)
				    {
						DestroyDynamicObject(obb);
						Delete3DTextLabel(armylabel[val]);
						SendClientMessage(playerid, -1, "You have picked up a weapon from the package");
						new rand = random(8);
						switch(rand)
						{
							case 0: GivePlayerWeapon(playerid, 16, 2);
							case 1: GivePlayerWeapon(playerid, 18, 2);
							case 2: GivePlayerWeapon(playerid, 26, 300);
							case 3: GivePlayerWeapon(playerid, 28, 300);
							case 4: GivePlayerWeapon(playerid, 29, 300);
							case 5: GivePlayerWeapon(playerid, 32, 300);
							case 6: GivePlayerWeapon(playerid, 37, 400);
							case 7: GivePlayerWeapon(playerid, 35, 1);
						}
						crateexists[val] = false;
						KillTimer(CrateDespawnTimer[val]);
						ArmyCrate[val] = -1;
						break;
 					}
				}
			}
		}
	}
 	return 1;
}
You didn't check if the crate was actually hit. Also you are mixing up a variable - you check if ArmyCrate[val] is a valid object, but it's holding the health of the crate later on?

You'll need to save the Object ID for the crate seperately and then, inside the loop, check if the objectid that was hit matches one of the crates you are looping through.

For example:

Code:
for(new val = 0; val < MAX_CRATES; val++)
{
if(crateexists[val] && CrateObjectID[val] == objectid)
{
// ...
}
}
If you already saved the Object ID in the ArmyCreate array, use that one instead of "crateobjectid" and make a new array for the crate's health.

I'd also suggest to rename crateexists to something like "ArmyCrateExists" or make an array with an enumerator. Using that many differnet arrays will get messy really soon.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)