GMS 2 Score that follows camera

W

Wyrmis

Guest
Hello

I am new to GMS2, and am programming a sonic game for a programming project.
The last thing I need to do is to show the score, but I need it follow the view. I don't know what code I need or where to put it. I have created an invisible, non-solid object with the following code:

Code:
x= view_xport[view_current] + 320;
y= view_yport[view_current];
draw_set_font(RingFont);
draw_set_color(c_white);
draw_text(x, y, (global.rings));
 

Selva

Member
I am going to recommend that you investigate the "Draw GUI" event. It draws directly to the visible screen, to to the room.

Note, however, my knowledge of this is very limited. So consulting the user manual to the rather difficult to find Draw GUI section would be very very recommended.
 
G

GameMakerPero

Guest
I am also pretty new to gamemaker but I believe I originally learned this from one of the Shaun Spalding videos. The below is the code (within the draw event of my score object) I am using for an experiment I am doing (with a room size of 4096x3072, viewpoints enabled, camera and viewpoint properties both 2048x1536). You'll want to change the "40" and "60" values below based on your needs. If you are doing multiple rooms, make sure you understand the "persistent" option.

Correct me if I am wrong but I believe by merely using "x" and "y," you are pulling the location of where you have placed the object within the room (or where it currently has moved if for some reason you are controlling movement of your score :) ), whereas with the below, it overrides such placement (and therefore you can place it anywhere within the room).

You might also want to place your score object within its own layer above your instances so that instances will appear behind your text.

Hope this works/helps.

var vx = camera_get_view_x(view_camera[0]);
var vy = camera_get_view_y(view_camera[0]);

{
draw_set_color(c_red);
draw_set_font(f_dos);
draw_text(vx + 40, vy + 60, global.points)
}
 
W

Wyrmis

Guest
I got this error when I tried using Draw GUI Begin:


___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object Score:

DoAdd :: Execution Error
at gml_Object_Score_Draw_74 (line 6) - draw_text(x, y, ("Rings: " + (global.rings)));
############################################################################################
 

chorrus

Member
View_xview and view_yview is what you need.

For example if you want it to be in the top center of the screen use:

Draw_text(view_xview + room_width/2, view_yview + 20, global.rings)
 
W

Wyrmis

Guest
I am also pretty new to gamemaker but I believe I originally learned this from one of the Shaun Spalding videos. The below is the code (within the draw event of my score object) I am using for an experiment I am doing (with a room size of 4096x3072, viewpoints enabled, camera and viewpoint properties both 2048x1536). You'll want to change the "40" and "60" values below based on your needs. If you are doing multiple rooms, make sure you understand the "persistent" option.

Correct me if I am wrong but I believe by merely using "x" and "y," you are pulling the location of where you have placed the object within the room (or where it currently has moved if for some reason you are controlling movement of your score :) ), whereas with the below, it overrides such placement (and therefore you can place it anywhere within the room).

You might also want to place your score object within its own layer above your instances so that instances will appear behind your text.

Hope this works/helps.

var vx = camera_get_view_x(view_camera[0]);
var vy = camera_get_view_y(view_camera[0]);

{
draw_set_color(c_red);
draw_set_font(f_dos);
draw_text(vx + 40, vy + 60, global.points)
}

I tried doing what you said, and the strangest, yet funniest thing happened, the score was moving with the player, but at a bigger speed. (EX: When i jumped, it jumped, when I went right , it went right), but that was when I tried to Draw GUI, it worked when I put it in the regular draw event.

Thanks so much
 

Perseus

Not Medusa
Forum Staff
Moderator
View_xview and view_yview is what you need.
No, they aren't, for those variables (arrays, to be precise) are now obsolete with the introduction of cameras in GMS 2.

I got this error when I tried using Draw GUI Begin:
A DoAdd error message shows up when the + operator is given operands of two different types. You can't add a real number with a string. You must convert the real operand into a string via the string function.

Code:
draw_text(x, y, "Rings: " + string(global.rings));
but that was when I tried to Draw GUI, it worked when I put it in the regular draw event.
That's because Draw GUI draws relative to the screen/application surface, while the regular Draw event draws relative to the room. Since the camera_get_view_x/y functions that you're using return coordinates relative to the top-left corner (0, 0) of the room, you have to use the regular Draw event.
 
Top