struct name {
Declares a custom struct type in your script.
Structs allow you to group together related variables in order to make your
script more structured and readable. For example, suppose that wanted to store
some information on weapons that the player could carry. You could declare
the variables like this:
int swordDamage;
int swordPrice;
String swordName;
but that quickly gets out of hand and leaves you with tons of variables to keep track of.
This is where structs come in:
struct Weapon {
int damage;
int price;
String name;
};
Now, you can declare a struct in one go, like so:
Weapon sword;
sword.damage = 10;
sword.price = 50;
sword.name = "Fine sword";
Much neater and better organised. You can also combine structs with arrays:
// at top of script
Weapon weapons[10];
// inside script function
weapons[0].damage = 10;
weapons[0].price = 50;
weapons[0].name = "Fine sword";
weapons[1].damage = 20;
weapons[1].price = 80;
weapons[1].name = "Poison dagger";
structs are essential if you have complex data that you need to store in your scripts.
|