foreach
#1

Hey i'm new to foreach and i want to know how to convert this
Code:
for(new h = 0; h <= MAX_HOUSES; h++)
to foreach?
Reply
#2

Yes,...

pawn Code:
for(new h = 0; h <= MAX_HOUSES; h++)
{
/* HERE YOUR CODE */
}
Reply
#3

Quote:
Originally Posted by DraKoN
View Post
Yes,...

pawn Code:
for(new h = 0; h <= MAX_HOUSES; h++)
{
/* HERE YOUR CODE */
}
man, i want to know how to convert the same code to foreach -.-

this... this one?
http://forum.sa-mp.com/showthread.ph...hlight=foreach
Reply
#4

Yes.

Code:
new i;
foreach (Player, i)
{    
printf("Player %d is connected", i);
}
Reply
#5

In the global vars section of your game mode (where MAX_HOUSES is defined) add

pawn Code:
Itter_Create(Houses,MAX_HOUSES);
As a house is created, you need to add it to the Houses itter by doing:
pawn Code:
Itter_Add(Houses,houseid);
House ID of course being whatever you are using to identify the house ID (that is purely an example, make sure you have the houseid var defined if you use it that way).

If you delete or remove a house you must do:
pawn Code:
Itter_Remove(Houses,houseid);
You do not want a house id (or id # for that matter) to double-up in a foreach itter. I speak from experience here. Your server will hit an endless loop and go to 99.9% cpu use.

When you want to loop that Houses itter you would do:
pawn Code:
foreach(Houses,i)
{
//Insert code here
}
Hope that helps. I can expound further on 2D itters though I don't think you'll need that here. 2D itters can be a monster to deal with.
Reply
#6

Sorry for the late reply, but that actually worked thank you very much for explaining very detailed

EDIT:
The problem is now with loading the houses,
Code:
Iter_Create(Houses, MAX_HOUSES);
Code:
    foreach(Houses,h)
    {
        LoadPlayerHouse(h);
    }
AND WHEN IT DOES "LoadPlayerHouse"
Code:
Itter_Add(Houses,houseid);
and if i try
[code]
Code:
    foreach(Houses,h)
    {
        LoadPlayerHouse(h);
        print("loaded");
    }
IT DOES NOTHING!
Reply
#7

anyone? BUMP
Reply
#8

Quote:
Originally Posted by armyoftwo
View Post
Sorry for the late reply, but that actually worked thank you very much for explaining very detailed

EDIT:
The problem is now with loading the houses,
Code:
Iter_Create(Houses, MAX_HOUSES);
Itter_Create.

Quote:
Code:
    foreach(Houses,h)
    {
        LoadPlayerHouse(h);
    }
AND WHEN IT DOES "LoadPlayerHouse"
Code:
Itter_Add(Houses,houseid);
You can't cycle an itter via foreach if the itter lacks any information to begin with. You need to add to an itter via Itter_Add. To cycle those contents you can then use foreach(Houses,h). You are basically cycling an itter that has nothing in it.
Reply
#9

Quote:
Originally Posted by nemesis-
View Post
Itter_Create.



You can\'t cycle an itter via foreach if the itter lacks any information to begin with. You need to add to an itter via Itter_Add. To cycle those contents you can then use foreach(Houses,h). You are basically cycling an itter that has nothing in it.
i have been trying to do this for 30 mins but i think that i still don\'t get it
Reply
#10

I haven't got into of how foreach works, but I think itter_add must be used before you use it in a foreach loop. When you are running your foreach loop, the itter is empty so the houses arent loaded.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)