PDA

View Full Version : Despawning object.


Sam5513
24/08/2012, 04:42 PM
Hey guys.
Recently I was trying to develop a script for a server, by which if you would stand on a place, a siren would spawn and once you would get out, it would despawn.
I have been trying to work it out the whole day now but whenever I exit the place, the sirens dont despawn.
I'd really appreciate if you could help me please. Thank you.
Here is so far what I have done:


public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 0.1, 1207.0170, -1333.9816, 13.3984))
{
CreateObject(18646, 1207.5631, -1334.1051, 13.5816);
CreateObject(18646, 1206.5031, -1334.1051, 13.5816);
}
if(!IsPlayerInRangeOfPoint(playerid, 0.1, 1207.0170, -1333.9816, 13.3984))
{
if(IsValidObject(18646, 1207.5631, -1334.1051, 13.5816))
{
DestroyObject(18646, 1207.5631, -1334.1051, 13.5816);
DestroyObject(18646, 1206.5031, -1334.1051, 13.5816);
}
}

return 1;
}

RedJohn
24/08/2012, 05:10 PM
public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 0.1, 1207.0170, -1333.9816, 13.3984))
{
CreateObject(18646, 1207.5631, -1334.1051, 13.5816);
CreateObject(18646, 1206.5031, -1334.1051, 13.5816);
}
else
{
if(IsValidObject(18646, 1207.5631, -1334.1051, 13.5816))
{
DestroyObject(18646);
}
}
return 1;
}

Note: Untested

Sam5513
24/08/2012, 05:23 PM
Thank you for your effort but it doesnt work.
The objects still stay there when exiting the area.
Besides I noticed you were trying to remove only one object, and not both of them.
Thank you for your effort though.

HuSs3n
24/08/2012, 05:28 PM
new a,b;
public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 1.0, 1207.0170, -1333.9816, 13.3984))
{
if(!IsValidObject(a)) a=CreateObject(18646, 1207.5631, -1334.1051, 13.5816);
if(!IsValidObject(b)) b=CreateObject(18646, 1206.5031, -1334.1051, 13.5816);
}
else
{
if(IsValidObject(a)) DestroyObject(a);
if(IsValidObject(b)) DestroyObject(b);
}
return 1;
}

this is just a fix for your code
if ANY player IS NOT near that point ,it will destroy the object for all.

Shetch
24/08/2012, 05:31 PM
Try this.

new bool:SirenCreated;
new obj1;
new obj2;

public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 0.1, 1207.0170, -1333.9816, 13.3984) && SirenCreated == false)
{
SirenCreated = true;
obj1 = CreateObject(18646, 1207.5631, -1334.1051, 13.5816, 0.0, 0.0, 0.0);
obj2 = CreateObject(18646, 1206.5031, -1334.1051, 13.5816, 0.0, 0.0, 0.0);
}
if(!IsPlayerInRangeOfPoint(playerid, 0.1, 1207.0170, -1333.9816, 13.3984) && SirenCreated == true)
{
SirenCreated = false;
DestroyObject(obj1);
DestroyObject(obj2);
}
return 1;
}

Sam5513
24/08/2012, 05:40 PM
new a,b;
public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 1.0, 1207.0170, -1333.9816, 13.3984))
{
if(!IsValidObject(a)) a=CreateObject(18646, 1207.5631, -1334.1051, 13.5816);
if(!IsValidObject(b)) b=CreateObject(18646, 1206.5031, -1334.1051, 13.5816);
}
else
{
if(IsValidObject(a)) DestroyObject(a);
if(IsValidObject(b)) DestroyObject(b);
}
return 1;
}

this is just a fix for your code
if ANY player IS NOT near that point ,it will destroy the object for all.

Doesnt work. Still is spawned when leaving the area.

Sam5513
24/08/2012, 05:42 PM
Try this.

new bool:SirenCreated;
new obj1;
new obj2;

public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 0.1, 1207.0170, -1333.9816, 13.3984) && SirenCreated == false)
{
SirenCreated = true;
obj1 = CreateObject(18646, 1207.5631, -1334.1051, 13.5816, 0.0, 0.0, 0.0);
obj2 = CreateObject(18646, 1206.5031, -1334.1051, 13.5816, 0.0, 0.0, 0.0);
}
if(!IsPlayerInRangeOfPoint(playerid, 0.1, 1207.0170, -1333.9816, 13.3984) && SirenCreated == true)
{
SirenCreated = false;
DestroyObject(obj1);
DestroyObject(obj2);
}
return 1;
}
Works perfectly! Thank you very much!
I have one more question though. Do you think you would know a way to make it shine with all those colors like some unmarked cruisers have on rp servers? It would be great.


EDIT: I found out I only have to set the weather to 2 for the siren flashing to work. Thanks anyways for all who helped and all who were trying to help! Appreciate it.

Sam5513
24/08/2012, 07:17 PM
With help of Schetch, we have eddited the script so that the sirens will spawn only when player is armed, although there is a bug. The sirens will go only on if the armed player has the weapon in hand. If the weapon is on him but the weapon is not in his hand, the sirens will not go on.
I could really use some more help, please.
Current code:

new bool:SirenCreated;
new obj1;
new obj2;

public OnPlayerUpdate(playerid)
{
if(IsPlayerInRangeOfPoint(playerid, 0.3, 1207.0170, -1333.9816, 13.3984) && SirenCreated == false && GetPlayerWeapon(playerid) > 0)
{
SirenCreated = true;
obj1 = CreateObject(18646, 1207.5631, -1334.1051, 13.5816, 0.0, 0.0, 0.0);
obj2 = CreateObject(18646, 1206.5031, -1334.1051, 13.5816, 0.0, 0.0, 0.0);
}
if(!IsPlayerInRangeOfPoint(playerid, 0.3, 1207.0170, -1333.9816, 13.3984) && SirenCreated == true)
{
SirenCreated = false;
DestroyObject(obj1);
DestroyObject(obj2);
}
return 1;
}