I recall posting about this earlier, but could someone please point me in the direction of the For In World page thing in the gmod wiki? THank you, I'd find it myself but, "Searching is Disabled". :/
You mean this? [url]http://wiki.garrysmod.com/?title=Using_the_for_in_loop[/url]
Nope, won't work. Need something with GetAll.
What are you trying to count? If you post exactly what you are trying to use the for loop for I can probably help you
I'm trying to make it so that it cycles through all the entities and when the entities with a certain var are found they are warped.
[lua]for k, v in pairs( ents.GetAll() ) do
--blah blah
end[/lua]
That's looping through all the ents on the current map, you can then use it to cycle out others.
I don't understand quite how, could you please explain it to me if it's not that much of a problem?
You want me to explain the for loop to you, or how to cycle out entities using the for loop?
Both. Thank you though for your help.
[lua]for k, v in pairs[/lua]
This starts the loop, the first value, shown above as "k", is the key, and the second value shown above as "v" is the value, these will be used to loop through a table, defined within the brackets "pairs" means both will work subsequently together, and loop through the defined table.
[lua]for k, v in pairs( ents.GetAll() )[/lua]
We have added the table for the for loop to loop through, in this case, it's looping through EVERY entitiy on the map, which includes props both physics and static.
Below is a list of available tables to use in a for loop:
[code]
Entity Loops:
-------------
ents.FindByModel
ents.FindByName
ents.FindByClass
ents.FindInBox
ents.FindInCone
ents.FindInSphere
ents.GetAll
ents.GetByIndex
Player Loops:
-------------
player.GetAll
player.GetByID
player.GetByUniqueID
player.GetBots
player.GetHumans[/code]
You can also use custom tables, and then using them in loops themselves;
[lua]myTable = {}
myTable[1] = "This"
myTable[2] = "is"
myTable[3] = "a"
myTable[4] = "table"
for k, v in pairs( myTable ) do
--Stuff
end[/lua]
Now, we tell it to do something:
[lua]for k, v in pairs( ents.GetAll() ) do --The "do" makes it do the following code
if !v:IsPlayer() then --If ANY entity is NOT a player then...
v:Remove() --Remove said entity
end --End if statement
end --End for loop[/lua]
When using for loops, whatever you defined as the second value, shown above is "v", can be used to define each object this loop finds, so when I do:
[lua]v:Remove()[/lua]
I'm removing EVERY entity it finds, however, before that, I check if the entity is a player or not, to do this I do
[lua]if !v:IsPlayer() then[/lua]
This literally means [I]If whatever the loop finds is NOT a player, then do the following:[/I].
The symbol ! means [I]not[/I] when used in this way, it is most commonly used in C Scripting language, but has been inserted into lua for use as well.
After this if function, we can do whatever we want to whatever the for loop has found, we can Remove, SetOwner, SetPos, you name it, whatever functions are available for use on an entity, you can do it. But it will apply to every entity within this table, unless you do an if statement, stating the entity's you do, or do not want the following code to apply to, as shown above.
So, to remove EVERY entity that isn't a player, we have to do this AFTER the if function:
[lua]v:Remove()[/lua]
This will remove the filtered objects.
The for loop is simple to understand, but quite hard to explain, I hope this explanation helps.
Finished Example of a for loop, which removes non-player entities:
[lua]for k, v in pairs( ents.GetAll() ) do --The "do" makes it do the following code
if !v:IsPlayer() then --If ANY entity is NOT a player then...
v:Remove() --Remove said entity
end --End if statement
end --End for loop[/lua]
I also add that it does not have to be k and v. It could be q and s etc.
[QUOTE=Ronon Dex;24159573]I also add that it does not have to be k and v. It could be q and s etc.[/QUOTE]
Yeah, it culd be anything, as long as there are two values seperated by a comma.
[QUOTE=LuckyLuke;24159617]Yeah, it culd be anything, as long as there are two values seperated by a comma.[/QUOTE]
If you do not need the key, I normally do, for example:
[lua] for _,v in pairs(player.GetAll()) do[/lua]
Sorry, you need to Log In to post a reply to this thread.