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: Can you create constant variables?

Previous Thread :: Next Thread 
superinternetman is not online. superinternetman
Joined: 17 Feb 2011
Total Posts: 1957
23 Jul 2015 01:53 AM
In other words, variables that can't be changed.
In C, you can declare a constant integer by doing:

const int myVar = 0;
Report Abuse
MetroidManiac is not online. MetroidManiac
Joined: 05 Jun 2008
Total Posts: 305
23 Jul 2015 01:54 AM
No.
Report Abuse
superinternetman is not online. superinternetman
Joined: 17 Feb 2011
Total Posts: 1957
23 Jul 2015 01:57 AM
Shame, I cringe a little bit when I hard code in numbers, but I'll get over it.
Report Abuse
bosswalrus is not online. bosswalrus
Joined: 04 Jan 2013
Total Posts: 5430
23 Jul 2015 02:02 AM
Some people do

local MYVAR = 0

That's how they know it's constant

No implemented thing in Lua for it tho.
Report Abuse
superinternetman is not online. superinternetman
Joined: 17 Feb 2011
Total Posts: 1957
23 Jul 2015 02:03 AM
Good point bosswalrus.
I'll title my constant variables like so

_MYVAR

With all caps and an underscore before it.
Report Abuse
bosswalrus is not online. bosswalrus
Joined: 04 Jan 2013
Total Posts: 5430
23 Jul 2015 02:05 AM
ew constant typing of underscores
Report Abuse
superinternetman is not online. superinternetman
Joined: 17 Feb 2011
Total Posts: 1957
23 Jul 2015 02:06 AM
eh, maybe i won't use underscores. i normally only ever use underscores in C++ when i'm declaring private variables.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
23 Jul 2015 02:12 AM
Just use globals then.

thing = 0


"Talk is cheap. Show me the code." - Linus Torvalds
Report Abuse
PearSprite is not online. PearSprite
Joined: 02 Jul 2015
Total Posts: 230
23 Jul 2015 02:15 AM
You can make read-only tables!

do
local t = { FLORIDA = 'state', 'AFGHANISTAN' = country }
local Constants = setmetatable( { }, { __index = t, __newindex = error( 'attempt to write to const value', 2 ) } )
end
Report Abuse
PearSprite is not online. PearSprite
Joined: 02 Jul 2015
Total Posts: 230
23 Jul 2015 02:16 AM
"'AFGHANISTAN' = country" should be "AFGHANISTAN = 'country'"
Report Abuse
PearSprite is not online. PearSprite
Joined: 02 Jul 2015
Total Posts: 230
23 Jul 2015 02:17 AM
also __newindex should be set to function() error( 'attempt to write to const value', 2 ) end.

Sorry I'm tired and don't proofread enough
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
23 Jul 2015 02:18 AM
What's the point of making a variable that will never change? Variable literally means that it VARIES.
Report Abuse
PearSprite is not online. PearSprite
Joined: 02 Jul 2015
Total Posts: 230
23 Jul 2015 02:21 AM
math.pi <--
Report Abuse
murcury57 is not online. murcury57
Joined: 30 Jun 2010
Total Posts: 90299
23 Jul 2015 02:26 AM
[ Content Deleted ]
Report Abuse
superinternetman is not online. superinternetman
Joined: 17 Feb 2011
Total Posts: 1957
23 Jul 2015 02:28 AM
@murcury57
wow, that actually works, i don't know why, i don't know how, but that helps a lot, thanks.
Report Abuse
Goulstem is not online. Goulstem
Joined: 04 Jul 2012
Total Posts: 7177
23 Jul 2015 02:33 AM
That's interesting that the __newindex metamethod fires when changing the value of the variable without invilving tab[index] = value .. I didn't expect that to work. Noice job mate.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
23 Jul 2015 02:36 AM
What a useless thread.


"Talk is cheap. Show me the code." - Linus Torvalds
Report Abuse
superinternetman is not online. superinternetman
Joined: 17 Feb 2011
Total Posts: 1957
23 Jul 2015 02:53 AM
@TimeTicks
Oh, shut up already.
Report Abuse
TimeTicks is not online. TimeTicks
Joined: 27 Apr 2011
Total Posts: 27115
23 Jul 2015 02:55 AM
Like I said, making a constant variable in Lua is the most useless thing I have ever seen. You shut up already.


"Talk is cheap. Show me the code." - Linus Torvalds
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
23 Jul 2015 03:50 AM
If you name your constants by uppercase, you'd already know not to change it.
I don't see why you would need to go so complicated and alter the env...
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