Hi there everyone, I'm trying to create some accesor like
MyGlobalVars.Entrance.HasPackage = false;
MyGlobalVars.Backyard.Grill.IsOn = true;
MyGlobalVars.Backyard.Pool.IsEmpty = true;
But I'm not achieving in a simple way I've created a `struct` that contains those each member like
Backyard{
bool IsEmpty = false;
}
But I'm going in a loop of failures.
Other case is that I create all variables in a new script using the naming convention MyVarsBackyardGrillIsOn; and the import those but I don't really like it taht way.
Thanks in advance.
There's a manual entry on structs, with examples:
https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html#struct
Some of the game templates must have structs too, although they often use "attributes", which is a more advanced thing that you might not need at this point.
The structs are declared as
struct Backyard {
bool IsEmpty;
}
This is just a struct's description, it's not an actual object containing data yet.
Declaring an object of such struct is:
Backyard backyard; // here you declare a variable "backyard" of type Backyard.
You need to put this object inside some script, and then declare an export of this object nearby, and also import of this object in the script's header for other scripts to see.
Just in case, how to share variables between scripts:
https://adventuregamestudio.github.io/ags-manual/ImportingFunctionsAndVariables.html
There's are two limitations in AGS currently:
You cannot put default values when declaring struct, because in AGS script there are no "constructors". All the variables will be zero/false at the start.
If you want to set up starting values, for single global object you may do that in game_start function, for structs in rooms you may do this in "room load" event.
Another problem is that you cannot put structs inside structs. There's a new advanced script compiler in the upcoming experimental version that allows that, but it's not formally released yet.
Thanks for your explaination Crimson Wizard
I was trying to achieve the same as you showed me, the issue that I have is this
MyVars.ash
struct RoomCoordXY{
 int x;
 int y;
 String charDirection;
};
RoomCoordXY EntranceRoom;
export EntranceRoom;
game_start
EntranceRoom.x = 22;
But when I access within the room the value is 0
My idea is, because I use lots of XY and Direction coords I'm defining the Room particulars id, x, y, directionPick, directionOut, etc etc
And I would like to use it when interactig with doors and changing rooms.
When I add the headers the EntranceRoom is undefined, and when I can access, the value is never changed :confused:
You do the export/import wrong.
https://adventuregamestudio.github.io/ags-manual/ImportingFunctionsAndVariables.html#exporting-and-importing-a-variable
In MyVars.ash you should have:
struct RoomCoordXY{
int x;
int y;
String charDirection;
};
import RoomCoordXY EntranceRoom;
In the MyVars.asc you should have:
RoomCoordXY EntranceRoom;
export EntranceRoom;
Excellent, now I can set objects and hotspost data and reuse it! thanks again for your time and help Crimson Wizard :smiley:
Sorry I revive this, but something I don't get it is why I cannot use my exported variables in other scripts
I can use it anywhere, on rooms scripts and functions, but when used crossed-scripts I get Undefined symbol DungeonRoomMetalDoor
MyVar.asc
RoomCoordXY DungeonRoomMetalDoor;
export RejillaRoomDoor;
MyVar.ash
struct RoomCoordXY{
int ID;
int x;
int y;
CharacterDirection charDirection;
};
import RoomCoordXY DungeonRoomMetalDoor
MyCutScenes.asc
void cutscenePreWar()
{
player.Walk(DungeonRoomMetalDoor.x, DungeonRoomMetalDoor.y, eBlock);
}
There, complie error :undecided:
A script upper in the script list can't depend from a script below it in the script list. This is likely the cause. This is not true I think from ags4 forward, where the script order doesn't matter.
ooooohhhh order matters.... :=
Thanks eri0o
Since AGS 3.6.0 you may also import variables from lower scripts too, but the import declaration must be higher than the usage anyway.
You may even place import declaration inside a script's body.
For example, if you have scripts like
- MyCutScenes
- MyVars
and you have global vars in MyVars, then you may make them available in MyCutScenes without changing script order, if you declare their import second time inside MyCutScenes.asc. This will make MyCutscenes.asc "see" them.
Quote from: Crimson Wizard on Mon 12/09/2022 01:33:18
Since AGS 3.6.0 you may also import variables from lower scripts too, but the import declaration must be higher than the usage anyway.
You may even place import declaration inside a script's body.
For example, if you have scripts like
- MyCutScenes
- MyVars
and you have global vars in MyVars, then you may make them available in MyCutScenes without changing script order, if you declare their import second time inside MyCutScenes.asc. This will make MyCutscenes.asc "see" them.
Excellent thanks for that Crimson Wizard really appreciated.
Reordering now the imports and script names fixed.
I think I will change the approach, Rooms Coordinates into
#define ROOM_NAME_DOOR_NAME x, y
It's less MUCH less amount of work than creating exporting and importing 🤦
Just adding to the MyCoords.ash maybe this helps people.