SA-MP Forums Archive
How to create a constant string - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to create a constant string (/showthread.php?tid=627332)



How to create a constant string - rt-2 - 26.01.2017

How to create a constant string?

I am trying
Код:
new const INV_OBJ_menuTitle[] = "Inventory";
new dialog_title[64] = INV_OBJ_menuTitle;
and I get the error: error 008: must be a constant expression; assumed zero (on the second line)

but when I try
Код:
const INV_OBJ_menuTitle[] = "Inventory";
new dialog_title[64] = INV_OBJ_menuTitle;
I get the error: error 001: expected token: "=", but found "[" (on the first line)

How am I supposed to set a constant string, is it possible?

Thank you


Re: How to create a constant string - Dayrion - 26.01.2017

PHP код:
static const INV_OBJ_menuTitle[] = "Inventory"



Re: How to create a constant string - rt-2 - 26.01.2017

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
PHP код:
static const INV_OBJ_menuTitle[] = "Inventory"
Thank you,
But in the documentation, it says that the variable with a static scope would only be available for the script in the same "section" it was created, does anyone knows what that means a "section" refers to?
https://sampwiki.blast.hk/wiki/Keywords:Initialisers#static

Thank you,
rt-2


Re: How to create a constant string - Eoussama - 26.01.2017

It means a block of code, the code between the two "{}", which defines an independent scope
see here
https://sampwiki.blast.hk/wiki/Scripting_Basics#Scope


Re: How to create a constant string - Dayrion - 26.01.2017

Quote:
Originally Posted by blinkpnk
Посмотреть сообщение
Thank you,
But in the documentation, it says that the variable with a static scope would only be available for the script in the same "section" it was created, does anyone knows what that means a "section" refers to?
https://sampwiki.blast.hk/wiki/Keywords:Initialisers#static

Thank you,
rt-2
If you declare ur variable as a global variable, you can use it in your whole script. If you include your script in an another one, you cannot use this variable.


Re: How to create a constant string - rt-2 - 26.01.2017

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
PHP код:
static const INV_OBJ_menuTitle[] = "Inventory"
Код:
static const INV_OBJ_menuTitle[] = "Inventory";
function(attr1)
{
     new dialog_title[64] = INV_OBJ_menuTitle;
}
This is not working, I am still getting "error 008: must be a constant expression; assumed zero" (on fourth line)

Note that I am setting up the variable at the top of my script and trying to use the constant value in a function/callback.

Thank you


Re: How to create a constant string - Vince - 26.01.2017

Quote:
Originally Posted by Eoussama
Посмотреть сообщение
It means a block of code, the code between the two "{}", which defines an independent scope
see here
https://sampwiki.blast.hk/wiki/Scripting_Basics#Scope
No. That's something else entirely. A section basically refers to the file itself, e.g. a global static variable in an include file will only be visible to that file. There is also a directive #section that can be used to declare multiple sections within a file, but that doesn't work. Perhaps it is fixed in Zeex' compiler.


Re: How to create a constant string - Dayrion - 26.01.2017

Quote:
Originally Posted by blinkpnk
Посмотреть сообщение
Код:
static const INV_OBJ_menuTitle[] = "Inventory";
function(attr1)
{
     new dialog_title[64] = INV_OBJ_menuTitle;
}
This is not working, I am still getting "error 008: must be a constant expression; assumed zero" (on fourth line)

Note that I am setting up the variable at the top of my script and trying to use the constant value in a function/callback.

Thank you
PHP код:
static const INV_OBJ_menuTitle[][] = {"Inventory"};
function(
attr1)
{
     new 
dialog_title[64] = INV_OBJ_menuTitle;




Re: How to create a constant string - rt-2 - 26.01.2017

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
PHP код:
static const INV_OBJ_menuTitle[][] = {"Inventory"};
function(
attr1)
{
     new 
dialog_title[64] = INV_OBJ_menuTitle;

Can you test before posting?
It gives the same error.


Re: How to create a constant string - Dayrion - 26.01.2017

Quote:
Originally Posted by blinkpnk
Посмотреть сообщение
Can you test before posting?
It gives the same error.
Of course a constant can contain a string. Stop being rude when I'm trying to help you. -.-
PHP код:
#define title "Invetory"
function(attr1

     new 
dialog_title[64] = title;

working fine with basic warnings.