Warning with 2.7 scripting

Started by SSH, Tue 20/09/2005 16:33:43

Previous topic - Next topic

SSH

Those that don't understand the concepts of scope, garbage collection and so on might have trouble understanding why this code doesn't do anything in AGS 2.7 and later. I post this so people can understand the problem, and is there anythign we cna do to help people not do this by mistake?

With old AGS overlays you had to create them and then get rid of them (unless you used DisplaySpeechBackground that got rid of them for you after a bit). However, if you do this:

if (myvar == 2) {
  Overlay* testOverlay = Overlay.CreateTextual(50,50,120,2,15,"This is a text overlay");
}

You will probably not see your overlay at all. Why? becuase the Overlay you create is a manged type and automatically garbage collected when the testOverlay variable goes out of scope: i.e. when you reach the closing brace "}" within which the variable was declared. So the overlay is created and destroyed almost instantly.

Instead, do:

Overlay* testOverlay;
if (myvar == 2) {
  testOverlay = Overlay.CreateTextual(50,50,120,2,15,"This is a text overlay");
}

except, of course, if this is inside a function. The safest thing to do is only declare Overlay *s outside ANY function. That way it will stay in scope all the time.


I just helped a friend (who will remain anonymous) sort out this problem on #ags, so its already happenign and I can just see lots of questions like this occuring.
12

Pumaman

This is a good point, but I'm not sure what the best approach would be to warn people about it. The documentation for CreateTextual probably needs to mention this, it looks like I forgot to do so.

SSH

While we're on documentation... it's flippin hard to find out how to do optional arguments in your own code. Eventauilly,  found:

http://www.adventuregamestudio.co.uk/yabb/index.php?topic=20582.msg260629#msg260629

but this should go in Manual, and In strazer's tidbits!
12

SMF spam blocked by CleanTalk