(SOLVED) Type mismatch when I try to call a function

Started by EliasFrost, Tue 29/10/2013 13:47:25

Previous topic - Next topic

EliasFrost

Hey, I'm having a bit of trouble with one of my scripts. In my global script I have defined a function that is meant to display a tooltip based on the string argument given to the function, it looks like this:

Code: ags
function tooltip(string tiptext)
{
    Tiplabel.Text = tiptext;
    gTip.Transparency = 100;
    gTip.Visible = true;
    while (gTip.Transparency >= 10)
    {
      gTip.Transparency -= 10;
      Wait(1);
    }
    gTip.Transparency = 0;
    WaitMouseKey(2000);
    aTip_close.Play();
    while (gTip.Transparency <= 90)
    {
      gTip.Transparency += 10;
      Wait(1);
    }
    gTip.Visible = false;
}


But when I try to call the function

Code: ags
tooltip("hey");


from one of my room scripts I get this error:

room2.asc(56): Error (line 56): Type mismatch: cannot convert 'const string' to 'string'

Which I guess means that the argument I give doesn't match the type in the function right? But I don't see the problem, what constant is the error refering to?

Thanks in advance! Elias

Ghost

#1
That's a case of string vs. String. AGS is case-sensitive and you need to use a String instead of a string.

As a rule, always use the String variable- you can use String.Format on them, use them as pointers, and do all the stuff a "true" programming language would allow you.

EliasFrost

I found the problem, it seems that string is not a valid type? but String (with a capital S) is and it works now. Can someone shed some light on why String works but not string?

EDIT: We posted at the same time, I found the solution but thanks for the answer Ghost :)

Ghost

#3
No prob (laugh)

A more experienced programmer will be able to explain in greater detail, but technically:
String is used as a variable. So you will want to use that.
string is used by the engine to hold "constant" values.

edit:
Thanks for clearing that up, Cimson! (nod)

Crimson Wizard

#4
Quote from: Ghost on Tue 29/10/2013 13:54:53
string is used by the engine to hold "constant" values.
No, not constant values. These are old-style strings which were 200 characters max.
In exotic cases they may be more effective to work with, because they are practically raw arrays of chars. But they have their limitations, and probably tad less safe. (edit: fixed last sentence)

EliasFrost

So it's similar to the difference between strings and C-strings in C++/C?

Crimson Wizard

Quote from: Frostfalk on Tue 29/10/2013 14:15:34
So it's similar to the difference between strings and C-strings in C++/C?
Yes, to some degree.
For example, you can convert real array of chars to old string, as shown in the example on this wiki page:
http://www.adventuregamestudio.co.uk/wiki/Array_decay

EliasFrost

Interesting read, I'm still a novice at programming (hence why I go with AGS, it's a great tool for learning) and there are still lots of concepts I'm trying to get my head around, good thing we've got lots of helpful people around to provide useful information. Thanks! :)

SMF spam blocked by CleanTalk