Author Topic: Import function in header error  (Read 174 times)

Import function in header error
« on: 22 Dec 2018, 19:27 »
Hi,
What am I doing wrong in the following?

In GlobalScript.asc I have this function:
Code: Adventure Game Studio
  1. bool IsItemInList(String[] itemList, String itemName)
  2. {
  3.   for(int i=0; i< itemList.Length; i++)
  4.   {
  5.     if(itemList[i] == itemName)
  6.       return true;
  7.   }
  8.   return false;
  9. }
  10.  

In GlobalScript.ash:
Code: Adventure Game Studio
  1. import bool IsItemInList(String[] itemList, String itemName);
  2.  

I'm getting:
"Failed to save room room301.crm; details below
GlobalScript.ash(23): Error (line 23): PE02: Parse error at 'itemList'"

Why room 301? What's the connection to the function I added?
I use the function in other rooms but not in 301.

Thank you!

Khris

  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on one or more games that was nominated for an AGS Award!
Re: Import function in header error
« Reply #1 on: 23 Dec 2018, 13:31 »
For parameters you need to put the [] behind the variable name:
Code: Adventure Game Studio
  1. bool IsItemInList(String itemList[], String itemName)

Snarky

  • Global Moderator
  • Private Insultant
    • Best Innovation Award Winner 2018, for his numerous additions to the AGS open source ecosystem including the new Awards Ceremony client and modules
    • Snarky worked on one or more games that won an AGS Award!
    •  
    • Snarky worked on one or more games that was nominated for an AGS Award!
Re: Import function in header error
« Reply #2 on: 23 Dec 2018, 13:43 »
Also, does AGS really have an array-length property? I have no memory of that existing. (And if this compiles, I would suspect that it's actually checking the String.Length property.)

Re: Import function in header error
« Reply #3 on: 23 Dec 2018, 15:54 »
No, sadly in AGS arrays still don't have Length property.

There are two workarounds:
1) store array's length as a separate variable and also pass it to functions;
2) always keep one extra element in array which serves as the indicator of its end (this is how strings work in C programming language for example);
the second method will only suit you if there is a distinct value that is not allowed anywhere else in array (like "null" pointer).

Re: Import function in header error
« Reply #4 on: 23 Dec 2018, 18:37 »
Thank you for your help.

Maybe I can further elaborate what I tried to do, because given the mentioned restrictions I don't think what I tried to do can work.
I'm trying to create a notebook, that when important information is discovered by the player, that data will be "written" to the notebook.
For that I tried to use a GUI with a listbox, so I can use the listbox.AddItem(), and add new items.
Of course, the problem is not adding the same thing twice.
So how do I check if a value exists before adding?

Khris

  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on one or more games that was nominated for an AGS Award!
Re: Import function in header error
« Reply #5 on: 23 Dec 2018, 19:15 »
Why not test the listbox items directly?

Assuming you have an array of strings containing the items that will get added, you can just compare the item you want to add directly against the listbox items.
I usually dislike this approach because it doesn't separate game logic from presentation properly, but it should be fine here.

Code: Adventure Game Studio
  1. void AddItemToNotebook(String item)
  2. {
  3.   bool found = false;
  4.   for(int i=0; i< lbNotebook.ItemCount; i++)
  5.     if(lbNotebook.Items[i] == item) {
  6.       found = true;
  7.       break;
  8.     }
  9.   if (!found) lbNotebook.AddItem(item);
  10. }
  11.  

That way, using
Code: Adventure Game Studio
  1.   AddItemToNotebook(note[10]);
will only add the item the first time it's called.

Re: Import function in header error
« Reply #6 on: 23 Dec 2018, 19:25 »
Exactly what I wanted!
Works great!
A super thanks to you Khris  :smiley: