If whole array?
#1

Hi guys.

I have a array with e.g. 10 different objects and I want to detect if is player near one of them.

I used cycle for:
Код:
for(new i = 0; i < sizeof(myObjArray); i++)
{
     if(IsPlayerNearObject(playerid, myObjArray[i]))
    {
      ...
    }
}
but it doesn't work. I tried a lot of other ways, but just one of them worked, this one ->

Код:
if(IsPlayerNearObject(playerid, myObjArray[0]) || 
IsPlayerNearObject(playerid, myObjArray[1]) ||
IsPlayerNearObject(playerid, myObjArray[2]) ||
IsPlayerNearObject(playerid, myObjArray[3]) ||
IsPlayerNearObject(playerid, myObjArray[4]) ||
IsPlayerNearObject(playerid, myObjArray[5]) ||
IsPlayerNearObject(playerid, myObjArray[6]) ||
IsPlayerNearObject(playerid, myObjArray[7]) ||
IsPlayerNearObject(playerid, myObjArray[8]) ||
IsPlayerNearObject(playerid, myObjArray[9]))
{
...
}
and It's not very good solution.

Do you have any suggestions?
Thank you.
Reply
#2

Show IsPlayerNearObject, loop is best for this, its streamer object or normal?
Reply
#3

Its my own function, just returns true or false

Код:
stock IsPlayerNearObject(playerid, objectid, Float:range = 2.0)
{
	new Float:X, Float:Y, Float:Z;
	GetDynamicObjectPos(objectid, X, Y, Z);
	if(IsPlayerInRangeOfPoint(playerid, range, X, Y, Z)) return 1;
	else
		return 0;
}
Reply
#4

You can use simply

pawn Код:
stock IsPlayerNearObject(playerid, objectid, Float:range = 2.0)
{
    new Float:X, Float:Y, Float:Z;
    GetDynamicObjectPos(objectid, X, Y, Z);
    return IsPlayerInRangeOfPoint(playerid, range, X, Y, Z);
}
and whats is wrong with this?

pawn Код:
for(new i = 0; i < sizeof(myObjArray); i++)
{
    if(IsPlayerNearObject(playerid, myObjArray[i]))
    {
        ...
    }
}
if nothing near so problem is in range
Reply
#5

So why this doesn't work?

Код:
obj[0] = CreateDynamicObject(id, X, Y, Z, 0, 0, 0);
obj[1] = CreateDynamicObject(id, X, Y, Z, 0, 0, 0);
obj[2] = CreateDynamicObject(id, X, Y, Z, 0, 0, 0);

	
for(new i = 0; i < sizeof(obj); i++)
{
	if(IsPlayerNearObject(playerid, obj[i], 2.0))
	{
		ShowTDActivity(playerid, "Something");
		activityTimer[playerid] = SetTimerEx("Something", 1000, true, "i", playerid);
	}
}
Reply
#6

You might want to break out of that loop using "break" if you find your first match (I assumed that from your || in if's).
Reply
#7

Quote:
Originally Posted by Professional_Programmer
Посмотреть сообщение
So why this doesn't work?

Код:
obj[0] = CreateDynamicObject(id, X, Y, Z, 0, 0, 0);
obj[1] = CreateDynamicObject(id, X, Y, Z, 0, 0, 0);
obj[2] = CreateDynamicObject(id, X, Y, Z, 0, 0, 0);

	
for(new i = 0; i < sizeof(obj); i++)
{
	if(IsPlayerNearObject(playerid, obj[i], 2.0))
	{
		ShowTDActivity(playerid, "Something");
		activityTimer[playerid] = SetTimerEx("Something", 1000, true, "i", playerid);
	}
}
because player is not in range 2.0 ?
Reply
#8

Misiur, It works. Thank you.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)