importing a struct

Started by rancidrat, Wed 08/08/2007 16:40:10

Previous topic - Next topic

rancidrat

Lo
I am a bit of a newbie at this...
I recently discovered how to use structs and I think they are the coolest things since sliced bread. The problem is that I cant figure out how to export and import them cuz the manual only deals with importing a single variable.
Say I did this in the Global script or i a Module:

struct product {
  int price;
  int sellprice;
}

product beer;
beer.price = 40
beer.sellprice = 60

1. How do I export it?
export beer;  ?

2.Now if I want to access this from any script how do I import it cuz..
import int beer;  - does not work
import int beer.price; - does not work

I would like to be able do do something like this in any room script:

Display(" We are trading Beer for %d credits and you can sell it for %d 
               credits",beer.price, beer.sellprice);
Thanks


Khris

Just to clarify: the struct declaration goes into the header.
Then, also in the header, use
import product beer;
In the global/module script, use
product beer;
export beer;

Ashen

This is mentioned (VERY briefly) in the BFAQ entry on variables:
Quote
vartype can be String (for strings of alphanumeric text), bool (for values that can be true or false), int (for whole numbers), float (for decimal numbers), an enumerated type, or a user defined type (however, structs cannot be imported unless the struct definition is in the script header).

It doesn't go into more detail because, well, it's little beyond 'the basics'. Maybe a 'More about Variables' entry would make sense? I'm away starting tomorrow, but someone can add it, if it seems necessary
I know what you're thinking ... Don't think that.

rancidrat

Thanks, I got it now, You guys should be in every major city! :)

NOTE: You cannot currently import string variables.

Just a pity I can't import and export my own strings that are in a struct yet... maybe they could add that in future.

Khris

You don't need to import/export the members of a struct.
If the instance is global, you can access it and its members from anywhere, even if they are strings.

header:
struct test_struct {
  String s;
};

import test_struct ts;

main script:
test_struct ts;
export ts;
...
// inside a function
  ts.s="Contents.";

monkey0506

Just to try and clarify what Khris said, as of AGS 2.71 you can use the String type which was used to replace the old-style string type (notice the capital 'S'; this does make a difference).

You can import/export Strings and use them in structs.

SMF spam blocked by CleanTalk