Variables only change the compatibiity of save-games?

Started by bx83, Sun 04/04/2021 13:41:24

Previous topic - Next topic

Crimson Wizard

#20
Quote from: bx83 on Fri 16/04/2021 00:41:22
I just want a named variable - as in, create ThisIsAVar, then use it later. indexes are a bit impersonal and I'll likely make a mistake.

Is there any way to have named global variables? (other than named arrays with just pure indices)

If the problem is only in using some name to access an element of array, then there are multiple solutions to this.

First of all, as I mentioned earlier, you may create regular arrays too (not dynamic). After you do that, each time you need extra variable you add one right before that array, and subtract one element from array. This way total size of variables and their order stays the same.
For example, you have this:
Code: ags

int GVInt[999];

then you have this:
Code: ags

int myNewVariable;
int GVInt[998];

and then
Code: ags

int myNewVariable;
int myNewVariable2;
int GVInt[997];



Second, you may declare enum and use it as named indexes. Note that new enums do not break saves, because they are just named constants so don't take any program memory.
Code: ags

enum MyVariables {
    eVarNewGlobalInt = 0,
    eVarSecondGlobalInt = 1
};

and the use them to access array as:
Code: ags

GVInt[eVarNewGlobalInt]



Third, you may create attributes... But that's part of advanced scripting, and I am not sure if I should suggest that here.
If you're suddenly interested, there's where they are mentioned in the manual: https://adventuregamestudio.github.io/ags-manual/OOProgramming.html#defining-attributes

bx83

Thankyou for this, just what I was looking for :)


As usual, having trouble defining things...

.asc
Code: ags
enum eGVInt {
  eFirst=0, 
  eSecond=1,
  eThird=2
};


function game_start()
{
  GVInt = new int[999];
........
}

export eGVInt;


.ash
Code: ags
import int eGVInt;


error: "eGVInt is already defined" (referring to line 'enum eGVInt {', first line in .asc)

Khris

Header:
Code: ags
enum eGVInt {
  eFirst=0,
  eSecond=1,
  eThird=2
};

import int GVInt[]; // after import this is the exact same as the declaration


Main script:
Code: ags
int GVInt[];
export GVInt; // just the name

function game_start() {
  GVInt = new int[999];
}

Crimson Wizard

Yes, you should export array, not enum. Array is the real variable, enum is not a variable, it's just a named constant, and of course you need to have it in the header, otherwise no other script would know what names are there.

Cassiebsg

Sure, just create a bunch of variables in Global variables and call them"Dummy1", "Dummy2", etc... Once you need to use a dummy to patch the game, just grab one of them and rename it and it's ready to use.  ;)
There are those who believe that life here began out there...

bx83

Thank you, works :)

One more bit of fun; I've found https://www.adventuregamestudio.co.uk/forums/index.php?topic=55545.0, which is a great Timer script to replace standard timers in AGS.
To define a global timer (with a name), how do I do it?

.ash
Code: ags
Timer              *GVTimers[20];
export GVTimers;

.asc
Code: ags
import Timer              *GVTimers[];


error: .ash, expected variable or functions after import, not 'Timer'

ps. only 20 indices because that's the maximum amount of timers that can run simultaneously.

Crimson Wizard

#26
Quote from: bx83 on Fri 16/04/2021 20:57:02
error: .ash, expected variable or functions after import, not 'Timer'
Make sure you have Timer module above the script where you declare these arrays.

Also, when you are importing regular arrays you must include their size.

I will link same article about import/export again:
https://github.com/adventuregamestudio/ags-manual/wiki/ImportingFunctionsAndVariables

Quote from: bx83 on Fri 16/04/2021 20:57:02
ps. only 20 indices because that's the maximum amount of timers that can run simultaneously.

For the Timer module you can increase that amount to whatever you want, by changing MAX_RUNNING_TIMERS in the module's header.
Then, you can create much more timer variables. It's just that only so much of them would be able to run at the same time. You may have timers that run only sometimes.

SMF spam blocked by CleanTalk