GameMaker Studio: How to ignore global left button when dealing with virtual keys? - game-maker

I have the following problem:
When I press the pause button my characters jumps at the same time, the pause button being a VIRTUAL KEY and the jump button is keyboard_mouse_button_check(mb_left) .
I am trying to:
be able to press the pause button without making the character jump at the same time.
Obj_pause
Create Event
global.pause = 0;
Step Event
if keyboard_check_pressed(vk_space) and !global.death and !global.stop
global.pause = !global.pause
Obj_player
Create Event
// Initialize Variables
global.stop = 0;
previously_paused = false;
global.bigscore = 0;
global.col = true;
global.cartof = 0;
grav = 0.2;
hsp = 0;
vsp = 0;
jumpspeed = 4;
movespeed = 4;
antigrav = true;
jumps = 0;
jumpsmax = 2;
delay = 3;
alarm[3] = delay;
Step Event
if (global.pause) or (global.death) exit; // Pause
// Get the player's input
key_jump = mouse_check_button(mb_left);
// React to inputs
hsp = 0;
if vsp < 10
vsp += grav;
if place_meeting(x, y + 1, obj_wall)
jumps = jumpsmax;
// Double jump
if (key_jump) && (jumps > 0) && (!previously_paused)
{
jumps -= 1;
vsp = -jumpspeed;
previously_paused = global.pause;
}
// Fly
else if (jumps == 0)
{
key_jump = mouse_check_button(mb_left);
if key_jump
vsp -= 0.4
}
if global.col
{
// Horizontal Collision
if (place_meeting(x+hsp, y, obj_wall))
{
while (!place_meeting(x + sign(hsp), y, obj_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
}
// Vertical Collision
if (place_meeting(x, y + vsp, obj_wall))
{
while (!place_meeting(x, y + sign(vsp), obj_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
// Exit the game or restart
if keyboard_check(ord("P"))
game_restart();
if keyboard_check(vk_space)
alarm[1] = room_speed;
// Score
if background_hspeed[0] < 0
global.cartof += 10;
I have tried:
Check if I press the pause button don't do the jump To change key_jump to be equal 0 when I click on the pause button. When mouse is over the pause button to dezactivate the mouse ArchbishopDave Method
I am using:
GM:S 1.4
GameMaker Language (GML) or Drag and Drop (DnD)

Did you read event order? Step events will be runned before Keyboard and Mouse events (and virtual buttons too). So if you checked jump button in Step event, and only after it will be pressed virtual key, then your character always will jump.
You need change order of checks. For example, you can manually check, is pressed virtual key or not, before jump (check, is mouse inside coords of virtual key or not), something like this:
key_jump = mouse_check_button(mb_left);
if point_in_rectangle(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), virt_left, virt_top, virt_right, virt_bottom)
key_jump = false;
where virt_left, virt_top, virt_right, virt_bottom is coords of your virtual button.
There are many ways for improve it, but all depend on your code/architecture (depth of objects, creation order of instances, etc), so I can't give a finihsed perfect solution. If you will have a problem with fix, just share your project and I'll help.

Related

Simple Button using DRAW GUI in GameMaker

This is my button under DRAW GUI.
if point_in_rectangle(window_mouse_get_x(),window_mouse_get_y(),790,317,943,385)
{ var hover=1; } else { var hover=0; }
draw_sprite(spr_mainmenu_smallbutton,hover,865,360);
if (distance_to_point(mouse_x,mouse_y)<=0)
{
if mouse_check_button(mb_left)
draw_sprite(spr_mainmenu_smallbutton,2,865,360);
}
draw_set_color(make_colour_rgb(163,190,240));
draw_set_font(fnt_mainmenu_text);
draw_text(865,350,"New Game");
Fairly simple. It draws a rectangle and the text "New Game" on it.
When the mouse hovers over it, it lights up. When you click it, the graphic changes. It's a good button.
The problem occurs when I enter the area of the button while HOLDING the mouse button.
See how it behaves? Instead of lighting up, it gets pressed.
All because I am holding the mouse button as I come in. Any way to avoid this and have it light up instead?
You need use mouse_check_button_pressed() instead mouse_check_button().
Something like this:
Create event:
button_pressed = false;
mouse_over = false;
button_x = 865;
button_y = 350;
button_width = 153;
button_height = 68;
button_left = button_x - button_width div 2;
button_right = button_left + button_width - 1;
button_top = button_y - button_height div 2;
button_bottom = button_top + button_height - 1;
Step end event:
mouse_over = point_in_rectangle(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), button_left, button_top, button_right, button_bottom);
if !mouse_over
button_pressed = false;
else
{
if mouse_check_button_pressed(mb_left)
{
button_pressed = true;
}
else if mouse_check_button_released(mb_left) and button_pressed
{
// action
show_message("pressed");
}
}
Draw GUI event:
if button_pressed
draw_sprite(sprite_index, 0, button_x, button_y);
else if mouse_over
draw_sprite(sprite_index, 1, button_x, button_y);
else
draw_sprite(sprite_index, 2, button_x, button_y);

How to make more alarm in game marker

Hello am using game maker to create my project. My project has a start button and when you click it, the alarms will go off and run code which i set in the alarm for example.
Example, alarm[0] = 30 // set the timer
Now in alarm[0] we add code.
Create (x,y ,player); // code i want to run when i click the start button.
However game maker only has 11 alarms i wanted to know if there were ways to create more alarms like alarm[14] and so on.
An alarm is just a variable.
Some scripts for custom alarms:
script custom_alarms_initialize. Call it from Create event
/// custom_alarms_initialize(alarms_number)
for (var i=0; i<argument0; i++)
{
custom_alarm[i, 0] = -1;
custom_alarm[i, 1] = -1;
}
script custom_alarms_step. Call it from Step Begin event
/// custom_alarms_step()
var sz = array_height_2d(custom_alarm);
for (var i=0; i<sz; i++)
{
if custom_alarm[i, 0] != -1
{
custom_alarm[i, 0]--; // Counter
if custom_alarm[i, 0] = 0
{
custom_alarm[i, 0] = -1;
if script_exists(custom_alarm[i, 1])
script_execute(custom_alarm[i, 1]);
}
}
}
script custom_alarm_set. Use it for start alarm
/// custom_alarm_set(alarm, time, script)
var sz = array_height_2d(custom_alarm);
if argument0 < sz
{
custom_alarm[argument0, 0] = max(-1, argument1);
custom_alarm[argument0, 1] = argument2;
}
else
{
show_message("Out of range (alarm " + string(argument0) + "). Max alarm id: " + string(sz - 1));
}
script custom_alarm_get_current. Use if you want get current value.
/// custom_alarm_get_current(alarm)
var sz = array_height_2d(custom_alarm);
if argument0 < sz
{
return custom_alarm[argument0, 0];
}
else
{
show_message("Out of range (alarm " + string(argument0) + "). Max alarm id: " + string(sz - 1));
}
Example of use:
alarm_start(5, room_speed * 10, your_action);
where your_action - name of the script which will be executed.
P.S. This code is for GameMaker Studio, not for GM 8.
P.P.S. Link to example: click

Unity - Making Camera Lock on To Enemy and stay behind player?

I am attempting to create a Camera that moves with the Player but locks onto an enemy when the player clicks the lock on button. The behaviour is almost working as I want it, the camera locks onto the target. And when the player stand in-front of the target it works fine. However as soon as the player runs past the target, the camera behaves strangely. It still looks at the Enemy, however it does not stay behind the player. Here is the code that dictates the behaviour:
if(MouseLock.MouseLocked && !lockedOn){ // MOUSE CONTROL:
Data.Azimuth += Input.GetAxis("Mouse X") * OrbitSpeed.x;
Data.Zenith += Input.GetAxis("Mouse Y") * OrbitSpeed.y;
} else if(lockedOn) { // LOCKON BEHAVIOUR:
FindClosestEnemy();
}
if (Target != null) {
lookAt += Target.transform.position;
base.Update ();
gameObject.transform.position += lookAt;
if(!lockedOn){
gameObject.transform.LookAt (lookAt);
} else if(enemyTarget != null) {
Vector3 pos1 = Target.transform.position ;
Vector3 pos2 = enemyTarget.transform.position ;
Vector3 dir = (pos2 - pos1).normalized ;
Vector3 perpDir = Vector3.Cross(dir, Vector3.right) ;
Vector3 midPoint = (pos1 + pos2) / 2f;
gameObject.transform.LookAt (midPoint);
}
}
And the Code for Finding the nearest Enemy:
void FindClosestEnemy () {
int numEnemies = 0;
var hitColliders = Physics.OverlapSphere(transform.position, lockOnRange);
foreach (var hit in hitColliders) {
if (!hit || hit.gameObject == this.gameObject || hit.gameObject.tag == this.gameObject.tag){
continue;
}
if(hit.tag != "Enemy") // IF NOT AN ENEMY: DONT LOCK ON
continue;
var relativePoint = Camera.main.transform.InverseTransformPoint(hit.transform.position);
if(relativePoint.z < 0){
continue;
}
numEnemies += 1;
if(enemyTarget == null){
print ("TARGET FOUND");
enemyTarget = hit;
}
}
if(numEnemies < 1){
lockedOn = false;
enemyTarget = null;
}
}
As I said, teh behaviour almost works as expected, however I need the camera to stay behind the player whilst locked on and it must face the enemy/midPoint between the enemy and player. How can this be done? Thank you for your time.
To clarify your intent: you want to lock the position relative to the target (player), whilst setting the camera rotation to look at either the target or a secondary target (enemy)? And your current code performs the rotation correctly but the positioning is buggy?
The easiest way to fix the camera relative to another object is to parent it in the scene. In your case you could add the camera as a child under the Player game object.
If you would rather not do this then look at your positioning code again:
lookAt += Target.transform.position;
base.Update ();
gameObject.transform.position += lookAt;
I don't know where lookAt comes from originally but to me this looks all wrong. Something called lookAt should have nothing to do with position and I doubt you want to += anything in the positioning code given that you want a fixed relative position. Try this instead:
public float followDistance; // class instance variable = distance back from target
public float followHeight; // class instance variable = camera height
...
if (Target != null) {
Vector3 newPos = target.position + (-Target.transform.forward * followDistance);
newPos.y += followHeight;
transform.position = newPos;
}
This should fix the positioning. Set the followDistance and followHeight to whatever you desire. Assuming your rotation code works this should fix the problem.

Switching sprites of a object in different rooms

I am creating a character selection which changes the sprites of the main character.
To do this I have an arrow object which the user clicks to change the sprite of the main character.
global.Mario = true;
global.PrincessPeach = false;
global.Luigi = false;
global.Bowser = false;
if (mouse_check_button_pressed(mb_left)) {
if (global.Mario = true) {
Mario = false;
PrincessPeach= true;
}
if (global.PrincessPeach= true) {
PrincessPeach = true;
Mario = false;
}
if (global.Luigi = true) {
Luigi = false;
Bowser = true;
}
if (global.Bowser = true) {
Bowser = false;
Mario = true;
}
}
Then, on my main character I have a created an event executing code similar to the following:
if (global.Mario = true) {
sprite_index = Mario_NotJumping;
}
if (global.Luigi = true) {
sprite_index = Luigispr;
}
However, when I run my game to test it out, I get the following error:
FATAL ERROR in
action number 1
of Create Event
for object Mario_selection:
Push :: Execution Error - Variable Get -5.Mario(100000, -1)
at gml_Object_Mario_selection_Create_0 (line 1) - if (global.Mario = true) {
The object Mario_selection has the exact create event and same code as the main character. To display changes to the user.
If anyone could help me out, I am fairly new to GameMaker so I have a feeling I'm just misunderstanding global variables.
It looks like the var global.Mario, has not been declared. In the arrow objects create event put,
global.Mario = true;
global.PrincessPeach = false;
global.Luigi = false;
global.Bowser = false;
If i were you, i would save the chosen "image" of the user in 1 simple variable.
Add a few constants;
PLAYER_MARIO = 0;
PLAYER_PEACH = 1;
PLAYER_LUIGI = 2;
PLAYER_BOWSER = 3;
Then you can use those constants and save them in a global variable;
global.player_image = PLAYER_LUIGI;
Then you can use those in your objects like so;
switch (global.player_image) {
case PLAYER_MARIO:
sprite_index = Mario_NotJumping;
break;
case PLAYER_PEACH:
sprite_index = Peach_NotJumping;
break;
case PLAYER_LUIGI:
sprite_index = Luigi_NotJumping;
break;
case PLAYER_BOWSER:
sprite_index = Bowser_NotJumping;
break;
}
The specific error you were getting was because the global variable was not defined at the time of use.
A better method by the way, would be something like this - saving all sprite handles into specific variables, then using those.
Define some constants for readability;
P_LEFT = 0;
P_RIGHT = 1;
P_JUMP = 2;
Then whenever a player switches its character;
//When the user switches to peach;
global.player_sprite[P_LEFT] = spr_Peach_Left;
global.player_sprite[P_RIGHT] = spr_Peach_Right;
global.player_sprite[P_JUMPING] = spr_Peach_Jumping;
And overwrite;
//When the user switches to Luigi;
global.player_sprite[P_LEFT] = spr_Luigi_Left;
global.player_sprite[P_RIGHT] = spr_Luigi_Right;
global.player_sprite[P_JUMPING] = spr_Luigi_Jumping;
Then you can code all code like this;
if (jumping) {
sprite_index = global.player_sprite[P_JUMPING];
} else {
sprite_index = global.player_sprite[P_LEFT];
}
Do you understand what i mean? This way you won't need the switch statements everywhere, and just store the chosen sprites in 1 single array at the beginning of the game.
You can then code the game without having to check what player image the player chose.
Also, always define global variables at the beginning of the game. Global variables will always exist in the game, in every room, at any given moment.
You should use var mario=true; instead.

Can't get my object to point at the mouse

I'm using a combination of SDL and OpenGL in a sort of crash course project to teach myself how this all works. I'm really only interested in OpenGL as a way to use acceleration in 2D games so I just need this to work in a 2D plane.
I have been having a lot of problems today with my current issue, I would like an object to point towards the mouse while the mouse button is clicked and then of course stay pointing in that direction after the mouse is lifted.
void Square::handle_input() {
//If a key was pressed
if( event.type == SDL_KEYDOWN ) {
//Adjust the velocity
switch( event.key.keysym.sym ) {
case SDLK_UP: upUp = false; yVel = -1; break;
case SDLK_DOWN: downUp = false; yVel = 1; break;
case SDLK_LEFT: leftUp = false; xVel = -1; break;
case SDLK_RIGHT: rightUp = false; xVel = 1; break;
case SDLK_w: wUp = false; sAng = 1; break;
case SDLK_s: sUp = false; sAng = -1; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP ) {
//Adjust the velocity
switch( event.key.keysym.sym ) {
case SDLK_UP: upUp = true; yVel = 0; break;
case SDLK_DOWN: downUp = true; yVel = 0; break;
case SDLK_LEFT: leftUp = true; xVel = 0; break;
case SDLK_RIGHT: rightUp = true; xVel = 0; break;
case SDLK_w: wUp = true; sAng = 0; break;
case SDLK_s: sUp = true; sAng = 0; break;
}
}
//If a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN ) {
switch ( event.type ) {
case SDL_MOUSEBUTTONDOWN: mouseUp = false; mousex == event.button.x; mousey == event.button.y; break;
case SDL_MOUSEBUTTONUP: mouseUp = true; break;
}
}
}
And then this is called at the end of my Object::Move call which also handles x and y translation
if (!mouseUp) {
xVect = mousex - x;
yVect = mousey - y;
radAng = atan2 ( mousey - y, mousex - x );
sAng = radAng * 180 / 3.1415926l;
}
Right now when I click the object turns and faces down to the bottom left but then no longer changes direction. I'd really appreciate any help I could get here. I'm guessing there might be an issue here with state versus polled events but from all the tutorials that I've been through I was pretty sure I had fixed that. I've just hit a wall and I need some advice!
I'm assuming that you want the object to keep pointing at the mouse's position while the mouse button is held. To fix this, you will have to update the mouse position every time the mouse is moved.
Add some code similar to this:
if( event.type == SDL_MOUSEMOVE ) {
// update the mouse position here using event.???.x / y
}
Note that I haven't got an SDL reference here, so I can't give you the exact members, but that should help.
NB: I would also guess that there would be problems with your button handling code. You have an if statement that checks for one value of event.type, but then inside its body you have a switch statement with two values. Only one of these values will ever be executed - you should probably think about just using 2 separate if statements for the button down / button up events.

Resources