coroutine/create

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (Also menu state)
(Added an example: display 1 player's position on each frame)
Line 1: Line 1:
 
{{Func
 
{{Func
|Name=create
 
|Parent=coroutine
 
 
|Description=Creates a coroutine of the given function.
 
|Description=Creates a coroutine of the given function.
 
|Realm=Shared and Menu
 
|Realm=Shared and Menu
Line 14: Line 12:
 
|type=thread
 
|type=thread
 
|desc=coroutine
 
|desc=coroutine
 +
}}
 +
{{Example
 +
|Description=Display the location of each player in an endless loop, but only one player per frame.
 +
|Code=do
 +
local function displayer()
 +
local players
 +
while true do -- endless loop: you must guarantee that coroutine.yield() is always called!
 +
players = player.GetAll()
 +
number_real = 0
 +
if not next( players ) then -- empty table
 +
coroutine.yield() -- guarantee a pause in coroutine even with an empty table
 +
else
 +
for _, ply in pairs( players ) do
 +
coroutine.yield() -- We yield here so what you may do next will start immediatly when this for loop finishes.
 +
if IsValid( ply ) then -- The player ply may be disconnected now!
 +
print( ply:Nick(), "is located at", ply:GetPos() )
 +
end
 +
end
 +
 +
end
 +
end
 +
end
 +
 +
local co
 +
hook.Add( "Think", "DisplayPlayersLocation", function()
 +
if not co or not coroutine.resume( co ) then
 +
co = coroutine.create( displayer )
 +
coroutine.resume( co )
 +
end
 +
end )
 +
end
 +
|Output=Custom Nickname is located at 10.102 59.04 -100.96
 +
SuperBoss is located at 55.85 1209.11 -100.96
 +
Custom Nickname is located at 11.126 51.92 -100.96
 +
...
 
}}
 
}}

Revision as of 14:31, 7 January 2017

 coroutine.create( )

Contents

Description

Creates a coroutine of the given function.

Arguments

function func

The function for the coroutine to use

Returns

thread

coroutine

Examples

Example

Display the location of each player in an endless loop, but only one player per frame.

do
	local function displayer()
		local players
		while true do -- endless loop: you must guarantee that coroutine.yield() is always called!
			players = player.GetAll()
			number_real = 0
			if not next( players ) then -- empty table
				coroutine.yield() -- guarantee a pause in coroutine even with an empty table
			else
				for _, ply in pairs( players ) do
					coroutine.yield() -- We yield here so what you may do next will start immediatly when this for loop finishes.
					if IsValid( ply ) then -- The player ply may be disconnected now!
						print( ply:Nick(), "is located at", ply:GetPos() )
					end
				end
				
			end
		end
	end
	
	local co
	hook.Add( "Think", "DisplayPlayersLocation", function()
		if not co or not coroutine.resume( co ) then
			co = coroutine.create( displayer )
			coroutine.resume( co )
		end
	end )
end

Output:

Custom Nickname is located at 10.102 59.04 -100.96

SuperBoss is located at 55.85 1209.11 -100.96 Custom Nickname is located at 11.126 51.92 -100.96

...
Personal tools
Navigation