using surface.DrawString twice [solved]

Started by HandsFree, Wed 03/08/2011 15:24:33

Previous topic - Next topic

HandsFree

How can I write something on the background and later write something else without losing the first text?
For practise I tried:

DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
surface.DrawingColor = 11;
surface.DrawString(0, 100, Game.SpeechFont, "First line.");
surface.Release();
// do things
Wait(60);
DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();
surface.DrawingColor = 11;
surface.DrawString(0, 120, Game.SpeechFont, "Second line.");
surface.Release();

This gives the error variable 'surface' already defined.
The manual says: To do any further drawing, you need to get the surface again.
How would I do that?
Or is there another way?

thanks

Wyz

When you do
Code: ags
DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();

you're actually defining a variable called 'surface', and after that assigning a value to it. You need to define the variable only once, so for the second time use this instead:
Code: ags

surface = Room.GetDrawingSurfaceForBackground();


I hope that helps you.
Life is like an adventure without the pixel hunts.

HandsFree

Yes it works.
I thought the first line would be overwritten that way. But apparently not.

thanks

Khris

To clarify what Wyz said:

Code: ags
DrawingSurface *surface = Room.GetDrawingSurfaceForBackground();


is equivalent to

Code: ags
DrawingSurface *surface;
surface = Room.GetDrawingSurfaceForBackground();


While calling the second line a second time is no problem, declaring an existing variable again isn't permitted.

Wyz

Yes and also maybe explain why you can't redefine a variable. From the point of view of someone who is used to languages where variables are just names (often functional programming languages) that could feel somewhat strange.

If you don't care for it skip this bit...

Defining a variable (the type followed by the name) is more then just creating a name to refer to a certain variable. It will tell the compiler that you want a spot in the memory to use for the new variable. Because of the type the compiler knows how large the spot has to be, and the address of this spot is linked to the name. The place where you define the variable is also important, since the after the code block (between { and }) it is in is executed this spot will be freed again. Now the trick is that the taking of these spots happens at the beginning of the code block so when you define two variables with the same name this will be a problem. The compiler could have been built to just to ignore this fact and simply use the same spot for both, but that could lead to nasty bugs. Imagine when you accidentally declare two variables for different uses with the same name and start using them alternating, yeah that will create a mess.
Life is like an adventure without the pixel hunts.

HandsFree

That's how I look at variables too, so that's no problem. Only, I didn't realize it was a variable declaration.
The only ones I know are like:
bool bSeenLetter = true;

I don't know what the thing with the asterisk is.

Wyz

The asterisk means that de variable will point to a datatype rather than declare the datatype. The reason for this is that the datatypes like DrawingSurface are maintained by the engine internally other then the script itself. The engine will return an id that can be used to access it and stores this in the variable. This is also the reason why you will get an error when you try to delete it twice: the second time the engine won't find something with the id stored in the variable.
Life is like an adventure without the pixel hunts.

SMF spam blocked by CleanTalk