How to create a custom array (I don't know if I got that right)
#1

What I meant is how to create a custom limit, like this:
PHP код:
#define MAX_ARROWS
enum Arrowinf
{
    
arrow,
    
Float:Posx,
    
Float:Posy,
    
Float:Posz,
    
Float:Rotat,
}
new 
ArrowInfo[MAX_ARROWS][Arrowinf]; 
But now how do I process the arrow IDs so it will start counting.

For example, in a command: /setarrow, how do I count the id of the arrows on every arrow set.
Like, I create an arrow, it becomes arrow id 0, then I create another one it becomes id 1 and so on and so forth.

I hope I am understood.
Reply
#2

Each Entry (Slot) should have a variable (in the enum) to tell whether that slot exists or not, preferably with bool tag.

Something like "ArrowExists" or "ArrowUsed".

When a arrow is created, you need to loop through the array and once a free slot is found (ArrowExists == false) that Slot can be used for the new arrow and the variable is set to true.

That way you can have for example 5 arrows, delete slot 2 and the next one that will be created will take slot 2.

That's basically how all dynamic Systems should be designed where each slot can be created and destroyed at any time.


I would also suggest a slightly different naming convention for enums. The names "Posx" etc are not very good because they are too general. There can be other enums that have a variable named "Posx".
I'd give them a prefix depending on what they are for, for example:

arrowUsed,
arrowX,
arrowY,
arrowZ,
arrowRZ

So that it is always clear what they are for and to avoid conflicts.
Reply
#3

So basically, when I do the command, I have to loop through all the arrow ids, like so:
PHP код:
for(new 0MAX_ARROWSi++)
{
     if(
ArrowInfo[i][ArrowUsed] == false)
    {
         
//Now do my command?
    
}

I don't exactly understand your point, mind if you demonstrate it in an example code?

Or should I just create a stock for it, would this work?:
PHP код:
stock GetEmptyArrowID()
{
       new 
id;
       for(new 
0MAX_ARROWSi++)
       {
              if(
ArrowInfo[i][ArrowUsed] == false)
              {
                    
id i;
                    break;
              }
       }
       return 
id;

Reply
#4

Quote:
Originally Posted by PizzaMag
Посмотреть сообщение
So basically, when I do the command, I have to loop through all the arrow ids, like so:
PHP код:
for(new 0MAX_ARROWSi++)
{
     if(
ArrowInfo[i][ArrowUsed] == false)
    {
         
//Now do my command?
    
}

I don't exactly understand your point, mind if you demonstrate it in an example code?
Your code is actually almost correct. Just a small thing - you need to stop the loop once a free slot is found (otherwise it will fill ALL empty slots with the new one).

You can do this by adding a break at the end of the if:

PHP код:
for(new 0MAX_ARROWSi++)
{
     if(
ArrowInfo[i][ArrowUsed] == false)
    {
         
ArrowInfo[i][ArrowUsed] = true;
         
// Your code
         
break; // Stop the loop
    
}

I personally do it a slight different way though, to know if there is a free slot or not (at least if the creating happens in a function with return value):

PHP код:
CreateArrow(...) // example function
{
    new 
slot = -1;
    for(new 
0MAX_ARROWS++) if(!ArrowInfo[i][ArrowUsed])
    {
        
slot i;
        break;
    }
    if(
slot == -1) return -1// Returns -1 if no free Slot was found
    // Code
    
return slot// Returns the ID if a free Slot was found

About the GetEmptyArrow function, you can do that too. I mostly don't do that as there is probably just one place where it is needed (when creating), but if you need it in more places it's a good idea to do it that way.
You should however set "id" to -1 initially, otherwise it will return 0 when there is no slot found (0 would also be a possible Array Index).
Take a look at the code above, there you can see how to determine if a slot was found or not
Reply
#5

Alright, thank you so much for the quick help, I really appreciate it

Rep+ ^^
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)