GM:S 1.4 How to find the center of a view

Discussion in 'Programming' started by Linkforce, Jul 5, 2016.

Tags:
  1. Linkforce

    Linkforce Member

    Joined:
    Jul 4, 2016
    Posts:
    47
    I'm surprised myself I don't how to do this exactly.
    Let's say I have a view following the player, and when I press the Z key, I want something to pop up in the exact middle of the screen.
    Now if the view is a 640x480, of course I can just do,
    instance_create( view_xview+320, view_yview+240,obj_object)

    But is there a way so that I would never have to go back that code if I were to change the view to another size?
     
  2. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    view_xview + view_wview * 0.5
    view_yview + view_hview * 0.5
     
  3. Supercoder

    Supercoder Member

    Joined:
    Jul 4, 2016
    Posts:
    38
    Try this:
    Code:
    view_xview[0]+view_width[0]/2
    And the same for the vertical height

    edit:
    ninja'd :p
     
    404bug and Paolo Mazzon like this.
  4. OverSeer909

    OverSeer909 Guest

    Taking the example you provided...

    Just divide the number of the view by 2 (for both X & Y).
    Example: instance_create(view_xview+640/2,view_yview+480/2,obj_object)
     
  5. Gamma

    Gamma Guest

    hold up- if you're trying to create a bullet or similar object (because you mentioned the view was following the player), then instead of using the view, use the player.

    instance_create(obj_player.x, obj_player.y, obj_bullet)
     
    ParodyKnaveBob likes this.
  6. Linkforce

    Linkforce Member

    Joined:
    Jul 4, 2016
    Posts:
    47
    That would be a much more efficient way of doing that, yeah. Never actually thought of that haha!

    By the way, not sure why I didn't respond to the other replies before, but thanks a lot to everyone who responded, even if it has been a year since I've posted this. Still helps to this day.
     
  7. KaelaOpalheart

    KaelaOpalheart Member

    Joined:
    Oct 8, 2017
    Posts:
    22
    Hmmm, I'm trying to do something similar to this except instead of the center of whatever the current visible screen is, I'm trying to get a fireball to spawn every few seconds at a random point along the y axis of the visible screen, moving left to the other end, like in bowser's castle in the original mario game. I can't seem to figure out how to not only refer to the rightmost edge of the visible screen, but then how to also say, a random point along that edge. Can anyone help please?

    (I have an invisible fireball spawner with a step event that will run a spawn script in an alarm set to go off every couple seconds, and in the fireball itself I set the speed in the create event, along with a couple other minor things like image speed etc. I'm relatively sure I set this up correctly, I just can't figure out the specific "vocabulary and grammar" to put in the alarm to spawn the fireball on the right edge of the screen.)

    (I'd show code but I don't even really have anything relevant to this to show, hence why I'm describing the basic set up at the least.)
     
    Last edited: Oct 30, 2017
  8. Linkforce

    Linkforce Member

    Joined:
    Jul 4, 2016
    Posts:
    47
    Not 100% sure I understand your question(kinda tired), but I'll try to answer it.

    You can use view_xview,view_yview,view_wview,view_hview.
    • view_xview: locate the most left position of the view.
    • view_yview: locate the most top position of the view.
    • view_wview: gives you the length of how wide your view is.
    • view_hview: gives you the height of how tall your view is.
    So in order to spawn the fireball, you said you need a random point along the y axis. Well, using the selection of choices above, you can probably guess you'd need view_hview(as this gives you how tall your view is). You'll also want to add in view_yview, as that gives you the top of your view(you don't want the fireball to spawn above the view).
    You'll also want to make use of random_range(if you don't know what that is, that's just telling gamemaker to choose a number between 2 numbers you give it).
    Overall, the spawning code for a fireball would look something like,
    Code:
    instance_create(view_xview+view_wview,random_range(view_yview,view_hview),obj_fireball);
    
    view_xview+view_wview will make it spawn on the right side of the screen (left most position on view, plus the length of the view).
    and the random_range, as explained above, will simply choose a random number from the top of the view, to the bottom of the view.

    Hope I helped ya.
    Side note: You can make view_xview, and all the others into variables because they get a little annoying to type after a while. Personally I like to use vxv,vyv,vwv,vhv.
     
  9. Terminator_Pony

    Terminator_Pony Member

    Joined:
    Dec 23, 2016
    Posts:
    85
    One slight adjustment... when getting the random point on the y axis, view_hview needs to be relative to the current view.

    So instead of
    Code:
    random_range(view_yview,view_hview)
    it would be
    Code:
    random_range(view_yview,view_yview+view_hview)
     
  10. KaelaOpalheart

    KaelaOpalheart Member

    Joined:
    Oct 8, 2017
    Posts:
    22
    Ah! You two are great, thanks for the response! Unfortunately it's still not spawning fireballs. This is what I put into the spawner's Alarm0:
    instance_create((view_xview+view_wview),random_range(view_yview,view_yview+view_hview),obj_fireball)

    and the step event for it is
    alarm[0] = room_speed * 3

    The fireball itself has only code to set the speed and animation stuff upon creation and the destroy when out of room stuff, and the player object has the damage code in it for any collisions.

    When it didn't work at first I even tested putting a fireball into the room for at the start, and yeah it moves to the left like it's supposed to. But no new ones show up whether I stand there or move through the room. Any ideas?
     
  11. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    821
    @KaelaOpalheart
    in the create event do you set the alarm? you need to initally set it or it cant start its loop of endless alarm triggers.
     
  12. KaelaOpalheart

    KaelaOpalheart Member

    Joined:
    Oct 8, 2017
    Posts:
    22
    Hmmm. I was using the alarm event itself to set the create script into that alarm, and the step event for triggering it every 3 seconds. If there's an additional step using the create event I was unaware of that, as my only other use of alarms so far has been for like, setting invincibility and flashing length after the player ran into spikes, which didn't need a create event. I didn't realize anything else might be needed. so it's.... in the create event, just, tell the script to run once? If I'm understanding correctly? I'll go try that, thanks

    Ooooook, well, it made the fireball once upon entering the room. Then never again. =/

    First in create I simply tried alarm[0] = 1 and got one fireball, then I switched it to the room_speed*3 and nothing at all, then switched to alarm[0] = true and also got one fireball and then nothing, then switched the step event from room_speed*3 to just saying alarm[0] = 90, that didn't help anything.

    (The good news is the few I got came from the correct side! :D Thanks for that)
     
    Last edited: Oct 30, 2017
  13. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    821
    @KaelaOpalheart
    i'm not sure what you mean when you said "tell the script to run once" I meant in the create event just put:
    Code:
    alarm[0] = room_speed*3;
    that way the alarm can be ran once, then once it runs it'll create a fireball, then run the alarm again, causing the loop to begin
     
  14. KaelaOpalheart

    KaelaOpalheart Member

    Joined:
    Oct 8, 2017
    Posts:
    22
    I see.... well, when I tried that, it never made a fireball at all. I tried it again but this time deleted the step event with the same code, in case there was somehow a conflict or something, and it did produce a fireball once after 3 seconds, but again no further ones. I wouldn't think removing the step trigger was the right answer anyway, but after it wasn't working in the first place I'm just guessing, lol!

    Yeah I put the step one back in and again there's no fireballs at all, not even a first one.

    Heh, I got help with the edge of the view on screen issue, which is fantastic and thank you everyone. Just wish the alarm thing didn't suddenly start acting so weird. This wasn't supposed to be what I needed help with XD It seems like the alarm just isn't running in the step event for whatever reason, even after being started in create. But I can't figure out what might be wrong with the code I used. Is it somehow *too* simple to just say, alarm[0] = room_speed*3? Do I need to put something else alongside it?

    *edit* Phew! Found the answer. The repeat trigger needed to go into the alarm itself, to my great surprise, not a step event. It's finally working the way it's supposed to ^_^ Thanks for the help getting them created along the edge!
     
    Last edited: Oct 31, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice