Post code tricks here!
#1
Posted 25 March 2013 - 11:35 AM
I'll start:
I made an arcade conversion of Hurdles, and in order for it to restart, when the game exited its final line was os.reboot().
Then, I made a startup program to run hurdles again on the monitor. I thus worked around having the program directly use the monitor in its code, and saved a ton of hassles.
So anyway post your Lua tricks here!
#2
Posted 25 March 2013 - 01:09 PM
#4
#5
Posted 25 March 2013 - 02:14 PM
Sammich Lord, on 25 March 2013 - 02:06 PM, said:
#6
Posted 25 March 2013 - 09:50 PM
for item=1,15 do local condition = item<=10 and "Less than or equal to 10" or item==11 and "Equal to 11" or "Greater than 11" print(condition) end
I've only ever seen condition statements like this in Java, and even there it's not as simple or powerful as what Lua has (unless I'm missing something pretty huge from Java).
Edit: Oh and also this thread has most of the lua tricks that I find useful, although metatables are missing.
#7
Posted 25 March 2013 - 10:03 PM
Bubba, on 25 March 2013 - 09:50 PM, said:
Bubba, on 25 March 2013 - 09:50 PM, said:
#8
Posted 26 March 2013 - 05:43 AM
#10
Posted 26 March 2013 - 01:20 PM
for event, p1, p2 in os.pullEvent do -- Add other parameter variables as needed -- Do stuff with event/params end
There's not much difference from a while true do loop that calls os.pullEvent each time, but I think this looks a bit nicer.
#11
Posted 26 March 2013 - 05:31 PM
#12
Posted 26 March 2013 - 06:44 PM
Old long way:
if not someVariable then error( 'You did not give me the parameter I wanted', 2) endNew way:
assert( someVariable, 'You did not give me the parameter I wanted', 2)
Code that does this:
_G.assert = function(condition, errMsg, level) if not condition then error(errMsg, (tonumber(level) or 1) + 1) end return condition end
A usage example:
views = {}
function registerKey(screen, key, text, xPos, yPos)
-- validate the parameters
assert(views[screen], 'A screen with the key \''..screen..'\' does not exist', 2)
assert(not views[screen][key], 'A key with the key \''..screen..'\' already exists', 2)
assert(type(text) == 'string', 'Invalid parameter: expected text to be a string, got '..type(text), 2)
assert(type(xPos) == 'number', 'Invalid parameter: expected xPos to be a number, got '..type(xPos), 2)
-- do stuff here
end
The example using the old way
local views = {}
function registerKey(screen, key, text, xPos, yPos)
-- validate the parameters
if not views[screen] then
error( 'A screen with the key \''..screen..'\' does not exist', 2)
end
if views[screen][key] then
error('A key with the key \''..screen..'\' already exists', 2)
end
if type(text) ~= 'string' then
error('Invalid parameter: expected text to be a string, got '..type(text), 2)
end
if type(xPos) ~= 'number' then
error('Invalid parameter: expected xPos to be a number, got '..type(xPos), 2)
end
-- do stuff here
end
#13
Posted 26 March 2013 - 07:13 PM
#14
Posted 26 March 2013 - 07:44 PM
Snippet from my menu code:
function menu.addOption(menuTable, name, func)
assert(type(menuTable) == "table", "menu.addOption: menu argument must be table")
assert(type(name) == "string", "menu.addOption: name argument must be string")
assert(type(func) == "function", "menu.addOption: function argument must be function")
assert(#menuTable == 3, "menu.addOption: menu argument must be of form {string, table, table}")
assert(type(menuTable[1]) == "string", "menu.addOption: menu argument must be of form {string, table, table}")
assert(type(menuTable[2]) == "table", "menu.addOption: menu argument must be of form {string, table, table}")
assert(type(menuTable[3]) == "table", "menu.addOption: menu argument must be of form {string, table, table}")
menuTable[2][#menuTable[2] + 1] = name
menuTable[3][#menuTable[3] + 1] = func
return menuTable
end
Edit: This is default by the way, no extra code used to add assert.
#15
Posted 27 March 2013 - 12:39 AM
Bubba, on 25 March 2013 - 09:50 PM, said:
for item=1,15 do local condition = item<=10 and "Less than or equal to 10" or item==11 and "Equal to 11" or "Greater than 11" print(condition) end
I've only ever seen condition statements like this in Java, and even there it's not as simple or powerful as what Lua has (unless I'm missing something pretty huge from Java).
Edit: Oh and also this thread has most of the lua tricks that I find useful, although metatables are missing.
PHP way:
<?php
for ($i=1; $i <= 15; $i++) {
echo $i.' - '.($i < 11 ? 'Less than or equal to 10' : ($i == 11 ? 'Equal to 11' : 'Greater than 11')).'<br>';
}
?>
#16
Posted 27 March 2013 - 02:02 AM
remiX, on 27 March 2013 - 12:39 AM, said:
Bubba, on 25 March 2013 - 09:50 PM, said:
for item=1,15 do local condition = item<=10 and "Less than or equal to 10" or item==11 and "Equal to 11" or "Greater than 11" print(condition) end
I've only ever seen condition statements like this in Java, and even there it's not as simple or powerful as what Lua has (unless I'm missing something pretty huge from Java).
Edit: Oh and also this thread has most of the lua tricks that I find useful, although metatables are missing.
PHP way:
<?php
for ($i=1; $i <= 15; $i++) {
echo $i.' - '.($i < 11 ? 'Less than or equal to 10' : ($i == 11 ? 'Equal to 11' : 'Greater than 11')).'<br>';
}
?>
Yup. TOB already corrected my unfortunate lack of knowledge there
#17
Posted 27 March 2013 - 03:17 AM
faubiguy, on 26 March 2013 - 07:44 PM, said:
Snippet from my menu code:
function menu.addOption(menuTable, name, func)
assert(type(menuTable) == "table", "menu.addOption: menu argument must be table")
assert(type(name) == "string", "menu.addOption: name argument must be string")
assert(type(func) == "function", "menu.addOption: function argument must be function")
assert(#menuTable == 3, "menu.addOption: menu argument must be of form {string, table, table}")
assert(type(menuTable[1]) == "string", "menu.addOption: menu argument must be of form {string, table, table}")
assert(type(menuTable[2]) == "table", "menu.addOption: menu argument must be of form {string, table, table}")
assert(type(menuTable[3]) == "table", "menu.addOption: menu argument must be of form {string, table, table}")
menuTable[2][#menuTable[2] + 1] = name
menuTable[3][#menuTable[3] + 1] = func
return menuTable
end
Edit: This is default by the way, no extra code used to add assert.
#19
Posted 27 March 2013 - 03:19 AM
#20
Posted 27 March 2013 - 04:21 AM
MudkipTheEpic, on 27 March 2013 - 03:19 AM, said:
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











