Ok, first of all, I'm sorry that I'm cluttering the section with my stupid questions.
I can't seem to get a hold on how to use the arrays right. I have a programming background, but PHP doesn't seem to have taught me anything about strongly typed languages, and so I'm stuck.
Basically, I have a script module (which I decided to use as a data junk yard) and it has an array of strings which my character is supposed to speak when he looks at the chair.
The code is (MCData.asc):
string TalkInanimateArray[];
TalkInanimateArray[0] = "I don't usually talk to inanimate objects. 1";
TalkInanimateArray[1] = "I don't usually talk to inanimate objects. 2";
TalkInanimateArray[2] = "I don't usually talk to inanimate objects. 3";
function getTalkInanimate(int number) {
return TalkInanimateArray[number];
}
With a header (MCData.ash):
import function getTalkInanimate(int number);
Then, in the room script, I have this code:
function oChair_Talk()
{
int i;
i = Random(2);
cMainCharacter1.Say(getTalkInanimate(i));
}
And I get an error:
Quoteroom1.asc(18): Error (line 18): Type mismatch: cannot convert 'int' to 'const string'
There's something I grasped wrong, it seems. Appreciate any help.
Since your function is supposed to return string, you should declare it appropriately:
String getTalkInanimate(int number);
Just for reference, "function" is a substitute for "int". So you actually declared "int getTalkInanimate(int number)", which is why the error message says it can't convert int to string.
Also, unless that was just an example, you can achieve "doesn't work" replies without adding a function for each object; just add this to your global script:
void unhandled_event(int what, int type) {
if (what == 2) { // objects
if (type == 2) { // talk to
cMainCharacter1.Say(getTalkInanimate(Random(2)));
}
...
}
...
}
Note that I used "void" instead of "function" because it doesn't return anything, although it doesn't really matter.
This function is called whenever an interaction event does not have a function associated with it. Which means the above will cover all Objects unless you override it by linking your own function.
Wow, that's new, haha. Thanks a lot.
I re-wrote it into this, took me a while actually to get all the types right:
String TalkInanimateArray[];
String[] initTalkInanimateArray() {
TalkInanimateArray = new String[3];
TalkInanimateArray[0] = "I don't usually talk to inanimate objects. 1";
TalkInanimateArray[1] = "I don't usually talk to inanimate objects. 2";
TalkInanimateArray[2] = "I don't usually talk to inanimate objects. 3";
return TalkInanimateArray;
}
String getTalkInanimate(int number) {
String result[] = initTalkInanimateArray();
return result[number];
}
And thanks for the unhandled_event tip, I'm gonna use it there.
tinyhippo, with your new code you are creating a new array every time you call getTalkInanimate!
Instead you may do this:
String TalkInanimateArray[];
void initTalkInanimateArray() {
TalkInanimateArray = new String[3];
TalkInanimateArray[0] = "I don't usually talk to inanimate objects. 1";
TalkInanimateArray[1] = "I don't usually talk to inanimate objects. 2";
TalkInanimateArray[2] = "I don't usually talk to inanimate objects. 3";
}
function game_start()
{
initTalkInanimateArray();
}
//
// When getting a string, either use array directly:
String my_text = TalkInanimateArray[number];
// if you really need a function:
String getTalkInanimate(int number) {
return TalkInanimateArray[number];
}
That's a good point, yeah, I've missed that. But I'm guessing that the game_start function will be cluttered rather unpplesantly in the end.
May be there is a way to initialize the array somewhere inside the script module? I'm guessing it's impossible to call functions in the global scope outside of a function?
EDIT:
So what I did was make an overall module function initMainCharacterModule() and there I put my function that initializes the array, then I put this function inside the game_start function. I'll probably do another function to enclose all the module initializers and then put it into the game_start instead to reduce clutter further. Kinda used to a deeper abstraction, haha.
You can put "game_start" in every module, except room script. So as "repeatedly_execute" and "repeatedly_execute_always".
"on_key_press" and "on_mouse_click" may be put in every module too, even in room script.