Pages: 1 2
Author Topic: How to declare variables in ENIGMA?  (93,679 Views)
Offline (Unknown gender) egofree

Contributor
Joined: Jun 2013
Posts: 601
View profile
Reply #15 Posted on: April 29, 2014, 09:17:14 AM
Very strange, i tried to modify my variable declaration to do some tests. In the end, i tested again my code :

global var myvar = 10;
show_message(string(myvar));


But now i've the following error message :

QuoteIn file included from SHELLmain.cpp:101:0:
C:/ProgramData/ENIGMA/Preprocessor_Environment_Editable/IDE_EDIT_objectaccess.h: In function 'var& enigma::varaccess_myvar(int)':
C:/ProgramData/ENIGMA/Preprocessor_Environment_Editable/IDE_EDIT_objectaccess.h:57:46: error: 'struct enigma::OBJ_child' has no member named 'myvar'
       case child: return ((OBJ_child*)inst)->myvar;

Here is my project :
https://dl.dropboxusercontent.com/u/29802501/test%20inheritance.egm

The code is in the 'child' object. By the way i had a lot of similar error messages in my main project.
Offline (Unknown gender) egofree

Contributor
Joined: Jun 2013
Posts: 601
View profile
Reply #16 Posted on: April 29, 2014, 09:17:46 AM
By the way sorry Darkstar2 for 'polluting' your topic  :D
Offline (Unknown gender) onpon

Member
Joined: Feb 2010
Posts: 102
View profile WWW
Reply #17 Posted on: April 29, 2014, 01:28:18 PM
Quote from: Darkstar2 on April 29, 2014, 08:56:52 AM
If I am NOT going to be using negative, that would be signed

You've got that backwards. Signed means that the first bit is used to indicate a negative number rather than being a part of the value, making negative numbers possible. Unsigned means that all bits are used to indicate the value, which doubles[1] the maximum positive value at the cost of not being able to represent negative numbers.

Example: the char value 10001011 would be -117 if it's signed; since the first bit is 1, the rest of the bits indicate the positive distance from the smallest possible negative number (-128). Unsigned, it would instead be 139.

Usually integers are signed by default. Declaring variables as signed explicitly is not necessary, it just makes your intention more explicit, really.

Quote
Does it mean if I decrease a variable and i goes below 0 it will not be negative?

That might differ between implementations, but I wouldn't depend on this. I don't have a whole lot of experience with this, but my guess is at least some implementations would just set all bits to 1, which would be -1 if the variable is signed, but the maximum value of the variable if it's unsigned (e.g. 255 for a char). Instead of expecting the C++ implementation to deal with unsigned integers differently from signed integers, I would take making sure values stay within a particular range into my own hands.

[1] Actually, it's doubled plus 1.
Offline (Unknown gender) Darkstar2

Member
Joined: Jan 2014
Posts: 1,238
View profile
Reply #18 Posted on: April 29, 2014, 03:27:15 PM
Quote from: egofree on April 29, 2014, 09:17:46 AM
By the way sorry Darkstar2 for 'polluting' your topic  :D

It's ok, it's about variables after all :D
Offline (Unknown gender) Darkstar2

Member
Joined: Jan 2014
Posts: 1,238
View profile
Reply #19 Posted on: April 29, 2014, 03:29:39 PM
Quote from: onpon on April 29, 2014, 01:28:18 PM
Quote from: Darkstar2 on April 29, 2014, 08:56:52 AM
If I am NOT going to be using negative, that would be signed

You've got that backwards. Signed means that the first bit is used to indicate a negative number rather than being a part of the value, making negative numbers possible. Unsigned means that all bits are used to indicate the value, which doubles[1] the maximum positive value at the cost of not being able to represent negative numbers.

Example: the char value 10001011 would be -117 if it's signed; since the first bit is 1, the rest of the bits indicate the positive distance from the smallest possible negative number (-128). Unsigned, it would instead be 139.

Usually integers are signed by default. Declaring variables as signed explicitly is not necessary, it just makes your intention more explicit, really.

Quote
Does it mean if I decrease a variable and i goes below 0 it will not be negative?

That might differ between implementations, but I wouldn't depend on this. I don't have a whole lot of experience with this, but my guess is at least some implementations would just set all bits to 1, which would be -1 if the variable is signed, but the maximum value of the variable if it's unsigned (e.g. 255 for a char). Instead of expecting the C++ implementation to deal with unsigned integers differently from signed integers, I would take making sure values stay within a particular range into my own hands.

[1] Actually, it's doubled plus 1.

So for unsigned it would be something like

int unsigned myvar;

right ?

if not using anything such as

int myvar;

it would be by default signed ?

Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #20 Posted on: April 29, 2014, 06:50:47 PM
QuoteI took this simple example from http://enigma-dev.org/docs/Wiki/Global
That example is wrong. It probably was meant to be "global var" instead of just "global". So this is how it's right now:
1) Use "global.variable;" or "global var variable;" to create a global variable (if you use the version with the dot you will have to write it everywhere).
2) Use "global int variable;" to define a global integer, or any other type (so the template is "global type variable;").
3) Use "int variable;" to declare TEMPORARY (just like "var") integer variable (so the template is "type variable;").
4) Use "local int variable;" to declare an integer variable in the local object scope (so the template is "local type variable;").

So basically "var" is a type, not a keyword. So wherever you use "var" you instead can use another type. So "global var" turns into "global double" for example. Or "global string" or "global unsigned". And just like "var" it will create a temporary variable (limited to the scope of the script) if the "local" keyword is not used.

QuoteVery strange, i tried to modify my variable declaration to do some tests. In the end, i tested again my code :
That is one of a million parser bugs. That's just because you have declared the same global twice and in the object it tries to use the local one. Just comment out or delete the duplicate code either in the script or the object.

QuoteDoes it mean if I decrease a variable and i goes below 0 it will not be negative?
No it wouldn't be. In ENIGMA for some reason this returns 0:
unsigned int myvar = -100;But that is a bug in assignment. If you do any arithmetic on it then it works fine. Meaning usually in all programming languages variables can overflow (http://en.wikipedia.org/wiki/Integer_overflow). So if a char can hold 255 values, then 256 would equal 0 and so on. So in ENIGMA this prints 255:
unsigned char myvar = 255;
show_message(string(myvar));
But this prints 4:
unsigned char myvar = 255;
myvar += 5;
show_message(string(myvar));
Even though I just added 5 to 255. That is because "(255+5) mod 256 = 4". So it "wraps around" the maximum value. The same with integers. Like an unsigned integer cannot be negative, so this results in a very large number:
unsigned int myvar = 10;
myvar -= 20;
show_message(string(myvar));
This will print INTEGER_MAX_VALUE-20 (which is more than 4.2 billion).

Quoteint unsigned myvar;
Usually the "unsigned" is before the data type. So it's "unsigned int".

Quoteit would be by default signed ?
Yes.

All the regular C/C++ stuff applies in ENIGMA and its variables. So I suggest googling like a beginners tutorial in C++ and learn about variables. ENIGMA is only different because we have scopes like "global" and "local", but those don't change the variables. Only from where they can be accessed.

QuoteOk, but I'm still pretty sure 4.5 integer would not make sense in any BASIC compiler that I know of, I think you're just remembering wrong.
Casting float to an integer is usually valid in any language. That is usually done constantly (and we do that all the time in ENIGMA).
Offline (Unknown gender) onpon

Member
Joined: Feb 2010
Posts: 102
View profile WWW
Reply #21 Posted on: April 29, 2014, 06:52:22 PM
In C++, "signed" or "unsigned" comes before the type, not after (e.g. "unsigned int foo"). I'm pretty sure EDL is the same in this regard.
Offline (Unknown gender) Darkstar2

Member
Joined: Jan 2014
Posts: 1,238
View profile
Reply #22 Posted on: April 29, 2014, 09:25:38 PM
Quote from: Robert B Colton on April 29, 2014, 06:39:23 AM
Ok, but I'm still pretty sure 4.5 integer would not make sense in any BASIC compiler that I know of, I think you're just remembering wrong.

Don't underestimate my memory :D  I have sharp memory.

Yes in BASIC I used INT and fraction numbers.

Here is an example, I used it a lot when generating random numbers.

A=INT(RND(0)*100)

BTW, in ENIGMA you can also use int(variable),
I tried it.

In C++ it would be (int)variable right ?


Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #23 Posted on: April 29, 2014, 11:28:37 PM
QuoteIn C++ it would be (int)variable right ?
Yes, same as ENIGMA. You can also write int(variable) and it is also valid. But I think that can cause more ambiguity problems, so it's usually (int)variable instead.
Offline (Unknown gender) egofree

Contributor
Joined: Jun 2013
Posts: 601
View profile
Reply #24 Posted on: April 30, 2014, 11:56:37 AM
Darkstar2, TheExdeus:
Thanks for you help.
Pages: 1 2