Jump to content




Some lua tricks you may not have known...


106 replies to this topic

#41 aaakk

  • Members
  • 58 posts

Posted 25 November 2012 - 11:06 PM

Thank you for this thread !

#42 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 27 March 2013 - 07:41 AM

Updated.

Added a section on metatables, trapping arguments in tables, and iterators.

#43 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 27 March 2013 - 09:46 AM

rename the 'Boolean' section to 'The Ternary Operator'

#44 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 27 March 2013 - 09:53 AM

View PostTheOriginalBIT, on 27 March 2013 - 09:46 AM, said:

rename the 'Boolean' section to 'The Ternary Operator'

It doesn't only have to do with ternary operations though. Although specifying that the shortened if statement is a ternary operator would probably be good.

#45 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 28 March 2013 - 03:00 AM

Another trick if you want double brackets in double brackets you can do.
[=[
This is [[cool]]
]=]
You can add up the ='s like this
[==[
[=[hey]=][hey]
]==]
Etc.

Cheers Alexander

EDIT: This could also fix a bug in lightshot with some brackets in the edit program not showing

#46 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 28 March 2013 - 03:02 AM

View Postsuperaxander, on 28 March 2013 - 03:00 AM, said:

Another trick if you want double brackets in double brackets you can do.
[=[
This is [[cool]]
]=]
You can add up the ='s like this
[==[
[=[hey]=][hey]
]==]
Etc.

Cheers Alexander

EDIT: This could also fix a bug in lightshot with some brackets in the edit program not showing
Hmmm seems someone has been reading this chapter of the PIL... :P

PIL extract
Spoiler


#47 superaxander

  • Members
  • 609 posts
  • LocationHolland

Posted 28 March 2013 - 03:04 AM

View PostTheOriginalBIT, on 28 March 2013 - 03:02 AM, said:

View Postsuperaxander, on 28 March 2013 - 03:00 AM, said:

Another trick if you want double brackets in double brackets you can do.
[=[
This is [[cool]]
]=]
You can add up the ='s like this
[==[
[=[hey]=][hey]
]==]
Etc.

Cheers Alexander

EDIT: This could also fix a bug in lightshot with some brackets in the edit program not showing
Hmmm seems someone has been reading this chapter of the PIL... :P/>

PIL extract
Spoiler
Nope I found it on lua users under hidden features.

#48 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 28 March 2013 - 03:05 AM

nice, I didn't know that. that way I can get sublime text to highlight correctly and not get confused when I use a table indexing to index a table (like mytable[mytable[max]])

#49 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 28 March 2013 - 03:07 AM

mine doesn't get confused with that... o.O

#50 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 28 March 2013 - 03:09 AM

it ends the string highlight or something. I can't quite remember the issue I was having but I remember it being highly irritating

EDIT: I think I'll move this to PM... no thread hijacking

#51 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 28 March 2013 - 03:12 AM

got the latest version of Grav's pack?

#52 Kilobyte

  • Members
  • 122 posts

Posted 08 April 2013 - 11:29 AM

Very good one. One note though: don't use slowPrint() itjust slows down the code. also by you using it here you encurage the noobs to use it. And thats not what is good. basicly slowPrint (and slowWrite) are functions which should just be removed.

#53 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 08 April 2013 - 11:40 AM

View PostKilobyte, on 08 April 2013 - 11:29 AM, said:

Very good one. One note though: don't use slowPrint() itjust slows down the code. also by you using it here you encurage the noobs to use it. And thats not what is good. basicly slowPrint (and slowWrite) are functions which should just be removed.

slowPrint() has a purpose and I would not advocate removing it. Occasionally people do have a valid reason to use it and taking it out would be pointless. Of course, noobs have a tendency of using it far too much which can be annoying, but if they want to make their code slow then that is up to them.

Valid use of it:
local menu = [[- File Edit Update Config About]]
term.setBackgroundColor(colors.white)
term.clear()
term.setBackgroundColor(colors.red)
term.setCursorPos(1,1)
term.clearLine()
local display = false

while true do
  term.setBackgroundColor(colors.red)
  term.setCursorPos(1,1)
  term.clearLine()
  if display then
    term.setBackgroundColor(colors.blue)
  end
  textutils.slowPrint(display and menu or "+")
  local e = {os.pullEvent()}
  if e[1] == "mouse_click" and e[3] == 1 and e[4] == 1 then
    display = not display
  elseif e[1] == "key" and e[2] == keys.enter then
    return
  end
end

Admittedly, the above code is not a very good example of slowPrint. But it does show that it has a purpose making interesting text effects.

#54 Pharap

  • Members
  • 816 posts
  • LocationEngland

Posted 04 May 2013 - 12:23 PM

The len operator ( this thing # ) can be applied to tables to count all their numerical indexes, not just the arg table.
You should also be able to override it with metatables in most implementations of lua, though I'm not certain if the CC implementation supports this.

#55 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 04 May 2013 - 12:53 PM

View PostPharap, on 04 May 2013 - 12:23 PM, said:

You should also be able to override it with metatables in most implementations of lua, though I'm not certain if the CC implementation supports this.
I don't think it does.... everytime I've overrode the __len metamethod it doesn't actually change anything.

#56 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 04 May 2013 - 01:01 PM

View PostPharap, on 04 May 2013 - 12:23 PM, said:

The len operator ( this thing # ) can be applied to tables to count all their numerical indexes, not just the arg table.
I thought that was pretty obvious, but alright.

#57 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 04 May 2013 - 01:08 PM

View PostKingdaro, on 04 May 2013 - 01:01 PM, said:

I thought that was pretty obvious, but alright.
I think the key thing stated there is "all their numerical indexes" ... so a table like so
local t = { 1, "hello", k = "world" }
would return 2 with both # and table.getn ...

#58 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 04 May 2013 - 01:16 PM

I alerady knew that; I don't get what you guys are saying.

#59 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 04 May 2013 - 01:18 PM

View PostKingdaro, on 04 May 2013 - 01:16 PM, said:

I alerady knew that; I don't get what you guys are saying.
I didn't realise that this was just for you to learn new stuff.

#60 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 04 May 2013 - 01:33 PM

I was confused because I thought you both were talking to me, haha.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users