How do I use structs?

Started by Reid, Mon 26/03/2007 05:41:43

Previous topic - Next topic

Reid

Hi all,

I started using AGS about a week ago and I'm loving it so far. My current plans are to create a test case where doing different actions will dynamically change a character's behavior. To do this I created a struct called char_mind in the global script like so:

struct char_mind
{
  int calmness;
  int IQ;
  int prejudice;
  String name;
};

char_mind guard01
guard01.calmness = 10;
guard01.IQ = 110;
guard01.prejudice = 10;
guard01.name = "Rob";

Then, in my interaction scripts, when the player interacts with a character, I want to modify the values of the variables defined from the struct and then based on the changed value the character will say different things, etc. I'm having trouble being able to do this though and I'm not sure if I'm placing my code in the right sections. Anyone have ideas? Thanks.

monkey0506

struct char_mind
{
  int calmness;
  int IQ;
  int prejudice;
  String name;
};

char_mind guard01;
guard01.calmness = 10;
guard01.IQ = 110;
guard01.prejudice = 10;
guard01.name = "Rob";


But what isn't working? What does it actually do? What doesn't it do? Where are you actually placing your scripts at? In short, more info please? ;)

Windenwart

#2
Hi,

this is my struct for handling open an closed doors:

Code: ags

struct door {
  bool door_open;
  int  door_object;
  int  door_hotspot;
  int  door_area;
};


when I declare a new "door" then I become an error:

Code: ags

door dleucht; // Türe am Leuchtturm
dleucht.door_open    = false; ---> ERROR: "unexpected dleucht"
dleucht.door_object  = 0;
dleucht.door_hotspot = 2;
dleucht.door_area    = 2;


I don't know what's wrong...

EDIT: With the "sword" example from help I see the same error...

Gilbert

Where did you put the value assignments?
(Specifically the following lines)
Code: ags

dleucht.door_open    = false; ---> ERROR: "unexpected dleucht"
dleucht.door_object  = 0;
dleucht.door_hotspot = 2;
dleucht.door_area    = 2;


They MUST be inside functions to work, you cannot assign their values outside of functions (if that's what you'd done).

Ashen

Topics merged - there's no need for TWO threads about structs in one day!
I know what you're thinking ... Don't think that.

Windenwart

#5
@Gilbot : That's it. Thank you!

It's impossible to use struct's as parameter?

Code: ags

function OpenDoor(door *my_door)
{
  my_door.door_open = true;
  object[my_door.door_object].Visible = false; // object image of closed door
  hotspot[my_door.door_hotspot].Enabled = true; // hotspot of opened door
  RestoreWalkableArea(my_door.door_area); // walkable area from door into the room
}
....
  OpenDoor(dleucht);
...


SSH

Quote from: Windenwart on Mon 26/03/2007 12:15:37
@Gilbot : That's it. Thank you!

It's impossible to use struct's as parameter?


Yes, but...

Code: ags

struct door {
  bool door_open;
  int  door_object;
  int  door_hotspot;
  int  door_area;
  import function open();
};

function door::open()
{
  this.door_open = true;
  object[this.door_object].Visible = false; // object image of closed door
  hotspot[this.door_hotspot].Enabled = true; // hotspot of opened door
  RestoreWalkableArea(this.door_area); // walkable area from door into the room
}


12

Windenwart

#7
Great! Thanks!

I'am a beginner in object oriented programing, but I love it.

Windenwart

#8
Hm.... other problem:

Code of "script header":

Code: ags

struct door
{
  bool door_open;
  int  door_object;
  int  door_hotspot;
  int  door_area;
  import function Init(bool door_open, int door_object, int door_hotspot, int door_area);
  import function Open();
  import function Close();
  import function Update();
};
door dleucht;


Code of "global script":

Code: ags

function door::Init(bool door_open, int door_object, int door_hotspot, int door_area)
{
  this.door_open    = door_open;
  this.door_object  = door_object;
  this.door_hotspot = door_hotspot;
  this.door_area    = door_area;

  Display("%d", this.door_open); // = 0
  Display("%d", this.door_object); // = 0
  Display("%d", this.door_hotspot); // = 2
  Display("%d", this.door_area); // = 2
}

function door::Open()
{
  Display("%d", this.door_open); // = 0
  Display("%d", this.door_object); // = 0
  Display("%d", this.door_hotspot); // = 0 ??? should be 2
  Display("%d", this.door_area); // = 0 ??? should be 2

  this.door_open = true;
  object[this.door_object].Visible = false;
  hotspot[this.door_hotspot].Enabled = true;
  RestoreWalkableArea(this.door_area);
}

function game_start() // called when the game starts, before the first room is loaded
{
  dleucht.Init(false, 0, 2, 2); // Tüare am Leuchtturm
}


Code of "room script":

Code: ags

...
dleucht.Open()
....


When I debug all values of "dleucht" in Open() function, then I can see that all values of "dleucht" are NULL...

strazer

Quote from: Windenwart on Mon 26/03/2007 14:00:24door dleucht;

If you define a variable or create an instance of a struct directly in the script header, a separate entity will created for each room (and the global script).
If you want to make one global entity for all rooms, you have to define it in the global script, export it and then import it into the script header:

Code: ags

//global script

door dleucht;
export dleucht;


Code: ags

//script header

import door dleucht;

Windenwart

OK, that's nonpractical for me . Now I use an array to save all doors...

deltamatrix

Hi,

I guess this classifys as a beginner question despite my previous experience. Ok, its been a long time and AGS has changed a lot.

Anyway, I have declared a struct in the script header along with an instance of it which I can access and modify perfectly well within the global script.
However, I can't seem to access it in room scripts. Although the compiler don't crash attempting to access it, the values of the struct are no longer instantiated.

For example:

mystruct.value = 64;

In the global script, it shows at 64 but in the room scripts, it shows as 0. Whats going on?
BAD WOLF - TORCHWOOD - MR SAXON - THE BEES ARE DISAPPEARING - PANDORICA - RIVER SONG

Ashen

#12
Since it's only been a few days, I'll keep all the struct questions in one thread.

Do you declare the instance in the Header as well? That'll create a spereate instance for each Room - so the version you set in the Global script wouldn't have any effect (it'd still be 0 in the room script). See strazer's post two above yours for the proper way to declare a global instance of a struct. (And you really should've seached the forum - this thread was still on the front page.)

If that's not the problem, can you show your code?
I know what you're thinking ... Don't think that.

deltamatrix

Thanx. Thats all I need to know. ;)
BAD WOLF - TORCHWOOD - MR SAXON - THE BEES ARE DISAPPEARING - PANDORICA - RIVER SONG

Reid

Hey all,

In the little time I have to play around with this, I'm still stuck creating my struct, :( Here's the info.

Global Script
Code: ags
// main global script file

Char_Mind charminds[2]; // here I want to create an array called charminds using the Char_Mind struct


Global Script - function
Code: ags
#sectionstart create_struct
function create_struct()
{
  charminds[cSeller.ID].calmness = -3;
	charminds[cSeller.ID].IQ = 90;
	charminds[cSeller.ID].prejudice = 75;
	charminds[cSeller.ID].name = "SellerDude";
}
#sectionend create_struct

I did the above because some searching on these forums someone said they needed to create the definition in a function. At this point I'm just stabbing in the dark, doing trial and error without any understanding why, hoping I discover what works.

Header Script
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.
import function create_struct ();

struct Char_Mind
{
  int calmness;
  int IQ;
  int prejudice;
  String name;
};


Room Script
Code: ags
#sectionstart hotspot4_a  // DO NOT EDIT OR REMOVE THIS LINE
function hotspot4_a()
{  
	// script for Hotspot 4 (Hotspot 4): Look at hotspot
  create_struct();
	trust[cSeller.ID] = -6;
	Display("Trust for Seller is %d", trust[cSeller.ID]);	
	Display("Calmness for Seller is %d", charminds[cSeller.ID].calmness);
}
#sectionend hotspot4_a  // DO NOT EDIT OR REMOVE THIS LINE


When I save my work, I get the following error:
Code: ags

The problem was In 'Room 1 script'
Error (line 205): undefined symbol 'charminds'


Which leads to this
Code: ags

Display("Calmness for Seller is %d", charminds[cSeller.ID].calmness);


I'm sure I have code in the wrong place, but don't know where. Thanks in advance for any help.

Gilbert

In global script, after defining charminds[], add the following line:
export Char_Mind charminds;

In the header script, add at the end:
import Char_Mind charminds[2];

I didn't check your codes, so I don't know actually what you are going to do, but I think there're probably other loose things that need to be addressed.

strazer

(Gilbot was faster, posting anyway)

You define charminds in the global script, so only there can it be used. Like any other variable defined in the global script, if you want to also use it in all rooms scripts, you have to export it from there, then import it into the script header (or the specific room where you need it):

Code: ags

// main global script file

Char_Mind charminds[2];
export charminds;


Code: ags
// Main header script

//struct here

import Char_Mind charminds[2];


Btw, you don't need the #sectionstart and #sectionend stuff for your own functions. AGS uses these markers to find its own functions in the script, you don't need them for your own. They don't hurt either, though.

Reid

Thank you Gilbot V7000a and Strazer, I tried out your suggestions this evening and it works. I was able to interact with the environment and change character properties and then when interacting with the character, based on their properties they respond to me differently.

SMF spam blocked by CleanTalk