Pages: « 1 2
  Print  
Author Topic: How to declare variables in ENIGMA?  (Read 3196 times)
Offline (Unknown gender) egofree
Reply #15 Posted on: April 29, 2014, 04:17:14 AM
Contributor
Joined: Jun 2013
Posts: 603

View Profile Email
Very strange, i tried to modify my variable declaration to do some tests. In the end, i tested again my code :

Code: [Select]
global var myvar = 10;
show_message(string(myvar));

But now i've the following error message :

Quote
In 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.
Logged
Offline (Unknown gender) egofree
Reply #16 Posted on: April 29, 2014, 04:17:46 AM
Contributor
Joined: Jun 2013
Posts: 603

View Profile Email
By the way sorry Darkstar2 for 'polluting' your topic  :D
Logged
Offline (Unknown gender) onpon
Reply #17 Posted on: April 29, 2014, 08:28:18 AM

Member
Joined: Feb 2010
Posts: 102

View Profile WWW Email
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.
« Last Edit: April 29, 2014, 08:30:02 AM by onpon » Logged
Offline (Unknown gender) Darkstar2
Reply #18 Posted on: April 29, 2014, 10:27:15 AM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
By the way sorry Darkstar2 for 'polluting' your topic  :D

It's ok, it's about variables after all :D
Logged
Offline (Unknown gender) Darkstar2
Reply #19 Posted on: April 29, 2014, 10:29:39 AM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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 ?

« Last Edit: April 29, 2014, 10:55:32 AM by Darkstar2 » Logged
Offline (Unknown gender) TheExDeus
Reply #20 Posted on: April 29, 2014, 01:50:47 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
Quote
I 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.

Quote
Very 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.

Quote
Does 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:
Code: [Select]
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:
Code: [Select]
unsigned char myvar = 255;
show_message(string(myvar));
But this prints 4:
Code: [Select]
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:
Code: [Select]
unsigned int myvar = 10;
myvar -= 20;
show_message(string(myvar));
This will print INTEGER_MAX_VALUE-20 (which is more than 4.2 billion).

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

Quote
it 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.

Quote
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.
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).
Logged
Offline (Unknown gender) onpon
Reply #21 Posted on: April 29, 2014, 01:52:22 PM

Member
Joined: Feb 2010
Posts: 102

View Profile WWW Email
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.
Logged
Offline (Unknown gender) Darkstar2
Reply #22 Posted on: April 29, 2014, 04:25:38 PM
Member
Joined: Jan 2014
Posts: 1244

View Profile Email
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 ?


Logged
Offline (Unknown gender) TheExDeus
Reply #23 Posted on: April 29, 2014, 06:28:37 PM

Developer
Joined: Apr 2008
Posts: 1872

View Profile
Quote
In 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.
Logged
Offline (Unknown gender) egofree
Reply #24 Posted on: April 30, 2014, 06:56:37 AM
Contributor
Joined: Jun 2013
Posts: 603

View Profile Email
Darkstar2, TheExdeus:
Thanks for you help.
Logged
Pages: « 1 2
  Print