How could I switch my custom view to another object??

D

Den

Guest
So in my game you can use a minion to scout ahead in a level but the trouble is I cant figure out how to make the view follow the other object. The player still exists while you take control of the minion too.

Here's the code:
Code:
if(!instance_exists(obj_minion)) {
    x+= (obj_player.x-x)/20;
    y+= (obj_player.y-y)/20;
}
See I thought I could just set up another if statement and switch what the view follows but that doesn't seem
to work for some reason (perhaps I could be doing it wrong?) the view follows the camera object btw,
I also tried just setting up another view the same way and switching it that way, which sorta works but I can't
have the custom set up that way. Does anyone know how I could do this??
 
G

GamingBud

Guest
View isn't an object, so the x and y variables would only pertain to the object your writing this in and not the view itself. Change those out with view_xview and view_yview.

Edit: By the way, this would not center the view on the object. You'd need to do a little math by subtracting half of the view size when you set it. For example, if I have a 1600x900 screen size, I'd say
Code:
view_xview = (obj_player.x-800);
view_yview = (obj_player.y-450);
 
Last edited by a moderator:
  • Like
Reactions: Den

Perseus

Not Medusa
Forum Staff
Moderator
You could also keep a camera instance. Instead of making the view follow either one the two other instances, make it follow the camera instance.

In the camera object, keep a variable target or something, then make it change its position this way:

Code:
if (instance_exists(target)) {
   x += (target.x - x) / 20;
   y += (target.y - y) / 20;
}
Whenever you need the view to follow a different instance, simply change this variable. Note that you might want to use floor() or ceil() to avoid the view jittering that might arise.
 
  • Like
Reactions: Den
P

PandaPenguin

Guest
so re-setting the view with room_set_view() is not what you want?

you could store all the views attributes in an array (or other variables) and just switch out the "obj" with this function when the minion is active

EDIT: derp, forgot the "This function can be used to define a view within any room in your game except the current one." part ... nvm then

EDIT 2:
with GMS2 you would be more lucky because the way views/cameras changed you are now able to switch the followed object any time anywhere with camera_set_view_target() ^^
 
Last edited by a moderator:
D

Diveyoc

Guest
I'm not sure what all you have for custom view code, but you could probably do something like this. Not necessarily the code, just the general idea.

Player create event:
Code:
my_camera = true;
Player Step Event:
Code:
if my_camera = true
    {
    //====  VIEW - CENTER ON PLAYER  ====\\
    view_xview = x - view_wview/2;
    view_yview = y - view_hview/2;
   
    //====  VIEW - CLAMP TO ROOM  ====\\
    view_xview = clamp(view_xview,0,room_width-view_wview);
    view_yview = clamp(view_yview,0,room_height-view_hview);
    }
And then in then in the minion Step Event:
Code:
    //====  VIEW - CENTER ON PLAYER  ====\\
    view_xview = x - view_wview/2;
    view_yview = y - view_hview/2;
   
    //====  VIEW - CLAMP TO ROOM  ====\\
    view_xview = clamp(view_xview,0,room_width-view_wview);
    view_yview = clamp(view_yview,0,room_height-view_hview);
So once the minion is created, write some code to change my_camera to false, and the view should change over to the minion...I think.
I basically have the same set-up with a player1 and player2, but when one gets created, the other gets destroyed, so I don't need a variable like my_camera.
 
D

Den

Guest
View isn't an object
Iv made an object called cam_view and the view is following cam_view (the code is in that objects step event) sorry I should have mentioned that lol Still thanks for your help though
 
Top