Code: Select all
local color = { 255, 255, 255, 255 }
local function randCol()
color = { math.random( 0, 255 ), math.random( 0, 255 ), math.random( 0, 255 ), math.random( 0, 255 ) }
coroutine.yield()
end
local randomColor = coroutine.create( randCol )
for i, v in ipairs( color ) do print( i, v ) end
print()
coroutine.resume( randomColor )
for i, v in ipairs( color ) do print( i, v ) end
Code: Select all
local color = { 255, 255, 255, 255 }
local function randCol()
local color = { math.random( 0, 255 ), math.random( 0, 255 ), math.random( 0, 255 ), math.random( 0, 255 ) }
while true do
coroutine.yield( color )
end
end
local randomColor = setmetatable(
{ coroutine.create( randCol ) },
{
__call = function( tab, ... )
return select( 2, coroutine.resume( tab[1], ... ) )
end
}
)
local function printColor() for _, v in ipairs( color ) do print( v ) end print() end
printColor()
color = randomColor()
printColor()
color = randomColor()
printColor()