Your Help.
#1

I'm making a roadblock command but facing some issues with deleting them.

Код:
enum PlayerBlocks
{
	pRoadblock,
}new Player[MAX_PLAYERS][PlayerBlocks];

CMD:roadblock(playerid,params[]) {
      new Float:Pos[4];
      GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);
      GetPlayerFacingAngle(playerid, Pos[3]);
      Pos[0] += (3 * floatsin(-Pos[3], degrees));
      Pos[1] += (3 * floatcos(-Pos[3], degrees));
      Player[playerid][PlayerBlocks] = CreateObject(978, Pos[0], Pos[1], Pos[2], 0.0, 0.0, Pos[3]);
      return 1;

CMD:removeall(playerid,params[])
{
  new Float:Pos[4];
  GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);
  if(IsPlayerInRangeOfPoint(playerid, 100, Pos[0], Pos[1], Pos[2]))
  {
    DestroyObject(PlayerInfo[playerid][PlayerBlocks]);
    
	}
	return 1;
}
Could someone tell me what i should do i was thinking of making a forloop but i wasn't sure of how to do the enum if it was right,thanks.

EDIT: i also wanted a command to remove the nearest roadblock to you but after your help i should know.
Reply
#2

So, here you go:

PHP код:
enum PlayerBlocks
{
    
pRoadblock
};
new 
Player[MAX_PLAYERS][PlayerBlocks];
CMD:roadblock(playerid,params[]) {
      if(
Player[playerid][PlayerBlocks]) return SendClientMessage(playerid,-1,"* You created a RoadBlock already!");
      new 
Float:Pos[4];
      
GetPlayerPos(playeridPos[0], Pos[1], Pos[2]);
      
GetPlayerFacingAngle(playeridPos[3]);
      
Pos[0] += (floatsin(-Pos[3], degrees));
      
Pos[1] += (floatcos(-Pos[3], degrees));
      
Player[playerid][PlayerBlocks] = CreateObject(978Pos[0], Pos[1], Pos[2], 0.00.0Pos[3]);
      return 
1;
}
CMD:removeall(playerid,params[])
{
    for(new 
ii<sizeof(Player); i++)
    {
        if(
Player[i][PlayerBlocks]) DestroyObject(PlayerInfo[i][PlayerBlocks]),PlayerInfo[i][PlayerBlocks]=0;
    }
    return 
1;
}
CMD:removeclosest(playerid,params[])
{
    new 
max 9999,Float:dis=0,index=-1;
    for(new 
i,Float:x,Float:y,Float:zi<sizeof(Player); i++)
    {
        if(
Player[i][PlayerBlocks])
        {
            
GetObjectPos(Player[i][PlayerBlocks], x,y,z);
            
dis GetPlayerDistanceFromPoint(playeridx,y,z);
            if(
dis maxmax=dis,index=i;
        }
    }
    if(
index == -1) return SendClientMessage(playerid,-1,"* No Roadblock was created!");
    if(
dis 50) return SendClientMessage(playerid,-1,"* You're too fare away from the closest roadblock!");
    
DestroyObject(Player[index][PlayerBlocks]);
    
Player[index][PlayerBlocks] = 0;
    return 
1;

Reply
#3

array index out of bounds (variable "Player")

Код:
 Player[playerid][PlayerBlocks] = CreateObject(...);
Reply
#4

Yes...sry copied your code..this is right:

PHP код:
enum PlayerBlocks 

    
pRoadblock 
}; 
new 
Player[MAX_PLAYERS][PlayerBlocks]; 
CMD:roadblock(playerid,params[]) { 
      if(
Player[playerid][pRoadblock]) return SendClientMessage(playerid,-1,"* You created a RoadBlock already!"); 
      new 
Float:Pos[4]; 
      
GetPlayerPos(playeridPos[0], Pos[1], Pos[2]); 
      
GetPlayerFacingAngle(playeridPos[3]); 
      
Pos[0] += (floatsin(-Pos[3], degrees)); 
      
Pos[1] += (floatcos(-Pos[3], degrees)); 
      
Player[playerid][pRoadblock] = CreateObject(978Pos[0], Pos[1], Pos[2], 0.00.0Pos[3]); 
      return 
1

CMD:removeall(playerid,params[]) 

    for(new 
ii<sizeof(Player); i++) 
    { 
        if(
Player[i][pRoadblock]) DestroyObject(PlayerInfo[i][pRoadblock]),PlayerInfo[i][pRoadblock]=0
    } 
    return 
1

CMD:removeclosest(playerid,params[]) 

    new 
max 9999,Float:dis=0,index=-1
    for(new 
i,Float:x,Float:y,Float:zi<sizeof(Player); i++) 
    { 
        if(
Player[i][pRoadblock]) 
        { 
            
GetObjectPos(Player[i][pRoadblock], x,y,z); 
            
dis GetPlayerDistanceFromPoint(playeridx,y,z); 
            if(
dis maxmax=dis,index=i
        } 
    } 
    if(
index == -1) return SendClientMessage(playerid,-1,"* No Roadblock was created!"); 
    if(
dis 50) return SendClientMessage(playerid,-1,"* You're too fare away from the closest roadblock!"); 
    
DestroyObject(Player[index][pRoadblock]); 
    
Player[index][pRoadblock] = 0
    return 
1

Reply
#5

Same Problem + Tag Mismatch.
Reply
#6

How many roadblocks is a single player meant to be able to place? Your code only supports 1 roadblock at a time.
Reply
#7

Quote:
Originally Posted by Threshold
Посмотреть сообщение
How many roadblocks is a single player meant to be able to place? Your code only supports 1 roadblock at a time.
That makes sense,max is 20,its for cops in my rp server. so i should do something like this then?

Код:
#define MAX_ROADBLOCKS 20
then make the enum with max roadblocks instead of player?

EDIT: also while creating one with the provided code i still get the warning.
Reply
#8

Код:
  new Float:Pos[4];
  GetPlayerPos(playerid, Pos[0], Pos[1], Pos[2]);
  if(IsPlayerInRangeOfPoint(playerid, 100, Pos[0], Pos[1], Pos[2]))
You have checked the player's position and then checked if hes around of his current position?
I think you meant to check if hes nearby the roadblock, am I right? If yes, make a global variable to save the roadblock's position and then compare it using IsPlayerInRangeOfPoint.
Reply
#9

I'm not normally this nice, but because it's a personal thing...
PHP код:
#define MAX_ROADBLOCKS  20 // MAX_ROADBLOCKS will be set to 20 by default. This means that a player can only place up to 20 roadblocks at a time.
enum PlayerBlocks
{
    
pRoadblock[MAX_ROADBLOCKS], // Creates an array that will hold the object IDs for 20 roadblocks. This number depends on your MAX_ROADBLOCKS.
}
new 
Player[MAX_PLAYERS][PlayerBlocks];
CMD:roadblock(playeridparams[]) { // Command /roadblock
    
new free_rb_id 0// Set the initial object ID to '0'. Object IDs start at 1, so 0 is considered an invalid object ID.
    
for(new 0MAX_ROADBLOCKSi++) { // Start looping through all 20 roadblocks that could potentially be placed by the player.
        
if(!Player[playerid][pRoadblock][i]) { // If the roadblock is '0', a.k.a. does not exist, then we have an available slot.
            
free_rb_id i// Store the slot/index as our 'free roadblock ID'.
            
break; // Break the loop to stop it from continuing, because we have already found a valid slot.
        
}
    }
    if(!
free_rb_id) return SendClientMessage(playerid, -1"You cannot place any more roadblocks. Please remove one first.");
    
// If 'free_rb_id' is still 0, it means that the value was never changed in the loop. This means that every roadblock slot was being used.
    
new Float:Pos[4];
    
GetPlayerPos(playeridPos[0], Pos[1], Pos[2]);
    
GetPlayerFacingAngle(playeridPos[3]);
    
Pos[0] += (floatsin(-Pos[3], degrees));
    
Pos[1] += (floatcos(-Pos[3], degrees));
    
Player[playerid][pRoadblock][free_rb_id] = CreateObject(978Pos[0], Pos[1], Pos[2], 0.00.0Pos[3]);
    
// Store the created object's ID into the available slot.
    
return 1;
}
CMD:removeall(playeridparams[]) { // Command /removeall
    
new Float:obxFloat:obyFloat:obz// Create floats to store the roadblock's position.
    
for(new 0MAX_ROADBLOCKSi++) { // Start looping through all 20 roadblocks that the placed could have placed.
        
if(!Player[playerid][pRoadblock][i]) continue; // If the roadblock does not exist, move on to the next slot.
        
GetObjectPos(Player[playerid][pRoadblock][i], obxobyobz); // The roadblock exists, so we need to get its position.
        
if(!IsPlayerInRangeOfPoint(playerid100.0obxobyobz)) continue; // If the player is not within 100.0 units of the roadblock, ignore it. It's too far away.
        
DestroyObject(Player[playerid][pRoadblock][i]); // The player is within 100.0 units of the object, meaning it can now be destroyed.
        
Player[playerid][pRoadblock][i] = 0// Reset the variable, so we can use this slot in the future.
    
}
    return 
1;
}
CMD:removerb(playeridparams[]) { // Command /removerb
    
new Float:obxFloat:obyFloat:obzFloat:dist 100.0Float:temp 100.0rb_to_del = -1;
    
// These variables store the object's position, a starting distance and a temporary distance, and the ID of the roadblock we should be removing.
    
for(new 0MAX_ROADBLOCKSi++) { // Start looping through all possible 20 roadblocks.
        
if(!Player[playerid][pRoadblock][i]) continue; // If the roadblock doesn't exist, ignore it and continue to the next one.
        
GetObjectPos(Player[playerid][pRoadblock][i], obxobyobz); // Store the object's position.
        
if((dist GetPlayerDistanceFromPoint(playeridobxobyobz)) <= temp) { // Now we check if the distance between the player and the roadblock is less than the temporary value. The value of 'GetPlayerDistanceFromPoint' is stored in 'dist'.
            // Our temporary value starts at 100.0, so the player still cannot remove the roadblock if they are too far away from it.
            
temp dist// Update our new temporary value to the distance, as this distance is shorter and this roadblock is closer than the previous one.
            
rb_to_del i// This is now our closest roadblock, so we store the roadblock's ID for later.
            
continue; // Continue on to the next roadblock, to see if it is even closer.
        
}
    }
    if(
rb_to_del == -1) return SendClientMessage(playerid, -1"You are not near any placed roadblocks."); // If rb_to_del is still -1, it means that either no roadblocks are placed, or there are no roadblocks within 100.0 units of the player.
    
DestroyObject(Player[playerid][pRoadblock][rb_to_del]); // Destroy our stored roadblock ID. This will remove the closest roadblock to us.
    
Player[playerid][pRoadblock][rb_to_del] = 0// Reset the variable, so we can use the slot later.
    
return 1;

Note, when comparing floats, you can also use the function Floatcmp.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)