Destroying object
#1

I have a problem, i have a crate system where players near a spawned crate can get weapons or hp/armour.

Problem is, it should destroy the crate object after the player presses ESC on the dialog (to close it) or when a selects a weapon to get, but it doesn't happen.

Only textlabel gets removed, but crate object stays still there:

pawn Code:
case DIALOG_CRATE:
        {
        if(response)
        {
            if(listitem == 0) // Combat Shotgun.
            {
            GivePlayerWeapon(playerid, 27, 32);
                SendClientMessage(playerid, COLOR_WHITE,"* You have equiped a {FFA500}Combat Shotgun{FFFFFF}.");
                if(IsValidDynamicObject(CrateObject)) DestroyDynamicObject(CrateObject);
                DestroyDynamic3DTextLabel(Text3D:dynamiccrate3DText);
                CrateObject = 0;
            }
            if(listitem == 1) // MP5.
            {
                GivePlayerWeapon(playerid, 29, 32);
                SendClientMessage(playerid, COLOR_WHITE,"* You have equiped a {FFA500}MP5{FFFFFF}.");
                if(IsValidDynamicObject(CrateObject)) DestroyDynamicObject(CrateObject);
                DestroyDynamic3DTextLabel(Text3D:dynamiccrate3DText);
                CrateObject = 0;
            }
        }
        if(!response)
        {
            if(IsValidDynamicObject(CrateObject)) DestroyDynamicObject(CrateObject);
            DestroyDynamic3DTextLabel(Text3D:dynamiccrate3DText);
            CrateObject = 0;
            SendClientMessageTA(-1, "{FFA500}» [CRATE] The crate has been destroyed! Another will respawn soon.");
        }
        }
Reply
#2

How many objects do you create?
Reply
#3

Related to crate? Just 1.
Reply
#4

Can you show, how you create the object?
Reply
#5

why you repate same useless code so many times, build framwork for this system, at the moment this is useless garbage where you waste time adding something new.
Reply
#6

Quote:
Originally Posted by codExpert
View Post
why you repate same useless code so many times, build framwork for this system, at the moment this is useless garbage where you waste time adding something new.
Repeat useless code many times? Where?

@Kaliber:

pawn Code:
new CrateObject;
pawn Code:
new Float:g_CrateSpawns[][3] =
{
    {821.9344,-1868.2295,6.7148}
    {1281.3082,-2000.0718,58.2978},
    {1800.3622,-2180.2473,13.5547},
    {1647.9771,-2102.3044,13.5547}
};
In a timer (crate randomly spawns every 5 mins):

pawn Code:
CrateObject = CreateDynamicObject(CRATE_MODEL_ID, g_CrateSpawns[rand][0], g_CrateSpawns[rand][1], g_CrateSpawns[rand][2]-0.4, -1, -1, -1);
Reply
#7

Quote:
Originally Posted by SymonClash
View Post
Repeat useless code many times? Where?
He wanted to say, you could write it like this:

PHP Code:
case DIALOG_CRATE:
{
    if(
IsValidDynamicObject(CrateObject)) DestroyDynamicObject(CrateObject);
    
DestroyDynamic3DTextLabel(Text3D:dynamiccrate3DText);
    
CrateObject 0;
    if(!
response) return SendClientMessageTA(-1"{FFA500}» [CRATE] The crate has been destroyed! Another will respawn soon.");
    switch(
listitem)
    {
        case 
0GivePlayerWeapon(playerid2732), SendClientMessage(playeridCOLOR_WHITE,"* You have equiped a {FFA500}Combat Shotgun{FFFFFF}.");
        case 
1GivePlayerWeapon(playerid2932), SendClientMessage(playeridCOLOR_WHITE,"* You have equiped a {FFA500}MP5{FFFFFF}.");
    }

Anyway, i think your problem is, that you overwrite your CrateObject.

Because if you Create the new object the old isnt destroyed and gets overrided.

So, you can solve this, by an array of CrateObjects or by destroying the old one, before you create a new one


PS:
For constant arrays, use stock const:

PHP Code:
stock const Float:g_CrateSpawns[][3] =
{
    {
821.9344,-1868.2295,6.7148}
    {
1281.3082,-2000.0718,58.2978},
    {
1800.3622,-2180.2473,13.5547},
    {
1647.9771,-2102.3044,13.5547}
}; 
Reply
#8

I fixed, i added a bool to check if the object spawned, and now gets removed. Thank you.
Reply
#9

Quote:
Originally Posted by Kaliber
View Post
He wanted to say, you could write it like this:

PS:
For constant arrays, use stock const:

PHP Code:
stock const Float:g_CrateSpawns[][3] =
{
    {
821.9344,-1868.2295,6.7148}
    {
1281.3082,-2000.0718,58.2978},
    {
1800.3622,-2180.2473,13.5547},
    {
1647.9771,-2102.3044,13.5547}
}; 
what?

Stock only tells the compiler to ignore if not used, there's no reason to use stock for that
Reply
#10

Quote:
Originally Posted by Kasichok
View Post
what?

Stock only tells the compiler to ignore if not used, there's no reason to use stock for that
No, not in that specific case.

Actually you are right, but you can't declare arrays normally as const (globally).

static const, would be possible or new const, but not const alone.

You can try it out, you will get an error

So to fool the compiler, you can use stock const, then it will work
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)