Liberty Unleashed

Scripting => Scripting Discussion => Topic started by: VetalYA on November 18, 2011, 04:34:13 pm

Title: Constants is rewritable
Post by: VetalYA on November 18, 2011, 04: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 (http://forum.liberty-unleashed.co.uk/index.php?topic=915.msg5660#msg5660)
Title: Re: Constants is rewritable
Post by: Romop5 on November 18, 2011, 07:49:43 pm
I don't see reason using const if they are rewritable. Just use normal variable.
Title: Re: Constants is rewritable
Post by: stormeus on November 18, 2011, 07: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>
Title: Re: Constants is rewritable
Post by: Romop5 on November 18, 2011, 08: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 :)
Title: Re: Constants is rewritable
Post by: stormeus on November 18, 2011, 08:54:59 pm
Sorry, I was addressing the thread, not your post. :P
Title: Re: Constants is rewritable
Post by: VetalYA on November 19, 2011, 12: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 :)