Camera Object Won't Read Variables (SOLVED)

G

G0vnah

Guest
(TL;DR version - I'm having trouble with the codes shown in the image. The game acts like the "view_w_half" and "view_h_half" variables don't exist on the step event page, and only uses the data they stand for if I put the data itself into that place).

------------------------------------------------------------------------------------------------------

Alright, I'm new to Game Maker Studio, but I absolutely love it. I've been following along with Shaun Spaulding's series, entitled "GameMaker Studio 2: Complete Platformer Tutorial."

Part 6 involves setting up an object to serve as a focus point for the camera.

I have the following script in the "create" event, where I place the variables upon the object's creation ('I've bolded the codes giving me trouble)

code_image.png
(just below are the event codes as text...)

Code:
cam = view_camera[0];
follow = oPlayer;
xTo = xstart;
yTo = ystart;
view_w_half = (camera_get_view_width(0)/2)
view_h_half = (camera_get_view_height(0)/2)

Code:
if (instance_exists(follow))
[INDENT]{
[INDENT]xTo = follow.x;
yTo = follow.y;[/INDENT]
};

x += (xTo - x)/25;
y += (yTo - y)/25;

camera_set_view_pos(cam,x - view_w_half, y - view_h_half);
[/INDENT]
-------------------------------------------------------------
Here's the problem:

Whenever I use the code above, the game seems to ignore the "view_w_half" and "view_h_half" variables entirely in the step event. It moves the camera so that the player is in the top left, always.

HOWEVER,
if I manually type in that last line of code as shown below, it focuses in on the player just fine, putting them at the center:

Code:
camera_set_view_pos(cam,x  -  (camera_get_view_width(0)/2), y - (camera_get_view_height(0)/2);
Why is it seemingly ignoring those two variables from the "create" event, despite CLEARLY not ignoring any other variables?
 

Slyddar

Member
The camera is getting updated every step, but you are only capturing the values once in the create event. When you place them in the step event they are then getting the new values each step and applying them.

Try adding this line to your step to see the difference in values in the output window.
Code:
show_debug_message("view_w_half : " + string(view_w_half) + ", camera_get_view : " + string(camera_get_view_width(0)/2));
 
G

G0vnah

Guest
The camera is getting updated every step, but you are only capturing the values once in the create event. When you place them in the step event they are then getting the new values each step and applying them.

Try adding this line to your step to see the difference in values in the output window.
Code:
show_debug_message("view_w_half : " + string(view_w_half) + ", camera_get_view : " + string(camera_get_view_width(0)/2));
Alright, I added that (after changing "show_debug_message" to "show_message_debug,"), and got this in the output tab at the bottom of the screen:

view_w_half : 0, camera_get_view : 512

What does that mean to you?
 

Slyddar

Member
Alright, I added that (after changing "show_debug_message" to "show_message_debug,"), and got this in the output tab at the bottom of the screen:

view_w_half : 0, camera_get_view : 512

What does that mean to you?
I would be extremely surprised to see show_message_debug() working inside GML, as it's not a known function. show_debug_message() is though. Are you sure it works?

Well what the code does is outputs the 2 values so you can compare them. It's a really handy tool for looking at output in your code, and something you probably will want to get used to using.

The create event camera is not using view_camera[0], instead it's using 0. Maybe try changing your create event to the camera you declared, cam.
Code:
view_w_half = camera_get_view_width(cam) / 2
 
G

G0vnah

Guest
Oh, bother. I messed up when I was typing the reply in the forum. Yeah, "Show_debug_message" is what I put in. Don't know why I got that mixed up. *shrug* eh, okay.

But yeah, it showed those values below. I just followed your advice, changing "0" in the variable to "cam". Now it's working great! Thanks for the help
 
Top