#Newbie here!

122 messages · Page 1 of 1 (latest)

limber copper
#
var _direction,_xcomponent,_ycomponent;
_direction  = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);

//draw a sprite here
draw_sprite_ext(spr_gun,0,x+_xcomponent,y+_ycomponent,1,1,_direction,c_white,1);
//or place an object
with (obj_gun) {
  x = other.x + _xcomponent;
  y = other.y + _ycomponent;
  image_angle = _direction;
}
#

puts the gun on an orbit 32 pixels away from the player and aims it towards mouse

sand verge
#

oh hey

#

where do i put it?

#

step event of player? @limber copper

limber copper
#

well, it would need to go in the draw event if you go with the draw sprite variant

#

if you dont use that, and use the with(obj_gun) instead, it can go in the step

#
var _direction,_xcomponent,_ycomponent;
_direction  = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);

//draw a sprite here
draw_sprite_ext(spr_gun,0,x+_xcomponent,y+_ycomponent,1,1,_direction,c_white,1);


///EITHER THIS OR THAT


var _direction,_xcomponent,_ycomponent;
_direction  = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);

with (obj_gun) {
  x = other.x + _xcomponent;
  y = other.y + _ycomponent;
  image_angle = _direction;
}
sand verge
#

whats the difference between draw and craete?

#

and i had another question, how do the 'draw_rectangle () ' works, im trying to make a dialogue box but i dont understand what ineed to put inside of the ()

limber copper
#

when your game runs at 30 steps a second or whatever, all logic in the step and draw events and so on will execute 30 times a second

limber copper
#

that means we also need to redraw everything every time. if you use draw functions outside of the draw event, they wont show up (there are exceptions that arent relevant rn) .
if you could draw in the create event, it would only show up for 1 frame

limber copper
sand verge
#

ok

limber copper
#

but if you think about it, the gun probably doesnt need to do a whole lot, you can put your shoot logic into the player, so the gun really only ever has to draw itself right

#

thats why we could also use the draw_sprite instead, and directly draw the gun in the player object.
one thing you need to know is, when you put code in the draw event, itll override the "default" drawing of the instance.
to get around that, you want to put draw_self() in there, so the player object still draws itself

sand verge
#

oh, thats why he disappears

limber copper
#

when you get a little more experienced you might even do away with the default drawing alltogether, and just use the draw functions for everything

#

more control, you can draw individual limbs and so on

sand verge
#

now he appears

#

i forgot the draw_self()

limber copper
#

depending on where you put the draw_self(), before or after the gun code, it will draw ontop or behind the gun

#

draw_self() is exactly the same as

draw_sprite_ext(sprite_index, image_index, x, y, image_xscale, image_yscale, image_angle, image_blend, image_alpha);
sand verge
#

i probably did something wrong, but the chaacter is still without the gun

limber copper
#

did you make sure you have a sprite named spr_gun

sand verge
#

yes

#

i cahnged it to my sprite

#

the gun doesnt follow him

limber copper
#

okay, where did you put the code

sand verge
#

wait a sec

#

your gamemaker is different than mine

limber copper
#

it is

#

it doesnt matter

#

you cant get this version anymore

sand verge
#

ok

#

is there a problem if i record with my phone?

limber copper
#

no, but idk if its necessary. i have a suspicion you put the code inside the obj_gun you have, instead of the player

sand verge
#

Yes

limber copper
#

its supposed to go into the player

sand verge
#

and, there is an issue

limber copper
#

and then you dont even need an obj_gun

sand verge
#

wait a sec

limber copper
sand verge
#

It's this type of 2d

limber copper
#

alright. i guess its easier if you work with objects.

so we are gonna do stuff a little bit different

sand verge
#

ok

limber copper
#

are you creating the gun in the player create event?
if so, we want to save the instance into a variable to reference later.
because if there is ever more than one obj_gun on screen, stuff is gonna get weird.

///obj_player

//create event:
my_gun = instance_create_depth(x, y, depth-1, obj_gun);  //we reference this specific gun

//step event:
var _direction,_xcomponent,_ycomponent;
_direction  = point_direction(x,y,mouse_x,mouse_y);
_xcomponent = lengthdir_x(32,_direction);
_ycomponent = lengthdir_y(32,_direction);

with (my_gun) {                                          //now we are sure to only move the players "personal" gun
  x = other.x + _xcomponent;                             //no matter how many obj_gun's are in the scene
  y = other.y + _ycomponent;
  image_angle = _direction;
}

//draw event:
draw_self();
sand verge
#

imma fix it

limber copper
#

ah well, remove the gun from the room

#

and update your player code with the new code

#

the create code will make sure to spawn a gun for the player once he is created

sand verge
#

i delete the draw_self from the gun?

limber copper
#

none of my code was ever meant to go inside your obj_gun

sand verge
#

ok

limber copper
#

if you have an emty draw event in the gun, it wont show

#

either remove the draw event entirely, or use draw_self() in there

#

by adding a draw event to an object, your telling gamemaker "hey, im gonna take matters into my own hands"

sand verge
#

(:

#

sometimes gamemaker is hard to understand

#

and im only just aweek

limber copper
#

it has some specific quirks

#

but they make sense once accustomed to them

#

most of the time

sand verge
#

i really want to thank you, you are using your time to help me

limber copper
#

all good, its good to get away from your own project once in a while

sand verge
#

you look like a proffesional

#

professional

#

how do you spell it

#

english is not my first language, sometimes i get stuck in some words

limber copper
#

one more thing, if you dont want the player to have the gun from the start, you could create an object
obj_gun_pickup.

if we restructure a bit

//player create event:
my_gun = noone; // we initialize the weapon variable;

//collision event obj_player with obj_gun_pickup
if (!instance_exists(my_gun)) {  //if we are not already holding a gun, we create one, and delete the pickup
  with (other) {
    instance_destroy();
    other.my_gun = instance_create_depth(x, y, other.depth-1, obj_gun);
  }
}
#

so you create obj_gun_pickup, give it the gun sprite, and thats it

#

then you place it in the room somewhere, and if you walk into it, the player should " pick it up"

sand verge
#

how do i fix, the gun is not on his hand, what do i need to change?

sand verge
limber copper
limber copper
sand verge
#

fixed position, fortunately

limber copper
#

no, fixed is good

sand verge
#

oh

limber copper
#

because else it gets way more complicated

#

how are you changing direction of your player

#

image_xscale?

sand verge
#

yep

limber copper
#

gotcha

#

im sorry for the wait, im having a brainfart for the _facing variable, this will do for now tho

var _direction,_facing,_xcomponent,_ycomponent;
_direction  = point_direction(x,y,mouse_x,mouse_y);
_facing     = 0;
if (image_xscale < 0) {
    _facing = 180;
}
_xcomponent = lengthdir_x(32,_facing);
_ycomponent = lengthdir_y(32,_facing);
sand verge
#

i was taking a look at your itch.io, if its not a problem

limber copper
#

yuh thats what its there for. dust off the cobwebs when your at it

#

not much traffic there

sand verge
#

yeah

#

what was supposed to do with the laers?

#

lazers

limber copper
#

got it, this is better.

//player step event:
var _direction,_facing,_xcomponent,_ycomponent;
_direction  = point_direction(x,y,mouse_x,mouse_y);
_facing     = 90 - (sign(image_xscale)*90);
_xcomponent = lengthdir_x(32,_facing);
_ycomponent = lengthdir_y(32,_facing);

with (my_gun) {                                          //now we are sure to only move the players "personal" gun
  x = other.x + _xcomponent;                             //no matter how many obj_gun's are in the scene
  y = other.y + _ycomponent;
  image_angle = _direction;
}
#

replaces the old step event code

#

makes the gun draw to the left or right side, based on your image_xscale

#

by adjusting the values for _xcomponent and _ycomponent, you can move the gun about relatively to the player. needs a bit of fiddling around on your end till you found the right spot where the gun should be

sand verge
#

it hasnt changed much

limber copper
#

the devil is in the details

sand verge
#

lol

#

th gun is still not in his arm

limber copper
#

ok but

sand verge
#

i think i need to chnage some xs and ys

limber copper
#

when you move left right, it should flip around right

sand verge
#

i dosnt really flip

#

see?

limber copper
#

oh it does, but, your sprite origins arent set to center

#

also, as i said, you need to adjust the values for x_component and _ycomponent

#

the 32, you can change that

#

also, here is a final rewrite, so the guns xscale changes with the players

//player step event:
var _direction,_facing,_xcomponent,_ycomponent;
_direction  = point_direction(x,y,mouse_x,mouse_y);
_facing     = 90 - (sign(image_xscale)*90);
_xcomponent = lengthdir_x(32,_facing);
_ycomponent = lengthdir_y(32,_facing);

with (my_gun) {                                            //now we are sure to only move the players"personal"gun
  x = other.x + _xcomponent;                               //no matter how many obj_gun's are in the scene
  y = other.y + _ycomponent;
  image_angle = _direction;
  image_xscale = abs(image_xscale)*sign(other.image_xscale); //this should mirror the gun sprite
}
sand verge
#

sorry, was playing hour zero, btw you did that alone?

limber copper
#

yes

#

still some way to go

#

started in 2017. project was put aside for the better half of that time tho