Hi, I'm making my first game in AGS, and I want to put in a feature which can allow the player to review objectives.
This would be done with a "notebook" inventory item, which when looked at brings up a GUI displaying the objectives that haven't been completed yet. Because the game isn't completely linear, I can't have each new objective taking the place of the last.
I did have a search around the forums for similar threads and found one from a while back: http://www.adventuregamestudio.co.uk/yabb/index.php?topic=31624.0
I don't know it was ever solved. The last post was by Ashen, who suggested an untested piece of code:
function AddReminder(String text) {
lstMemo.InsertItemAt(0,text);
}
function LoseReminder(String text) {
int Temp;
while (Temp < lstMemo.ItemCount) {
if (lstMemo.Items == text) {
lstMemo.RemoveItem(Temp);
Temp = lstMemo.ItemCount;
}
Temp ++;
}
}
The description sounded perfect for what I want to do, which is to have a ListBox within a GUI that displays strings as they are requested, that would be stored elsewhere.
However I don't fully understand how this script works. What do I put in the place of "text"?
From what I do understand I need to put in an array index somewhere, but where and how? I've never used them before, and the section in the manual didn't clarify with me how exactly I was supposed to use them in my script.
Apologies if I'm asking too many little questions. I'm just trying to establish what I do and don't understand.
This script is assuming you have a GUI with a ListBox named lstMemo. Then, if an npc tells the player they need a flower, run the function AddReminder("Get flower."); This will add the text in quotes to your list box.
You actually don't need an array. But using one will help keep your game organized.
~Trent
I gave this a bit more thought, and I personally would do it all manually. Here's a random untested script that I think would work:
GUI named gReminder.
Label named lblReminder.
Button named btnExitRemind. 30pixels wide, 20pixels high.
Change eFontWhatever to your font of choice.
function ResizeGUI () {
int txtWidth = GetTextWidth(lblReminder.Text, eFontWhatever);
int txtHeight = GetTextHeight(lblReminder.Text, eFontWhatever, txtWidth+5);
gReminder.SetSize(txtWidth+10, txtHeight +40);
gReminder.Centre();
lblReminder.Width=txtWidth;
lblReminder.Height=txtHeight;
lblReminder.SetPosition(10, 40);
btnExitRemind.SetPosition( (((txtWidth+10)/2)-15), 10);
}
function AddReminder (String textToAdd) {
if (lblReminder.Text==null) lblReminder.Text="";
lblReminder.Text = lblReminder.Text.Append(textToAdd.Append("["));
ResizeGUI();
}
function RemoveReminder (String textToRemove) {
String lookFor = textToRemove.Append("[");
lblReminder.Text = lblReminder.Text.Replace(lookFor, "");
ResizeGUI();
}
I'll semi-document it soon. BRB
~Trent
As fun and interesting this code was to write, it's currently broken. The GetTextWidth function doesn't take line breaks '[' into account. I can work around this, but I'm sure there's better ways to do this.
~Trent
Ok, but when I try to run the script, it comes up with an error saying "undefined symbol 'text'".
Am I supposed to put something else in the place of "text", or define it elsewhere? If so what and how?
Surely I wouldn't put in a specific example of reminder text in to the function? I assumed it would work by me replacing "text" with the reminder text whenever I ran the function in scripting. However, AGS doesn't seem to like the term "text".
Perhaps I'm just very, very confused.
Just saw your reply after making my big edit. What are you running for your function? Can you post the lines of script where the error is? (remember that you can use the [ code] tags (without the space) on the forum like I did above).
~Trent
I tried your example code, but it came back with the error "undefined token "txtWidth"".
Ah, wait. You said it doesn't work anyway.
Well back to Ashen's suggestion:
function AddReminder(String text) { // <------ this is where the error comes up.
lstMemo.InsertItemAt(0,text);
}
function LoseReminder(String text) {
int Temp;
while (Temp < lstMemo.ItemCount) {
if (lstMemo.Items == text) {
lstMemo.RemoveItem(Temp);
Temp = lstMemo.ItemCount;
}
Temp ++;
}
}
The error is that AGS says "text" is an undefined symbol. Am I supposed to define it somewhere and somehow?
I've been playing with this code some more and reading up on arrays and such. It still doesn't make complete sense to me.
This is the exact code I'm using at present
function AddReminder(String text) {
if (text == reminder[13]) {
lstJournal.InsertItemAt(0, text); }
}
function LoseReminder(String text) {
int Temp;
while (Temp < lstJournal.ItemCount) {
if (lstJournal.Items == reminder[13]) { // <------------- error here
lstJournal.RemoveItem(Temp);
Temp = lstJournal.ItemCount;
}
Temp ++;
}
}
I think I'm supposed to create an array called "reminder". Is that correct?
This array would have fourteen different strings. I read a beginner's guide to arrays that said I would have to declare them in the global script file within game_start. I do this like so:
reminder[0] = "Go talk to police.";
reminder[1] = "Go to Hall's Garage.";
and so on. This works ok, the code compiles fine until the point marked above. The error says "expected array index after Listbox::Items". I thought that was an array index that I put after that part.
I've been busting my head trying to make sense of this alone for days. If someone could help me understand what I'm supposed to be doing it would be greatly appreciated.
Quote from: Jono on Thu 13/11/2008 12:35:59
I've been playing with this code some more and reading up on arrays and such. It still doesn't make complete sense to me.
You don't need to use arrays for this. However, there's an error on that line because ListBox.Items is a collection of items -- I've corrected the code below:
function AddReminder(String text) {
lstJournal.InsertItemAt(0, text);
}
function LoseReminder(String text) {
int Temp;
while (Temp < lstJournal.ItemCount) {
if (lstJournal.Items
[Temp] == text) {
lstJournal.RemoveItem(Temp);
Temp = lstJournal.ItemCount;
}
Temp ++;
}
}
As Pumaman has pointed out, the items in lstJournal are themselves an array, and so you need to use an array index there too:
if (lstJournal.Items[Temp] == reminder[13]) {
This is what is causing the compiler error, but your code still looks as though it might not work. I don't know if you need these lines:
Temp = lstJournal.ItemCount;
and
Temp ++;
(There should be no space before the ++ in that last line, by the way.)
Surely once you have removed the line you want to remove, you don't need to look at any more of the items.
I also notice you pass in a string "text" that isn't used anywhere in the function - is this the text you are looking for, or is it always reminder[13]?
It seems to be compiling fine now, thankyou.
Now I'm trying to get the code working in game. I have the listbox lstJournal on the GUI gReminder.
I figured I could just set gReminder to visible when the function iJournal_Look() is run.
However, I get a "parse error" at "gReminder."
Here's the code I'm using for that.
function_iJournal_Look() {
gReminder.Visible == true;
}
Am I doing something wrong without realising it?
== is for comparing, = for setting.
Ah. I should've noticed that. Thanks everyone who posted for your help and patience.