Defining my own constant, global variable wth a value returned from a function

Started by bx83, Sat 09/05/2020 06:12:32

Previous topic - Next topic

bx83

I've tried defining a global const; no option for this in global variables.
I've tried defining one in-script, except then it's not global.
Tried defining a readonly int in global.h, but it says I need to enter a literal value like 2 rather than a function like Random(2); (I think)
I've tried searching google and the forums, but nothing obvious shows.
I've tried to look up the manual, but nothing in there.

So what do I do?

Lewis

Returning to AGS after a hiatus. Co-director of Richard & Alice and The Charnel House Trilogy.

Cassiebsg

like Lewis, I also did not understood what you are trying to accomplish.
If it's a constant, then it won't change during the entire game. Which means all you do is define the variable and not touch it again...

int constant=20;

String constant="Hello";

And using Global Variables is the same, you just give it the initial value.

But then you're talking about a Random(2), which is definetly not a constant, but a variable with 2 possibilities...  (roll)

Either way, constants are just variables that you do not change.  ;)
There are those who believe that life here began out there...

bx83

Main point is:
Can I make my own constant, global variable with a value returned from a function?
If not, why? Is it impossible?

Khris

As others have explained, doing this isn't necessary. You create a regular variable, then don't write to it.

But no, afaik you can't. Assigning a function's return value isn't possible during declaration, so you can't use a constant anyway because you'd have to assign to it in game_start.

morganw

Quote from: bx83 on Sat 09/05/2020 06:12:32
Tried defining a readonly int in global.h, but it says I need to enter a literal value like 2 rather than a function like Random(2); (I think)

Why not just use a macro? They are just text so you can do whatever you want with them.
https://adventuregamestudio.github.io/ags-manual/Preprocessor.html

Code: ags
#define THE_NUMBER_TWO 2
#define DAYS_IN_A_WEEK 7
#define RANDOM2 Random(2)

Snarky

If you were to #define it that way, morganw, it would calculate a new random number each time you used it (as I'm sure you're aware). If I understand bx83 correctly, he wants a single fixed number, decided randomly on each startup.

For various implementation reasons this is not possible to do as a constant, and the easiest way is to just use a normal variable and make sure never to change it after it's initially set.

However, if you want to ensure that it cannot be changed, you can protect it behind a read-only property:

Code: ags
// Header
struct Constant
{
   import static readonly attribute int Foo;
};

// Script
int foo;

int get_Foo(static Constant)
{
  return foo;
}

function game_start()
{
  foo = Random(2);
}

(Untested, so there might be errors.)

Now you can use Constant.Foo as a "constant" elsewhere. (However, it's not an actual constant, so you cannot use it to set static array sizes, for example.)

Crimson Wizard

If you really want to protect yourself from occasionally changing a variable, I know two ways. First is macros, as shown by morganw, but non-trivial macros may be not what you want as their result may depend on time of calling.

Another way is to declare a struct with writeprotected members, and initialize that struct in game_start by calling some kind of "init" function in it (because only struct's member function can write writeprotected variables).

EDIT: well, Snarky posted third way with properties.

For example, in global.ash declare:
Code: ags

struct ConstantVariables
{
    writeprotected int MyRandomVar;

    import void Init();
};

// the only instance of constant vars
import ConstantVariables ConstVars;


and in global.asc:
Code: ags

ConstantVariables ConstVars;
export ConstVars;

void ConstantVariables::Init()
{
    this.MyRandomVar = Random(2);
}

function game_start()
{
    ConstVars.Init();
}



If necessary, you may initialize this struct passing arguments into init function (or have several of these for convenience).

SMF spam blocked by CleanTalk