1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

problem of identical x and y variables

Discussion in 'Programming' started by Remeuf, Aug 13, 2019.

  1. Remeuf

    Remeuf Member

    Joined:
    Aug 11, 2019
    Posts:
    16
    Hello everyone,
    I have another problem with my tank. It consists of two objects on top of each other and independent in rotation but not moving. They must be able to orient themselves independently but to advance exactly the same. Here in my code the turret and the vehicle turn correctly but when the whole advance, the turret goes away elsewhere which is a bit annoying for a tank ...

    Question # 2: How do you display the result of a variable on the game screen?

    Code:
    // Rotation char
    var droite;
    droite = keyboard_check(vk_right);
    if (droite)
    {
        image_angle += -1;
    }
    
    var gauche;
    gauche = keyboard_check(vk_left);
    if (gauche)
    {
        image_angle += 1;
    }
    
    // Rotation tourelle
    var dir = point_direction(x,y,mouse_x,mouse_y);
    image_angle = dir-90;
    
    // Tank avance
    var avance;
    avance = keyboard_check(vk_up);
    if (avance)
        {
            x += lengthdir_x(4,image_angle+90);
            y += lengthdir_y(4,image_angle+90);
        }
    The code is identical for the 2 sprites
     
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    604
    To have two instances move together is straightforward. Make the body of the tank as one object, and the turret of the tank as a second object. In this example I call the turret "obj_turret"
    create event of tank:
    Code:
    turret = instance_create(x,y, obj_turret); // this is GMS 1 code. The code for GMS 2 might be a little different, but is essentially the same: it allows the tank to directly manipulate the turret object
    turret_xdiff = whatever difference there is horizontally between the origin of the tank and the point  where the turret should be
    turret_ydiff = whatever difference there is vertically between the origin of the tank and the point where the turret should be
    
    end step of tank:
    Code:
    turret.x = x + turret_xdiff;
    turret.y = y + turret_ydiff;
    You should have perfect alignment done like this. By doing it in the end step you ensure that the tank has already moved to it's current x / y position, and is only placing the turret once that has happened. This must be in the end step.

    general access of turret through any step event:
    Code:
    with (turret)
    {image_angle  = whatever}
    
    OR
    
    turret.image_angle = whatever // access through "turret" and include the "." 
    To draw a variable:
    Code:
    draw_text( x position, y position, variable name)
    example:
    Code:
    draw_text(20, 20, image_angle)
     
    Last edited: Aug 13, 2019
    Remeuf likes this.
  3. Remeuf

    Remeuf Member

    Joined:
    Aug 11, 2019
    Posts:
    16
    Thank you for your efforts The_dude_abides,
    It's nice. Well, nothing works even the draw_text.
    It does not matter, I'm going to dig and continue the tutorials;)
     
  4. Tiilerdye

    Tiilerdye Member

    Joined:
    Jan 2, 2017
    Posts:
    21
    I have a damn good tank code for the movement of the base and turret I can pass over to you once I’m off of work. Made it a few years back.
     
  5. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    604
    1) Drawing:
    To use draw_text it has to be in a draw event. You won't see it otherwise. If you're already doing this code in a draw event, and you still can't see it then you may be using coordinates for the position that are out of view.
    Code:
    draw_text(20, 20, image_angle)
    would be a position in the room (absolute coordinates)
    Code:
    draw_text(view_xview[0] + 20, view_yview[0] + 20, image_angle)
    would work if you were using a view, as it places it in relation to the view

    2) Moving instances together:
    If you are using physics for movement then that might be why this didn't work, as that requires specific physics commands. Or if you are using GMS 2 then I think it creates instances a bit differently. Generally it is one of the most straightforward things to do, so those are the main reasons I can think of why it won't work.

    I have a project with this exact code, and it works fine:

    create event 'obj_tank'
    Code:
    turret = instance_create(x,y, obj_turret);
    turret_xdiff = x - turret.x;
    turret_ydiff = y - turret.y;
    is_spd = 2;
    
    step event 'obj_tank':
    Code:
    var up = keyboard_check(ord("W"));
    var down = keyboard_check(ord("S"));
    var left = keyboard_check(ord("A"));
    var right = keyboard_check(ord("D"));
    if right
    {image_angle -= is_spd;}
    if left
    {image_angle += is_spd;}
    is_vert = up - down;
    direction = image_angle;
    speed = sign(is_vert) * is_spd;
    end step event 'obj_tank':
    Code:
    turret.x = x + turret_xdiff;
    turret.y = y + turret_ydiff;
    turret.image_angle = point_direction(x,y,mouse_x,mouse_y);
     
    Remeuf likes this.
  6. Remeuf

    Remeuf Member

    Joined:
    Aug 11, 2019
    Posts:
    16
    Thank you Tiilerdye why not? I will try to understand your code
     
  7. Remeuf

    Remeuf Member

    Joined:
    Aug 11, 2019
    Posts:
    16
    Yes I created a 'Draw' event under the 'Step' event: the values of the variable are displayed but my tank object disappears. My goal here is to visualize my variables to see how they evolve and understand how it all works.

    For the movement of the tank: yes I had created two objects (the body of the tank and the turret). I'll take your code later, I have to go to work. I am in the GM Studio 2 trial period (1 month trial before I decide to buy the license or not).

    Thank you The_dude_abides for your patience and it takes a lot to help beginners!
     
  8. rytan451

    rytan451 Member

    Joined:
    Jun 21, 2016
    Posts:
    50
    Remember that you need to use draw_self in the draw event, otherwise it will not draw itself.
     
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,661
    If you are just drawing variables for debugging purposes, use the Draw GUI Event, or the Draw Begin event, or the Draw End event. If you put any code in the normal Draw Event, then you will override the built-in drawing GM does automatically, but if you use the other Draw events, that's not an issue. Draw Begin and Draw End use room coordinates (as the_dude_abides explained), whereas Draw GUI uses window coordinates, so (0,0) will usually suffice in the GUI.
     
  10. Remeuf

    Remeuf Member

    Joined:
    Aug 11, 2019
    Posts:
    16
    @ TheouAegis & Rytan451 : The draw works. Actually I made the mistake of the normal 'draw event' instead of the draw in the GUI. Cool

    @ The_dube_Abides : and for moving tank body + turret, it works too! The turret follows the vehicle as its shadow. Youppppiiiiiii :). Just a small mistake that I will settle, I understood the code.

    Code:
    tourelle = instance_position(x,y, ob_tourelle);
    tourelle_xdiff = x - tourelle.x;
    tourelle_ydiff = y - tourelle.y;
    is_speed = 2;
    For information : intance_create --> instance.position in GM2

    Thank you all. I will be able to continue the tutorial. Here it was only an different exercise that I impose for each step of the tutorial.
     
  11. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    604
    Something I didn't communicate too well, is that the tank object creates the turret object. You don't need to place an instance of the turret in the room. Which you have done, as instance_position is a collision check.

    instance_create is something like instance_create_layer in GMS 2.
     
  12. Remeuf

    Remeuf Member

    Joined:
    Aug 11, 2019
    Posts:
    16
    OK it's noted ;)
     

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