foreach[NOT SOLVED] -
armyoftwo - 05.07.2010
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?
Re: foreach -
ipsBruno - 05.07.2010
Yes,...
pawn Code:
for(new h = 0; h <= MAX_HOUSES; h++)
{
/* HERE YOUR CODE */
}
Re: foreach -
armyoftwo - 05.07.2010
Quote:
Originally Posted by DraKoN
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
Re: foreach -
DJDhan - 05.07.2010
Yes.
Code:
new i;
foreach (Player, i)
{
printf("Player %d is connected", i);
}
Re: foreach -
nemesis- - 06.07.2010
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.
Re: foreach -
armyoftwo - 10.07.2010
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!
Re: foreach -
armyoftwo - 11.07.2010
anyone? BUMP
Re: foreach -
nemesis- - 12.07.2010
Quote:
Originally Posted by armyoftwo
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);
|
I
tter_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.
Re: foreach -
armyoftwo - 13.07.2010
Quote:
Originally Posted by nemesis-
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
Re: foreach -
Macluawn - 20.01.2011
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.