Problem importing a script into global header.

Started by kyodai, Thu 22/03/2012 19:12:50

Previous topic - Next topic

kyodai

OK, so what i wanted to do seems to be fairly easy, make a global function for my Text output to be displayed in a textbox in my GUI, to have a neat way of calling it.


So i go to GlobalScript.asc, place my function which looks like this:

Code: ags

function TextOut(string myText) 
{
  txtMainGUI.Text = myText;
}




So after a while when trying to call it i noticed the global script is "not so global" as my room script cant find the function in the global script. After reading a bit i see i have to "import" it in the header. God knows why. Anyways, so I am trying the import im my Globalscrip.ash.


Code: ags


import function TextOut (string);




And now i get the error "Error (line 80): Type mismatch: cannot convert 'const string' to 'string'"



Damn it, where did i say const? Can it really be so hard? I am out of ideas. I hope some gurus can enlighten me how to just make a simple global function. It should be very easy, but although i can program a bit VB6 and .net I am biting in my keyboard here.


Khris

Code: ags
import function TextOut(string myText);


You're wondering why you have to import stuff from _another_ script...?

kyodai

Quote from: Khris on Thu 22/03/2012 19:17:08
Code: ags
import function TextOut(string myText);


You're wondering why you have to import stuff from _another_ script...?


Yeah, i fell for the "suggestive" term "Global script". I thought "Global" equals to "accessible from everywhere, yay". I dun get why you have to import it into a header first.

But anyways, i dun wanna discuss about that, it is at it is. I accept that. Maybe if i was a C++ programmer i would understand it, but alas from a VB6 point of view i was indeed surprised.



Anyways, i really just wanna be able to call my function from all room scripts. The manual states that you just have to import the function into the global header. SO whats the trick with the "const string" to "string" conversion?

Khris

You cannot access a variable defined in room A's script from room B's script. However, a variable/function defined in the global script is accessible from all room scripts (after importing it). Hence the name "global".
Not sure why the name "global" would imply that every object is automatically exposed to all scripts (which btw would have the disadvantage of a cluttered auto-complete window).
AGS script is derived from C/C++, so yeah, any VB6 stuff should go out the window.

The error you got was due to the missing variable name. Did you miss the code correction in my previous post? Also, you're supposed to use String, not string.

(And don't quote the entire previous post. There's no reason to do so.)

monkey0506

string, const string, and String are all very different data types (none of which should be confused with string literals ("quoted text")).

string is referred to as an "old-style" string. It is internally defined as a char[200] (array of 200 chars) with some special provisions. String literals cannot be implictly converted to a string, you must use StrCopy, StrCat, etc. A string can be passed as a parameter to a function expecting a const string, but not the other way around.

const string is an old-style string with some special provisions. Specifically its value is constant, and cannot be changed by functions such as StrCopy or StrCat. As a const string is an old-style string, you must use old-style string commands when working with it (such as StrLen). String literals can be implicitly converted to a const string (that is, you can pass a string literal as a parameter to a function expecting a const string). A const string can be implicitly converted into a String (such as during assignment).

String is a new-style string. It can be passed as a parameter to functions expecting a const string or String, but not string. String literals and const strings can be implicitly converted to String. A String does not use the old-style string commands, but instead uses the new-style string properties and functions (such as String.Copy, String.Append, String.Length, etc.).

The only reason you need to use string/const string is if you're interoperating with legacy code. 2.71 was SEVERAL versions ago, so short of very long-term projects that have been around since before that time, most games should not be using old-style strings. There is no benefit behind using old-style strings vs. new-style strings (arguably of course, as everything can be a matter of opinion).

kyodai

Ahhhh omg, now i realized the capital "S" in String did the trick. That's really a good trap for beginners. XD

I was missing the string name in the import and i just wrote string lowercase. I was not aware that AGS makes a difference between uppercase/lowercase.


Now it's working as i expected it. Thanks to both of you!

monkey0506

All real languages are case-sensitive. Everyone knows that. :P

And it's not that good of a trap seeing as "Enforce new-style strings" is true by default in the editor, which means that the old-style strings and their commands won't even compile.

Khris

Heh, I didn't know that the import line only requires the datatypes. I knew that it's possible to rename the parameters, but omitting them? Oh well, you never stop learning.

Also, as long as you never call the function, using "string" will compile.

SMF spam blocked by CleanTalk