How to solve....Error: Cannot Declar Pointer to non-managed type?

Started by Gold Dragon, Fri 09/05/2008 00:13:49

Previous topic - Next topic

Gold Dragon

Ok I'm trying to emulate the Function Display using SSH's Sprite Font script module.

In GlobalScript.asc I have....

Code: ags

SpriteFont Courier;

function DisplayFont( String txt, int x=0,  int y=0,  SpriteFont *Font=null ){
  
  int txtwidth;
  DrawingSurface *Screen=Room.GetDrawingSurfaceForBackground();
  
  if(*Font != null){
    *Font=Courier;
  }
  
  if((x == 0) && (y == 0)){
    y = 121;
    txtwidth = Font.GetSpriteTextRawWidth(txt);
    x= FloatToInt((400-(txtwith * 0.5)), eRoundUp);
  }
  Font.TextOnBackground(x, y, txt);
  WaitMouseKey();
  Screen.Release();
}

.
.
.
.
.

export Courier;
export DisplayFont( String txt, int x=0,  int y=0,  SpriteFont *Font=null );


In my room Script I have

Code: ags


import SpriteFont Courier;
import function DisplayFont( String txt, int x=0,  int y=0,  SpriteFont *Font=null );
.
.
.
function hStool_Look(){
  DisplayFont("UMMM.... ITS A STOOL GENIUS. YOU SIT ON IT.");
}



But I get the Error "Cannot Declar Pointer to non-managed type?"
at "import function DisplayFont( String txt, int x=0,  int y=0,  SpriteFont *Font=null );"

Can anyone help me?
Want to see how I am doing and how far I've gotten in my game? Visit my forum  http://www.freepowerboards.com/DragonKnight

monkey0506

The problem here is that it isn't possible to declare a pointer to a non-managed type. Just like the error message says!

SSH's SpriteFont type is a custom type. Currently in AGS it is impossible to create pointers to custom types. This also means you cannot use them as a function parameter.

However, you could create the function as an extender method of the font:

Code: ags
SpriteFont Courier;

Overlay* RenderDisplay(this SpriteFont*, String text, int x, int y) {
  if ((text == null) || (text == "")) return null; // invalid text parameter, abort
  int width = this.GetSpriteTextRawWidth(text) + this.LineSpacing + 4; // create the box big enough to fit the text, with 1/2 line-spacing padding, and a 2 pixel black border
  int height = this.GetSpriteTextRawHeight(text) + this.LineSpacing + 4;
  DynamicSprite *sprite = DynamicSprite.Create(width, height); // the sprite we will render our textbox onto
  DynamicSprite *textSprite = this.TextOnSprite(text); // here we grab the sprite-text into a sprite so we can draw it into our textbox
  DrawingSurface *surface = sprite.GetDrawingSurface(); // finally open up the sprite to begin drawing
  surface.DrawingColor = 31; // white for the background
  surface.DrawRectangle(0, 0, sprite.Width, sprite.Height); // fill the entire sprite with white, everything else will go on top
  surface.DrawingColor = 0; // black for the border
  surface.DrawLine(0, 0, 0, sprite.Height, 2); // draw the border at 2px wide
  surface.DrawLine(0, 0, sprite.Width, 0, 2);
  surface.DrawLine(sprite.Width, 0, sprite.Width, sprite.Height, 2);
  surface.DrawLine(0, sprite.Height, sprite.Width, sprite.Height, 2);
  surface.DrawImage(2 + (this.LineSpacing / 2), 2 + (this.LineSpacing / 2), textSprite.Graphic); // draw our text
  surface.Release(); // release the sprite so we can create the overlay
  textSprite.Delete(); // delete this sprite, we don't need it any more
  if (x == -1) x = (System.ViewportWidth - sprite.Width) / 2; // default x to center-screen
  if (y == -1) y = (System.ViewportHeight - sprite.Height) / 2; // default y to center-screen
  if (x < 0) x = 0; // if the x value is negative, set it back to 0
  if (y > 0) y = 0; // same for y value
  Overlay *displayOverlay = Overlay.CreateGraphical(x, y, sprite.Graphic, false); // create our overlay
  sprite.Delete(); // delete our temporary sprite
  return displayOverlay; // return the overlay so we can use it (move it around, remove it, etc.) (also if we don't it will be deleted! ;))
}

Overlay *myDisplay = Courier.RenderDisplay("UMMM.... ITS A STOOL GENIUS. YOU SIT ON IT.");

import RenderDisplay(this SpriteFont*, String text, int x=-1, int y=-1);


Granted it is a bit counter-intuitive having to call the function on the font instead of passing it as a parameter, but it is perhaps the easiest method of creating a generic function for this case. Even if you were using enumerated values or some such you would still have to duplicate code. Another idea may be to set up all your fonts in an array and then use enumerated values for the indices.

In any case, it should give you a starting point.

Edit: I forgot that the function returns an Overlay. I did it this way so you could put your display on top of everything instead of just merging it into the background. But this means you have to keep an Overlay* pointing to it or the Overlay will just get deleted. ;)

Gold Dragon

Want to see how I am doing and how far I've gotten in my game? Visit my forum  http://www.freepowerboards.com/DragonKnight

SMF spam blocked by CleanTalk