From array to function parameters
#1

I have an array that stores different points of an area

PHP код:
new Float:array[MAX_POINTS
Код:
for example
array[0] = 111.0
array[1] = 222.0
array[2] = 333.0
array[3] = 444.0
array[4] = 777.0
array[5] = 888.0
...
How can I insert every Float (!=0.0) into the parameters of a function.

PHP код:
Areas[map] = Area_AddPoly(array[0], array[1], array[2], array[3], ... ); // without doing this 
Reply
#2

There are few ways, if you have a lot of areas and need to do the same for every one for whatever reason, you can define it so you only need to type it once.

pawn Код:
#define IMLAZY array[0],array[1],array[2],array[3]...
pawn Код:
Areas[map] = Area_AddPoly(IMLAZY);
That's the only example I can give you without knowing the context on what you're using it.
Reply
#3

Quote:
Originally Posted by Troydere
Посмотреть сообщение
There are few ways, if you have a lot of areas and need to do the same for every one for whatever reason, you can define it so you only need to type it once.

pawn Код:
#define IMLAZY array[0],array[1],array[2],array[3]...
pawn Код:
Areas[map] = Area_AddPoly(IMLAZY);
That's the only example I can give you without knowing the context on what you're using it.
But it should only be inserted in the function params if the array != 0.
Isn't there a way to do this with a loop?
Reply
#4

Just feed the entire array into the function, what's the problem?
Код:
Area_AddPoly(Float:points[], size = sizeof points)
Reply
#5

I still don't understand. The function need multiple arguments, like this:
PHP код:
Areas[map] = Area_AddPoly(array[0], array[1], array[2], array[3]); //for example this has 4 arguments, but it can me more or less 
The problem is that the amount of vars in the array is never going to be the same. I need a way to put every var of the array as a param for the Area_AddPoly function
Reply
#6

You can't do what you're trying to do.
I don't know if you created the Area_AddPoly function. But what Vince said is more logical. Instead of add an unlimited number os args, transform the parameter in an array.
Reply
#7

You can theoretically do this with numargs() and getarg() quite easily, but I don't see the point at all.

https://sampforum.blast.hk/showthread.php?tid=77000

This structure generally only makes sense if you really cannot do it any other way.
That applies to (in theory) format, which needs to accept a varying amount of different variables and/or tags. This is not the case for your code, you only have one variable (array) which doesn't have to be split to multiple arguments.

Quote:
Originally Posted by PeanutButter
The problem is that the amount of vars in the array is never going to be the same (...)
You use an array, thus the "amount of vars" will in fact ALWAYS be known to you or the script pre-compile time. You can artificially decrease the array's size if this is your only problem. Alternatively you can just skip entries with the value 0.0 within your function.

This is different if your function should accept any Float arguments from a varying amount of arrays, but you mentioned just one array.

PS: If you already know about getarg and how to write such a function, it is not possible by design to insert a varying number of arguments run-time (passing a variable depending on its current value would be a run-time operation).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)