Trouble with Dynamic Sprite & Pointers

Started by Ali, Wed 21/12/2005 14:20:47

Previous topic - Next topic

Ali

I don't really know what I'm talking about here, a wierd thing occurred that seemed like a problem in AGS but was probably down to my lack of knowledge about the way the editor works:

When the Load button on one of my GUIs is clicked it runs this script:

Code: ags
LoadList.FillSaveGameList();
if (LoadList.SelectedIndex != -1) listselected = LoadList.SelectedIndex;
if (buttonSprite != null) buttonSprite.Delete();
buttonSprite = DynamicSprite.CreateFromSaveGame(savegameindex[listselected], 73, 47);
if (buttonSprite != null) LoadScreen.NormalGraphic = buttonSprite.Graphic;
gLoad.Visible=true;


'LoadList' is the list on my load GUI, 'LoadScreen' is the button that shows the screenshot, 'gLoad' is the Load GUI itself. buttonSprite is the pointer that I load the screenshot into.

This loads a screenshot from the currently selected saved game and works absolutely fine. When I click the cancel or the load button on the GUI it calls:

Code: ags
if (buttonSprite!=null) buttonSprite.Delete();


A wierd thing happens though when I copy the script from the GUI button to an interaction on one of the objects in Room 0. I was using an object as a 'Load' button rather than make a new GUI. The Load GUI works fine and the screenshot appears, but the dynamic Sprite is not deleted when the cancel button is clicked.Ã,  When I exit the game I get a message from AGS telling me that I didn't delete the sprite slot I loaded.

Indeed if I remove the
Code: ags
if (buttonSprite!=null)
part of the script the game crashes telling me that buttonSprite is null.

I worked around this by making sure only to bring up the Load GUI from another GUI, but I thought it was worth asking if anyone (or perhaps everyone) understands this better than me.

monkey0506

Do you delete the sprite in the code where you turn the GUI off?  Because if you turn the GUI off, then never turn it back on again it seems to me the sprite would never get deleted.  And just as a matter of interest, where is DynamicSprite* buttonSprite defined at?

Ashen

Where & how is buttonSprite initially declared? (monkey posted first.)
Is it possible you're deleting a different instance of it in the room script (meaning the version created by the GUI isn't deleted)?

What if you make if (buttonSprite != null) buttonSprite.Delete(); a function, and call that in the interacton, instead of the code itself?
I know what you're thinking ... Don't think that.

Ali

I came to a similar conclusion Ashen, but couldn't understand how that could be happening.

buttonSprite was declared in the Global script header.

The function method sounds like a possibility, but the workaround I've achieved makes my scripting neater anyway so I don't want to keep fiddling. Thanks for your thoughts guys!

strazer

Don't declare variables/pointer in the main script header.
Doing this creates separate instances of said variable for the main script and each room. Changing the value in a room then doesn't affect any of the other variable instances. They're independent of each other, just share the same name.

Instead, you have to define the variable in the main script, export it, then import it into the main script header.

SMF spam blocked by CleanTalk