Using Global variables.

Started by Ophidic, Tue 04/04/2023 05:34:49

Previous topic - Next topic

Ophidic

I though I had whittled things down to be simple, but unfortunately not simple enough it seems. I gave up for now trying to do arrays, I could never do arrays, not in applesoft basic, not now.

After difficulties with other things I decided on a simple RNG on game start that defines a location of an item, just in checking a hot spot it will check the variable and if the 3rd location detects the variable is 3 it gives you the item and if says the other thing.

that should be defined as far as I'm aware as:

Code: ags
function game_start()
{
int RNG = (1, 4);
}

I think? I don't know if I'm doing that right, I've been talking to chat gpt and looking at the manual but I'm not gasping this somehow.

It INSISTS I can refer to RNG in a room function thing but it doesn't come up on the list of things that I can use. I keep getting errors about stuff not defined and I am not even really sure how to ask or continue to look for help on this since the documentation isn't helping me grasp this at all. I wish I knew the words for these things better.

Gilbert

I haven't read thoroughly what you're supposed to do, but try this first:
Code: ags
int RNG; 

function game_start()
{
  RNG = Random(3)+1; //Random(3) gives a value from 0-3 so add 1 to get a value from 1-4
}

A few points of note:
  • A global variable must be defined OUTSIDE of any function; otherwise the variable is only created inside the function and destroyed once the function is finished.
  • You can only assign a static value to a variable at definition, when it's defined outside of any function, so you cannot assign RNG a value from Random() immediately, but have to do so inside the function game_start() like your original code.
  • The Random() function takes only one parameter (you may check the manual).
  • If the variable is only used in the global script that's okay, but if you need to use it also in another script you have to export the variable and import it to other scripts (see this).

Khris

The idea that ChatGPT can help you understand AGS scripting is... curious. It's a program that strings words together based on probabilities, not a programming tutor.

Anyway, when you want a global array, you start with the top of the GlobalScript and think about a proper type, name and size.
For instance:
Code: ags
int randomFlag[10];

This gives you ten variables, randomFlag[0] to randomFlag[9]. You can now access them inside any function in the Global Script. If you also want to access them in room scripts, you need to make them actually global. First, export the variable right below the declaration. For this you only need the name. So it'll look like this:
Code: ags
int randomFlag[10];
export randomFlag; // it already exists, so we only need the name, not the type or size

Finally, we need to import it in the header. This header will end up above each room script and make the variable accessible in there. Now we do need the full information though, which simply means copy-pasting the declaration after the import keyword:
Code: ags
// script header
import int randomFlag[10];

Now let's say the first room is going to use randomFlag[0] for whatever purpose. We can now put
Code: ags
  randomFlag[0] = Random(3) + 1;
inside the linked room_FirstLoad function.

(If you only need to access the variable inside room functions though you shouldn't use a global variable for that. Just declare it at the top of the room script instead and you can use it in any function of the room script.)

Crimson Wizard

#3
If you're beginner in AGS, I also suggest using "Global Variables" panel in the project tree. It makes creating global vars easier, with the exception of custom types (it can only work with built-in types for the moment).

PS.
Regarding ChatGPT, from what I understand it learns from stuff found on the internet, and then generates the answer by merging common things together. I've seen examples of people asking it to write AGS script, and most times it writes a believable, but not accurate code. It often uses non-existing commands too, probably inventing them based on something found elsewhere. In other words, at this point it reminds a child who tries to imitate what adults do.

Ophidic

I managed to get it working, thank you for the response, I got a counter working as planned, I have no idea what I did but I guess just go with it for now. I was starting to freak out because I wasn't getting it lol.

SMF spam blocked by CleanTalk