Passing a pointer to my function compiling error. (SOLVED!)

Started by Ionias, Tue 10/04/2007 05:59:08

Previous topic - Next topic

Ionias

Ok, I have a custom function:

Code: ags

function Speech(Character *who, String msg) {
  // custom display speech on the screen function
  }

Now that works. However, I would like to pass this to my function:

Code: ags

// global script
struct ExtendedCharacter {
  int mad_loop;
  int happy_loop;
  Character* Char;
  };

ExtendedCharacter ecFatman;

function Speech(ExtendedCharacter *who, String msg) {
  // custom display speech on the screen function
  }


So this way I keep my function all nice and neat. However, whenever I try to compile the script I get this error:

There was an error compiling your script. The problem was:

In: 'Global script'

Error (line 43): Cannot declare pointer to non-managed type

I just can't wrap my brain around what I'm doing wrong.  :(

Gilbert

#1
I think you just can't declare a pointer object in a struct, it's a limitation to the script system at the moment.

Edit: Maybe I'm wrong, let me check further.
Edit2: Alright, checked the wiki:
QuoteStructs can only have pointers as members in AGS 2.71 and later.
What version of AGS are you using?
Edit3: Checked again with your codes, the problem is with putting ExtendedCharacter* as parameter of a function, seems that there's a limititation of not letting (user-defined) non-managed types to pass as parameter.
In your case, instead declare Speech() as member function of ExtendedCharacter() and it should work. So:

Code: ags

// global script
struct ExtendedCharacter {
  int mad_loop;
  int happy_loop;
  Character* Char;
  import int Speech(String msg);
  };

ExtendedCharacter ecFatman;

function ExtendedCharacter::Speech(String msg) {
  // custom display speech on the screen function
  }



So you can use this.Char in the body of Speech() to refer to the actual character pointer, and just use the function like
ecFatman.Speech("haha");

Ionias

#2
Excellent work around sir! I shall give it a try.

Edit1: That will work, thanks. :)

Edit2: Ok doing that for every character was getting a bit complicated so in case anyone is reading this I solved the problem a litle more simply by using this instead:

Code: ags

function Speech(String msg, int who = 99) {
  // custom display speech on the screen function
  if (who == 99) {
    who = player.ID;  // defaults to player character
    }
   mad_loop = character[who].GetProperty(“MadLoop”);

etc…  
}


Now my function can display speech based on the default of the player character and have all the custom stuff I need. Not sure why I didn't think of that in first place. :)

monkey0506

Although using custom properties instead of a new struct is a reasonable workaround for your problem (and probably more sensible so long as your properties don't need to change), you could still use a pointer in your function:

Code: ags
function Speech(Character* who, String msg) {
  if ((msg == null) || (msg == "")) return; // don't use empty Strings
  if (who == null) who = player;
  int mad_loop = who.GetProperty("MadLoop");
  // etc.
  }


Or if you really want to use the Character's ID you should consider something like this:

Code: ags
function Speech(String msg, int who) {
  if ((msg == null) || (msg == "")) return; // don't use empty Strings
  if ((who < 0) || (who >= 300)) who = player.ID;
  int mad_loop = character[who].GetProperty("MadLoop");
  // etc.
  }


Seeing as you can have up to 300 characters in your game. This version would convert any invalid Character IDs into the player character's ID whereas yours converts 99 (which is a VALID character ID) into the player's ID.

If you do decide you need to change the value of your properties during the game you will need to use a struct like you were doing, but might I suggest a different approach?

Code: ags
// script header
struct ExtendedCharacter {
  int mad_loop;
  int happy_loop;
  Character* Char;
  import int Speech(String msg);
  }

import ExtendedCharacter excharacter[AGS_MAX_CHARACTERS];

// global script
ExtendedCharacter excharacter[AGS_MAX_CHARACTERS];
export excharacter;

function ExtendedCharacter::Speech(String msg) {
  // custom display function
  // use this.mad_loop, this.happy_loop etc.
  }

// put this inside game_start
int i = 0;
while (i < Game.CharacterCount) {
  excharacter[i].Char = character[i];
  i++;
  }


Then you can use the excharacter array the same way you use the character array (except to use built-in functions you have to use the Char member).

SMF spam blocked by CleanTalk