A problem with using arrays

Started by tinyhippo, Wed 27/08/2014 12:57:24

Previous topic - Next topic

tinyhippo

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):
Code: ags

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):

Code: ags

import function getTalkInanimate(int number);


Then, in the room script, I have this code:

Code: ags

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.

Crimson Wizard

Since your function is supposed to return string, you should declare it appropriately:
Code: ags

String getTalkInanimate(int number);

Khris

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:
Code: ags
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.

tinyhippo

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:
Code: ags

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.


Crimson Wizard

tinyhippo, with your new code you are creating a new array every time you call getTalkInanimate!

Instead you may do this:
Code: ags

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];
}

tinyhippo

#5
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.                                                                                                                                                                                                                     

Crimson Wizard

#6
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.

SMF spam blocked by CleanTalk