Liberty Unleashed
Scripting => Scripting Discussion => Topic started 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:
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)
-
I don't see reason using const if they are rewritable. Just use normal variable.
-
You declared the constant twice. This code, however, will fail to work.
function onServerStart()
{
const MyConstant = "Hi";
print(MyConstant);
MyConstant = "Glad to see you :) ";
print(MyConstant);
}
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:
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.
function onServerStart()
{
// Create a global variable
MyGlobalVariable <- "MyData";
print(MyGlobalVariable);
// Change it
MyGlobalVariable = "MyOtherData";
print(MyGlobalVariable); // <-- this works
}
</rant>
-
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 :)
-
Sorry, I was addressing the thread, not your post. :P
-
This will work too:
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 :)