-- Runs a coroutine in a protected state, if available
function safeYield()
-- make sure we're not trying to yield in the main thread
-- do nothing.
local co, main = coroutine.running();
if( co == nil or main ) then
return;
end
--if( cocoAvailable ) then
local status, err = pcall(coroutine.yield);
if( status ~= true ) then
setTextColor(cli.yellow);
error(err, 3);
end
--[[else
coroutine.yield();
end]]
end
That now causes a proper error message including script and line numbers. Makes it easier to track down. It also ignores the yield when ran from the (back-end) main thread.
rock5 wrote:os.clock() has increments of about 1ms. It returns values in seconds and fractions of a second. If you need even more accuracy you can use getTime(). getTime() returns a table, though, so you have to use another function deltaTime() to return the difference between 2 getTimes(). So you would use it like this
I believe this returns values in ms and fractions of ms.
A bit late, but thank you rock for that info.
Some question regarding the new MM:
I was wondering if it would be possible to get a stack trace, if an error happens?
Is there some kind of reflection support available in LUA? Esp, can you get the name of a function during runtime, maybe the context/module where a function has been defined?
Jandrana wrote:
Some question regarding the new MM:
I was wondering if it would be possible to get a stack trace, if an error happens?
A stack trace has always been available in log.txt. You can also use the debug.traceback() function if you need.
Is there some kind of reflection support available in LUA? Esp, can you get the name of a function during runtime, maybe the context/module where a function has been defined?
local function myFunc()
print(debug.getinfo(coroutine.running(), "n").name);
end
Telling where a function has been defined probably won't work so well. Lua is too dynamic for that.
Thank you for your that information. That looks cool. Seems I still need to learn more about the built in abilities of LUA.
Administrator wrote:
Is there some kind of reflection support available in LUA? Esp, can you get the name of a function during runtime, maybe the context/module where a function has been defined?
What do you mean by reflection support?
Reflection means the ability of a programming language to provide functions / methods to "talk" about itself (the objects that make up the language itself)
Examples:
- ask an object about it's class
- ask a function about it's name
- ask anonymous functions about their context of definition
- ask the system to return all objects of a certain class
....
Jandrana wrote:
Reflection means the ability of a programming language to provide functions / methods to "talk" about itself (the objects that make up the language itself)
Examples:
- ask an object about it's class
- ask a function about it's name
- ask anonymous functions about their context of definition
- ask the system to return all objects of a certain class
....
There actually are no objects in Lua, but with tables you can emulate classes. One of the functions pushed into the class metatable is is_a(). That will let you tell what kind of class it is.
string.gfind and math.mod are not supported in lua 5.2 which is why Administrator has been waiting for me to commit the new version of the bot that includes changes that make it 5.2 compatible.
Other userfunctions that use those removed functions will need to be updated. It's easy to update. In most cases string.gfind can be replaced with string.gmatch and math.mod can be replaces with math.fmod. They should be similar functions.
Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.