Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Valentine on Thu 11/10/2007 18:25:57

Title: Problem with DynamicSprite.CreateFromFile [SOLVED]
Post by: Valentine on Thu 11/10/2007 18:25:57
Hi, I'm trying to use dynamic sprites, but nothing shows up. The code I'm using is;

if (keycode==32) { DynamicSprite* sprite = DynamicSprite.CreateFromFile("player.bmp");
                     RawDrawImage(mouse.x, mouse.x, sprite.Graphic);}

I'm unsure as to which directory this points to, so I've put of copy of player.bmp in both the Compiled folder as well as the game folder, but still nothing shows up. The file I'm pointing to is named player.bmp too.

Whats going wrong here :-\? Thanks.
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Ashen on Thu 11/10/2007 19:00:27
It needs to be in the 'Compiled' folder, not the game folder. That doesn't seem to be the proble, though - if it couldn't find the image file, you'd get a crash with an error message, not just nothing showing up.

Are you definitely using valid (i.e. on-screen) coordinates? For example, the code you posted says mouse.x, mouse.x, where I'd expect the second one to be .y. If you pressed the button while the mouse on the far left of the screen, (x, x) would draw the image out of bounds (e.g. 300,300). Also, if you're using a scrolling room and aren't in the top-right corner of it, the mouse coords might be off screen (RawDrawImage uses ROOM, not SCREEN, coords - use GetViewportX/Y to compensate).

Other than that, that code works fine for me...
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Valentine on Thu 11/10/2007 19:12:30
Ah, its because I was using a scrolling room (the mouse.x, mouse.x was just me mistyping my code onto the boards ::). Thanks for setting me right!

Unfortunately, I was wanting to use this to draw something onto the GUI, and in such a way that it moves when the room scrolls, is there any way to do this?

Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Ashen on Thu 11/10/2007 19:23:31
Well, you an draw relative to the screen position using GetViewportX/Y, like I said:

RawDrawImage(mouse.x + GetViewportX(), mouse.y + GetViewportY(), sprite.Graphic);


However, RawDrawImage draws below GUIs so you probably won't be able to use it in the way you want. How about declaring the DynamicSprite *sprite outside the on_key_press, grabbing the image as you are doing, and assigning it to a Button or the GUI Background graphic? (You'd need to declare the DynamicSprite seperately, as they're destroyed when the function/condition they're declared in ends - in this case, at the '}' after RawDrawImage.) It depends what exactly you're trying to do, however.
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Valentine on Fri 12/10/2007 10:06:33
Okay, I've taken it out of On_Key_Press, and I'm now using this script, but I can't get it to work;

DynamicSprite* PlayerPortrait = DynamicSprite.CreateFromFile("player.bmp");
btnMainPortrait.NormalGraphic = PlayerPortrait.Graphic;

which I've placed in the Game_Start section, although I've moved it to other sections (like Repeatedly_Execute) to see if it would work there, but it doesn't.

Thanks again.
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Ashen on Fri 12/10/2007 10:25:18
Ah, no. It looks like I wasn't clear. Declare the DynamicSprite OUTSIDE of all functions (top of the Global script). Set the sprite wherever you want. The way you've got it, the DynamicSprite is local to game_start - it'll be destroyed as soon as the function's over. Try:

DynamicSprite *PlayerPortrait;


function game_start() {
  PlayerPortrait = DynamicSprite.CreateFromFile("player.bmp");
  btnMainPortrait.NormalGraphic = PlayerPortrait.Graphic;
}


(Works for me.) Looks like you're letting the player have a custom 'avatar' the in game? Nice idea.

You'll also need to delete the DynamicSprite when you quit the game (will probably mean making your own Quit GUI, if you haven't already). I don't think it'll cause any major problems if you don't, but it does generate an error message (error log, when you're not going through the editor).
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Valentine on Fri 12/10/2007 10:37:04
It's working now :) Thanks very much! I really appreciate the effort you're going to to help.

And yeah, I'm allowing the player to put in their own avatar, I should be able to bring it up for conversations with a bit of coding.

I get the error when I quit the game, but I'll implement a custom quit game GUI as you say.

Thanks again!
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Ashen on Fri 12/10/2007 10:54:58
Quote
I should be able to bring it up for conversations with a bit of coding.

If you're using 'Seirra-style' or 'QFG4-style' speech, that should be pretty easy. Just create a ViewFrame pointer to Loop 0, Frame 0 of the player's speech view, and set its graphic to PlayerPortrait.Graphic, e.g.:

DynamicSprite *PlayerPortrait;
ViewFrame *Talk;


function game_start() {
  PlayerPortrait = DynamicSprite.CreateFromFile("player.bmp");
  btnMainPortrait.NormalGraphic = PlayerPortrait.Graphic;
  Talk = Game.GetViewFrame(2, 0, 0);
  Talk.Graphic = PlayerPortrait.Graphic;
}


(I don't know if the ViewFrame needs to be declared outside game_start - this is kind of off the top of my head - but I figured better safe than sorry...)

If you're using a custom dialog system, it might be harder.
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Valentine on Fri 12/10/2007 11:03:03
Hey cheers man, I'm gonna have a go at coding it myself first - I think I should be able to do it now I know how to import the graphic in the first place - best way to learn really.

Why do I think I'm setting myself up for a post later today saying 'Okay... guess its harder than I thought  ::)'  :P.

Thanks again!
Title: Re: Problem with DynamicSprite.CreateFromFile
Post by: Dualnames on Sat 13/10/2007 23:05:10
So you 've soldved your problem? Please change the title if you did so we can know.