Author Topic: Constants is rewritable  (Read 1238 times)

VetalYA

  • Guest
Constants is rewritable
« on: November 18, 2011, 05:34:13 pm »
Since ( & UNTIL   :) ) constants is rewritable in LU Server, you can use them as Global Variables.

Just try this:

Code: [Select]
function onServerStart()
{

const MyConstant = "Hi";
print(MyConstant);

const MyConstant = "Glad to see you :) ";
print(MyConstant);

}


Updated: To All Young scripters, Do not use it in your scripts.  Read below why.

http://forum.liberty-unleashed.co.uk/index.php?topic=915.msg5660#msg5660
« Last Edit: November 19, 2011, 02:03:22 am by VetalYA »

Romop5

  • Jr. Member
  • **
  • Posts: 83
  • Karma: +2/-1
  • A student of university and creator of LHMP.
    • View Profile
    • LHMP
Re: Constants is rewritable
« Reply #1 on: November 18, 2011, 08:49:43 pm »
I don't see reason using const if they are rewritable. Just use normal variable.

stormeus

  • No-Lifer
  • Developer
  • Full Member
  • *****
  • Posts: 112
  • Karma: +13/-2
    • View Profile
Re: Constants is rewritable
« Reply #2 on: November 18, 2011, 08:51:52 pm »
You declared the constant twice. This code, however, will fail to work.
Code: (Squirrel) [Select]
function onServerStart()
{
const MyConstant = "Hi";
print(MyConstant);

MyConstant = "Glad to see you :) ";
print(MyConstant);

}
Code: (Server Output) [Select]
Scripts/Main/Main.nut line = (6) column = (14) : error can't assign expression

I see no reason to use them as global variables anyways, since you'd have to use const every time you want to redeclare it, and they cannot be redefined like this, which a lot of scripts will require:
Code: (Squirrel) [Select]
function onServerStart()
{
MyConstant = "MyData";
}

Besides, constants are supposed to be... well, constant. This is absolutely bad practice. Want to use constants as global variables? Then use global variables.
Code: (Squirrel) [Select]
function onServerStart()
{
    // Create a global variable
    MyGlobalVariable <- "MyData";
    print(MyGlobalVariable);

    // Change it
    MyGlobalVariable = "MyOtherData";
    print(MyGlobalVariable); // <-- this works
}
</rant>
« Last Edit: November 18, 2011, 08:53:47 pm by stormeus »
Quote
Morphine says:
    them LU devs ranting about how LU doesn't have client pickups
    while us VC:MPers don't have client anything
    ;_;

Stormeus Argo says:
    we have client crashes though
    ohohohohoho

Morphine says:
    LU DOESN'T HAVE THAT

Stormeus Argo says:
    LU - 0
    VC:MP - 1

Romop5

  • Jr. Member
  • **
  • Posts: 83
  • Karma: +2/-1
  • A student of university and creator of LHMP.
    • View Profile
    • LHMP
Re: Constants is rewritable
« Reply #3 on: November 18, 2011, 09:17:09 pm »
I just wanted to say: in every lang is const only const, no-changing variable (just like in math or physic), so if you want use const as variable, rather use normal variable :)

stormeus

  • No-Lifer
  • Developer
  • Full Member
  • *****
  • Posts: 112
  • Karma: +13/-2
    • View Profile
Re: Constants is rewritable
« Reply #4 on: November 18, 2011, 09:54:59 pm »
Sorry, I was addressing the thread, not your post. :P
Quote
Morphine says:
    them LU devs ranting about how LU doesn't have client pickups
    while us VC:MPers don't have client anything
    ;_;

Stormeus Argo says:
    we have client crashes though
    ohohohohoho

Morphine says:
    LU DOESN'T HAVE THAT

Stormeus Argo says:
    LU - 0
    VC:MP - 1

VetalYA

  • Guest
Re: Constants is rewritable
« Reply #5 on: November 19, 2011, 01:55:55 am »
This will work too:

Code: [Select]
function onServerStart()
{

           const testA = 0;
const testA = 1;

if(testA == 1){  print(" It Works !"); }

const testA = 0;
if(testA == 1){  print(" Fail to print ? because of constant < testA> is zerrrrro"); }

const testA = 1;
if(testA == 1){  print(" Hehe !"); }

}

I just wanted to say: in every lang is const only const, no-changing variable

Yes, i know it  ::) that's why I started this topic.


Anyway, Constants should keep to be constants.

To all: Do not overwrite your constants . It's really bad and may cause of troubles in the future & I will not be responsible in this case.


Let's finish this delirium  :) Big thank to all :)
« Last Edit: November 19, 2011, 02:01:30 am by VetalYA »

 

© Liberty Unleashed Team.