generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Why do the tables in Lua start on 1?

Previous Thread :: Next Thread 
VorenKurn is not online. VorenKurn
Joined: 18 Jun 2008
Total Posts: 48
29 Apr 2012 05:59 AM
It feels really odd, because most tables/vectors are supposed to start on 0. Is this because Lua is made this way, or did Roblox change it to this form when they implemented Lua into the game?
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
29 Apr 2012 06:09 AM
The ROBLOX developers haven't changing anything in the Lua language at all.

They've implemented it as-is and haven't changed a single thing since then.

They've changed lots of libraries and have attached their API to it, but they haven't ever edited anything at all in the language.
Report Abuse
trappingnoobs is not online. trappingnoobs
Joined: 05 Oct 2008
Total Posts: 19100
29 Apr 2012 06:21 AM
Because it's much easier for newer people to start off with, and Lua was made to be fairly simple.

At least that's my understanding. Prehaps 0-based are easier if you learn them first?
Report Abuse
Brandonhare is not online. Brandonhare
Joined: 02 May 2007
Total Posts: 11005
29 Apr 2012 06:26 AM
It makes a little more sense to be able to do:

tab[#tab]

insead of:

tab[#tab-1]



0 is better for computers, 1 is better for people.
Report Abuse
oysi92 is not online. oysi92
Joined: 20 Nov 2008
Total Posts: 361
29 Apr 2012 06:37 AM
^
I need to watch the whole SG1 series again.
Because I can't remember the exact quote.
Report Abuse
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
29 Apr 2012 09:04 AM
I like the python way, where you get the last item in the table with `tab[-1]`.

There are lots of times when it makes more sense to start a table at 0. To quote an answer on programmers.stackexchange:

> Half-open intervals compose well. If you're dealing in 0 <= i < lim and you want to extend by n elements, the new elements have indices in the range lim <= i < lim + n. Working with zero-based arrays makes arithmetic easier when splitting or concatenating arrays or when counting elements. One hopes the simpler arithmetic leads to fewer fencepost errors.

This basically means you can do stuff like this with 0-base arrays:

    local my2d0BasedArray = {}
    local width = 10
    local height = 8

    --0-based 2D getter
    function getFromMyArray(x, y)
        return my2d0BasedArray [x + y*width]
    end

Whereas with 1-based:

    local my2d1BasedArray = {}
    local width = 10
    local height = 8

    --1-based 2D getter
    function getFromMyArray(x, y)
        return my2d1BasedArray[x + (y - 1)*width]
    end
   
It's a small change, but you end can end up with -1s everywhere
Report Abuse
JulienDethurens is not online. JulienDethurens
Joined: 11 Jun 2009
Total Posts: 11046
29 Apr 2012 09:09 AM
I love the way Python's arrays work.

You can use negative numbers to have fun going backwards with the array, you can also use colons and lots of other things.

They're so easy to manipulate..
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image