Import/Export custom structs - arrays (SOLVED)

Started by JpSoft, Sat 24/05/2008 11:58:09

Previous topic - Next topic

JpSoft

I have a custom struct declared in the global script

(this is an example since i dont have the source code with me)

Code: ags
struct People_Profile
  {
  string Name;
  int Age;
  bool Sex;
  }


And a array with that struct

Code: ags

People_Profile Humans[50]; 


It works perfect in my global script, so i guess is well implemented. Now what i need is be able to export all this data to every room, since i got an error importing or exporting since AGS ask for a variable or function (it do not recognize People_Profile with imports)

How i can export the array and import it from the room scripts?

Thanks

Jp

skuttleman

I ran into this issue a while back and I'm pretty sure it's not possible. Even when I got the struct accessible in all my scripts, it wasn't retaining assigned values from one script to another.

My advice would be to have all the read/write functions for the struct in one module (so all the date will be retained) and just call those functions from your room and global scripts.

Sample Module Code:
Code: ags

struct People_Profile
  {
  string Name;
  int Age;
  bool Sex;
  };

People_Profile Humans[50];

funtion UpdateHumans (string Name, int Age, bool Sex, int HumanNum)
{
  Humans[HumanNum].Name = Name;
  Humans[HumanNum].Age = Age;
  Humans[HumanNum].Sex = Sex;
}

function ReadHumans (int WhatToRead, int HumanNum)
{
  if (WhatToRead == 1) return Humans[HumanNum].Name;
  else if (WhatToRead == 2) return Humans[HumanNum].Age;
  else if (WhatToRead == 3) return Humans[HumanNum].Sex;
}

export funtion UpdateHumans (string Name, int Age, bool Sex, int HumanNum);
export function ReadHumans (int WhatToRead, int HumanNum);



Sample Room Code
Code: ags

String GetName;
int GetAge;
bool GetSex;

function myRoomFunction()
{
  GetName = ReadHumans(1, 3); // Reads Humans[3]'s Name
  GetAge = ReadHumans(2, 0); // Reads Humans[3]'s Age
  GetSex = ReadHumans(3, 21); // Reads Humans[3]'s Sex
  Display ("Human #3's Name is %s", GetName);
  Display ("Human #0's Age is %d", GetAge);
  if (GetSex == true) Display ("Human #21's Sex is True");
  else  Display ("Human #21's Sex is False");

  UpdateHumans ("Bob", 27, false, 12);
  // Set's Humans[12] Name to Bob, age to 27, and sex to False;
}


This isn't the best or most efficient way, but it should get the job done and hopefully it puts you on the right track.

Khris

#2
After "People_Profile Humans[50];", put "export Humans;"
In the global header, add "import People_Profile Humans[50];"

In general, just the name is exported, usually after the declaration.
Then copy&paste the declaration to the header and put "import" in front of it.

The only exception is a function inside a struct:

Code: ags
// header
struct myStruct {
  import myFunction(...);
}

// global/main script
function myStruct::myFunction(...) {
  ...
}


EDIT:
Quote from: skuttleman on Sat 24/05/2008 14:27:08
I ran into this issue a while back and I'm pretty sure it's not possible. Even when I got the struct accessible in all my scripts, it wasn't retaining assigned values from one script to another.
Then you've done something wrong, you've probably put the declaration using the struct inside the header.

Code: ags
// header
struct str

import str var

// global script

str var
export var

JpSoft

I guess i miss something...Here is the original code, using AGS 3.0.0

Top of GlobalScript.asc

Code: ags
// main global script file

struct ciudades {          
  String nombreciudad;
  int statusciudad;
  int factoryciudad;
  int labciudad;
  int defensaciudad;
  int infanciudad;
  int cabaciudad;
  int asediociudad;
  int Xciudad;
  int Yciudad;
  };

ciudades ciudad[22];
export ciudad;


Top of GlobalScript.ash

Code: ags
// Main header script - this will be included into every script in
// the game (local and global). Do not place functions here; rather,
// place import definitions and #define names here to be used by all
// scripts.
.
.
.
11 import ciudades ciudad[22];
.
.
. 


What i get...

Code: ags
Failed to save room room1.crm; details below
GlobalScript.ash(11): Error (line 11): expected variable or function after import, not 'ciudades'
Failed to save room room100.crm; details below
GlobalScript.ash(11): Error (line 11): expected variable or function after import, not 'ciudades'


What you mind with "Then copy&paste the declaration to the header and put "import" in front of it."? I must repeat the struct declaration in the main header? After or before line 11? Must i add a import line in the room scripts??? (i also tried it and didnt work neither)

Thanks for the sugerence skuttleman, but it looks like a hell to keep the info updated by that way (even when I'm sure it works)

Jp

Maverick

JP

The solution KhrisMuc gave you is 100% correct (as always).
Declare the struct in the header and the array in global and then just export the array. Using the code you supplied it should look like this:

Top of GlobalScript.asc

Code: ags
// main global script file


ciudades ciudad[22];
export ciudad;


Top of GlobalScript.ash

Code: ags
// Main header script - this will be included into every script in
// the game (local and global). Do not place functions here; rather,
// place import definitions and #define names here to be used by all
// scripts.
struct ciudades {          
  String nombreciudad;
  int statusciudad;
  int factoryciudad;
  int labciudad;
  int defensaciudad;
  int infanciudad;
  int cabaciudad;
  int asediociudad;
  int Xciudad;
  int Yciudad;
  };

.
.
11 import ciudades ciudad[22];
.
.
. 


What i get...

Code: ags
Failed to save room room1.crm; details below
GlobalScript.ash(11): Error (line 11): expected variable or function after import, not 'ciudades'
Failed to save room room100.crm; details below
GlobalScript.ash(11): Error (line 11): expected variable or function after import, not 'ciudades'


What you mind with "Then copy&paste the declaration to the header and put "import" in front of it."? I must repeat the struct declaration in the main header? After or before line 11? Must i add a import line in the room scripts??? (i also tried it and didnt work neither)

Thanks for the sugerence skuttleman, but it looks like a hell to keep the info updated by that way (even when I'm sure it works)

Jp
[/quote]

Khris

Yes, move the struct definition to the header.

See, when a room is compiled, its actual script is global header+room script. If the struct definition is inside the global script and not the header, the room doesn't 'see' it.

monkey0506

Basically to elaborate on what Khris said, your struct defines a new data type. If that definition is in the global script, then none of your other scripts will know what the data type is, or even that it exists (because it won't!). If you put the struct in your script header, then it will automatically be imported into all subsequent scripts. Thereby your struct will exist in all subsequent scripts, and they will understand what the data type is. Without this definition you may as well try to explain the inner workings of a florgsnocker to a prairie dog.

JpSoft

Thanks a lot. Its working perfect now.

Jp

SMF spam blocked by CleanTalk