Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: A Pushing/Pulling code, where i cant see where the error occurs.  (Read 2070 times)

0 Members and 1 Guest are viewing this topic.
A Pushing/Pulling code, where i cant see where t...
« on: May 12, 2012, 04:23:41 pm »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
Hello, in my game i have a pushing/pulling code. and its decently long, so im posting it at the end. i have the code where if there is the block(named Push) in front of you, you are allowed to grab it with Z. now if you are grabbing it, you are assigned a place, if you are grabbing it from the left, global.grableft = true and same for the other directions. if you press right while grabbing it from the left, you go right. and ect. now for pulling if you grab it from the left and press left, you go backwards. thats all my code is. for some reason i cannot get this working fluent. im lost on it, so any pointers would be appreciated. also if there is a way to clean up my code for it to be shorter, please tell me. thanks, so what im looking for is a pulling/pushing script that acts like the MM or OOT pulling(im sure the other 3Ds have the same) but on a LTTP style, since i dont like their way of pushing if you hit a wall. thank you once again
-Whitay.

the script:
Code: [Select]
///Checking if we should start grabbing

if collision_point(x,y+6,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

if collision_point(x-6,y,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

if collision_point(x,y+6,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

if collision_point(x,y-6,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

if collision_point(x,y,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

else
{
global.cangrab = false
}

//////////////////GRABBING//////////////////
if global.cangrab = true
{

if (alarm[6] < 0 or alarm[6] = 0) and (sprite_index = spr_left or sprite_index = spr_leftS) and keyboard_check_pressed(ord("Z"))
{
global.grableft = true
sprite_index = spr_PushL
image_speed = 0
}

if (alarm[6] < 0 or alarm[6] = 0) and (sprite_index = spr_right or sprite_index = spr_rightS) and keyboard_check_pressed(ord("Z"))
{
global.grabright = true
sprite_index = spr_PushR
image_speed = 0
}

if (alarm[6] < 0 or alarm[6] = 0) and (sprite_index = spr_up or sprite_index = spr_upS) and keyboard_check_pressed(ord("Z"))
{
global.grabup = true
sprite_index = spr_PushU
image_speed = 0
}

if (alarm[6] < 0 or alarm[6] = 0)  and (sprite_index = spr_down or sprite_index = spr_downS) and keyboard_check_pressed(ord("Z"))
{
global.grabdown = true
sprite_index = spr_PushD
image_speed = 0
}
//////////////////////PUSHING
if global.grableft = true and keyboard_check_direct(vk_left) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1) //and keyboard_check_pressed(ord("Z"))
{
global.pushleft = true
sprite_index = spr_PushL
image_speed = 0.3
alarm[6] = 3
}

if global.grabright = true and keyboard_check_direct(vk_right) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1) //and keyboard_check_pressed(ord("Z"))
{
global.pushright = true
sprite_index = spr_PushR
image_speed = 0.3
alarm[6] = 3
}

if global.grabup = true and keyboard_check_direct(vk_up) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1) //and keyboard_check_pressed(ord("Z"))
{
global.pushup = true
sprite_index = spr_PushU
image_speed = 0.3
alarm[6] = 3
}

if global.grabdown = true  and keyboard_check_direct(vk_down) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1)  //and keyboard_check_pressed(ord("Z"))
{
global.pushdown = true
sprite_index = spr_PushD
image_speed = 0.3
alarm[6] = 3
}
///////////////////////////////PULLING
if global.grableft = true and keyboard_check_direct(vk_right) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1) and keyboard_check_pressed(ord("Z"))
{
global.pullleft = true
sprite_index = spr_PullL
image_speed = 0.3
alarm[6] = 3
}

if global.grabright = true and keyboard_check_direct(vk_left) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1) and keyboard_check_pressed(ord("Z"))
{
global.pullright = true
sprite_index = spr_PullR
image_speed = 0.3
alarm[6] = 3
}

if global.grabup = true and keyboard_check_direct(vk_down) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1) and keyboard_check_pressed(ord("Z"))
{
global.pullup = true
sprite_index = spr_PullU
image_speed = 0.3
alarm[6] = 3
}

if global.grabdown = true and keyboard_check_direct(vk_up) and (alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1) and keyboard_check_pressed(ord("Z"))
{
global.pulldown = true
sprite_index = spr_PullD
image_speed = 0.3
alarm[6] = 3
}
}


///////////////////KEY RELEASES
if keyboard_check_released(vk_left) and global.pushleft = true and keyboard_check_pressed(ord("Z"))
{
global.grableft = true
sprite_index = spr_PushL
image_speed = 0
alarm[6] = -1
}

if keyboard_check_released(vk_right) and global.pushright = true and keyboard_check_pressed(ord("Z"))
{
global.grabright = true
sprite_index = spr_PushR
image_speed = 0
alarm[6] = -1
}

if keyboard_check_released(vk_up) and global.pushup = true and keyboard_check_pressed(ord("Z"))
{
global.grabup = true
sprite_index = spr_PushU
image_speed = 0
alarm[6] = -1
}

if keyboard_check_released(vk_down) and global.pushdown = true and keyboard_check_pressed(ord("Z"))
{
global.grabdown = true
sprite_index = spr_PushD
image_speed = 0
alarm[6] = -1
}
///////////////////////////////////////////
if keyboard_check_released(ord("Z")) and sprite_index = spr_PushL
{
sprite_index = spr_leftS
global.canpull = false
global.grableft = false
global.grabright = false
global.grabup = false
global.grabdown = false
global.pushup = false
global.pushright = false
global.pushleft = false
global.pushdown = false
global.pullup = false
global.pullright = false
global.pullleft = false
global.pulldown = false
alarm[6] = -1
}

if keyboard_check_released(ord("Z")) and sprite_index = spr_PushR
{
sprite_index = spr_rightS
global.canpull = false
global.grableft = false
global.grabright = false
global.grabup = false
global.grabdown = false
global.pushup = false
global.pushright = false
global.pushleft = false
global.pushdown = false
global.pullup = false
global.pullright = false
global.pullleft = false
global.pulldown = false
alarm[6] = -1
}

if keyboard_check_released(ord("Z")) and sprite_index = spr_PushU
{
sprite_index = spr_upS
global.canpull = false
global.grableft = false
global.grabright = false
global.grabup = false
global.grabdown = false
global.pushup = false
global.pushright = false
global.pushleft = false
global.pushdown = false
global.pullup = false
global.pullright = false
global.pullleft = false
global.pulldown = false
alarm[6] = -1
}

if keyboard_check_released(ord("Z")) and sprite_index = spr_PushD
{
sprite_index = spr_downS
global.canpull = false
global.grableft = false
global.grabright = false
global.grabup = false
global.grabdown = false
global.pushup = false
global.pushright = false
global.pushleft = false
global.pushdown = false
global.pullup = false
global.pullright = false
global.pullleft = false
global.pulldown = false
alarm[6] = -1
}

Alarm[5]:
Code: [Select]
////if place is free
if place_free(x+4,y)
{
global.canpull = false
global.cangrab = false
global.canpush = false
}

if place_free(x-4,y)
{
global.canpull = false
global.cangrab = false
global.canpush = false
}

if place_free(x,y+4)
{
global.canpull = false
global.cangrab = false
global.canpush = false
}

if place_free(x,y-4)
{
global.canpull = false
global.cangrab = false
global.canpush = false
}

/////if place isnt free
if !place_free(x+4,y) and sprite_index = spr_right
{
global.canpull = true
global.canpush = true
canroll = false
global.cangrab = true
}

if !place_free(x-4,y) and sprite_index = spr_left
{
global.canpull = true
global.canpush = true
canroll = false
global.cangrab = true
}

if !place_free(x,y+4) and sprite_index = spr_down
{
global.canpull = true
global.canpush = true
canroll = false
global.cangrab = true
}

if !place_free(x,y-4) and sprite_index = spr_up
{
global.canpull = true
canroll = false
global.canpush = true
global.cangrab = true
}
alarm[6]:
Code: [Select]
if sprite_index = spr_PushL and global.pushleft = true
{
x -= 4
sprite_index = spr_PushL
image_speed = 0.3
alarm[6] = 3
}

if sprite_index = spr_PushR and global.pushright = true
{
x += 4
sprite_index = spr_PushR
image_speed = 0.3
alarm[6] = 3
}

if sprite_index = spr_PushU and global.pushup = true
{
y -= 4
sprite_index = spr_PushU
image_speed = 0.3
alarm[6] = 3
}

if sprite_index = spr_PushD and global.pushdown = true
{
y += 4
sprite_index = spr_PushD
image_speed = 0.3
alarm[6] = 3
}
//////////////////////////////////////////////////////////////////
if sprite_index = spr_PullL and global.pullleft = true
{
x += 4
sprite_index = spr_PullL
image_speed = 0.3
alarm[6] = 3
}

if sprite_index = spr_PullR and global.pullright = true
{
x -= 4
sprite_index = spr_PullR
image_speed = 0.3
alarm[6] = 3
}

if sprite_index = spr_PullU and global.pullup = true
{
y += 4
sprite_index = spr_PullU
image_speed = 0.3
alarm[6] = 3
}

if sprite_index = spr_PullD and global.pulldown = true
{
y -= 4
sprite_index = spr_PullD
image_speed = 0.3
alarm[6] = 3
}


my error is half the time i cant even grab it, unless im walking into it. if im walking it glitches up. the whole code isnt working and i have no idea why.
« Last Edit: May 12, 2012, 04:27:03 pm by Whitay »
Logged
Re: A Pushing/Pulling code, where i cant see whe...
« Reply #1 on: May 12, 2012, 09:38:03 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Okay, you have a lot to learn about how to use the if-else control structure. Seriously it is awful. I quoted a part of the script and see already several errors.

the script:

--- New If structure (1) ---
if collision_point(x,y+6,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

--- New If structure (2) ---
if collision_point(x-6,y,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

--- New If structure (3) ---
if collision_point(x,y+6,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

--- New If structure (4) ---
if collision_point(x,y-6,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}

--- New If-else structure (5)---
if collision_point(x,y,Push,true,false) and keyboard_check_pressed(ord("Z"))
{
global.cangrab = true
}
else
{
global.cangrab = false
}



my error is half the time i cant even grab it, unless im walking into it. if im walking it glitches up. the whole code isnt working and i have no idea why.
The reason why you can't grab unless you are standing on top of it, is because that is the only time when structure (5) evaluates to true and thus the bold 'else' is not executed.

The reason why your entire code isn't working is because of the improper use of the if-else structure. If you just list a bunch of if statements after each other, does not automatically mean that the other if won't be executed when the first one is executed. If one of those evaluates to true then it still is executed. And if one of those evaluates false and has an else part then the else part is executed.

Then you have things like this: alarm[6] < 0 or alarm[6] = 0 or alarm[6] = -1. When something is -1 it means that it is automatically also smaller than 0. And smaller than 0 or equals to 0 can also be pulled together. Thus the entire expression should look alarm[6] <= 0. BTW, using a '=' as an equals symbol instead of '==' is really bad coding convention. Because '=' is actually an assignment, but GM compiler corrects it for you.

And you know that comments do nothing. Putting one somewhere does not automatically mean a different part of the code starts. I reorganized your script code and even removed redundancies.

Code: [Select]
///Checking if we should start grabbing
if(keyboard_check_pressed(ord("Z")))
{
if ( collision_point(x,y+6,Push,true,false)
|| collision_point(x-6,y,Push,true,false)
|| collision_point(x,y+6,Push,true,false)
|| collision_point(x,y-6,Push,true,false)
||  collision_point(x,y,Push,true,false) )
{
global.cangrab = true;
}
else
{
global.cangrab = false;
}
}
else
{
global.cangrab = false;
}


//////////////////GRABBING//////////////////
if (global.cangrab = true )
{
if(alarm[6] <= 0)
if( keyboard_check_pressed(ord("Z")))
{
//I assume that these 4 booleans are mutual exclusive. Meaning if on is true the others should be false.
//global.grableft = false;
//global.grabright = false;
//global.grabup = false;
//global.grabdown = false;

if (sprite_index = spr_left or sprite_index = spr_leftS)
{
global.grableft = true;
global.grabright = false;
global.grabup = false;
global.grabdown = false;
sprite_index = spr_PushL;
image_speed = 0;
}
else if (sprite_index = spr_right or sprite_index = spr_rightS)
{
global.grableft = false;
global.grabright = true;
global.grabup = false;
global.grabdown = false;
sprite_index = spr_PushR;
image_speed = 0;
}
else if (sprite_index = spr_up or sprite_index = spr_upS)
{
global.grableft = false;
global.grabright = false;
global.grabup = true;
global.grabdown = false;
sprite_index = spr_PushU;
image_speed = 0
}
else if (sprite_index = spr_down or sprite_index = spr_downS)
{
global.grableft = false;
global.grabright = false;
global.grabup = false;
global.grabdown = true;
sprite_index = spr_PushD;
image_speed = 0;
}

///////////////////////////////PULLING
if (global.grableft and keyboard_check_direct(vk_right))
{
global.pullleft = true;
global.pullright = false;
global.pullup = false;
global.pulldown = false;
sprite_index = spr_PullL;
image_speed = 0.3;
alarm[6] = 3;
}
else if global.grabright and keyboard_check_direct(vk_left))
{
global.pullleft = false;
global.pullright = true;
global.pullup = false;
global.pulldown = false;
sprite_index = spr_PullR;
image_speed = 0.3;
alarm[6] = 3;
}
else if global.grabup and keyboard_check_direct(vk_down))
{
global.pullleft = false;
global.pullright = false;
global.pullup = true;
global.pulldown = false;
sprite_index = spr_PullU;
image_speed = 0.3;
alarm[6] = 3;
}
else if global.grabdown and keyboard_check_direct(vk_up))
{
global.pullleft = false;
global.pullright = false;
global.pullup = false;
global.pulldown = true;
sprite_index = spr_PullD;
image_speed = 0.3;
alarm[6] = 3;
}
}

//////////////////////PUSHING
if (global.grableft and keyboard_check_direct(vk_left)) //and keyboard_check_pressed(ord("Z"))
{
global.pushleft = true;
global.pushright = false;
global.pushup = false;
global.pushdown = false;
sprite_index = spr_PushL;
image_speed = 0.3;
alarm[6] = 3;
}
else if (global.grabright and keyboard_check_direct(vk_right)) //and keyboard_check_pressed(ord("Z"))
{
global.pushleft = false;
global.pushright = true;
global.pushup = false;
global.pushdown = false;
sprite_index = spr_PushR;
image_speed = 0.3;
alarm[6] = 3;
}
else if (global.grabup and keyboard_check_direct(vk_up)) //and keyboard_check_pressed(ord("Z"))
{
global.pushleft = false;
global.pushright = false;
global.pushup = true;
global.pushdown = false;
sprite_index = spr_PushU;
image_speed = 0.3;
alarm[6] = 3;
}
else if (global.grabdown and keyboard_check_direct(vk_down))  //and keyboard_check_pressed(ord("Z"))
{
global.pushleft = false;
global.pushright = false;
global.pushup = false;
global.pushdown = true;
sprite_index = spr_PushD;
image_speed = 0.3;
alarm[6] = 3;
}
}
}


///////////////////KEY RELEASES
if(keyboard_check_pressed(ord("Z")))
{
if (keyboard_check_released(vk_left) and global.pushleft)
{
global.grableft = true;
global.grabright = false;
global.grabup = false;
global.grabdown = false;
sprite_index = spr_PushL;
image_speed = 0;
alarm[6] = -1;
}
else if (keyboard_check_released(vk_right) and global.pushright)
{
global.grableft = false;
global.grabright = true;
global.grabup = false;
global.grabdown = false;
sprite_index = spr_PushR;
image_speed = 0;
alarm[6] = -1;
}
else if (keyboard_check_released(vk_up) and global.pushup)
{
global.grableft = false;
global.grabright = false;
global.grabup = true;
global.grabdown = false;
sprite_index = spr_PushU;
image_speed = 0;
alarm[6] = -1;
}
else if (keyboard_check_released(vk_down) and global.pushdown)
{
global.grableft = false;
global.grabright = false;
global.grabup = false;
global.grabdown = true;
sprite_index = spr_PushD;
image_speed = 0;
alarm[6] = -1;
}
}
///////////////////////////////////////////
else if(keyboard_check_released(ord("Z")))
{
if(sprite_index = spr_PushL)
{
sprite_index = spr_leftS;
}
else if(sprite_index = spr_PushR)
{
sprite_index = spr_rightS;
}
else if(sprite_index = spr_PushU)
{
sprite_index = spr_upS;
}
else if(sprite_index = spr_PushD)
{
sprite_index = spr_downS;
}

global.canpull = false;
global.grableft = false;
global.grabright = false;
global.grabup = false;
global.grabdown = false;
global.pushup = false;
global.pushright = false;
global.pushleft = false;
global.pushdown = false;
global.pullup = false;
global.pullright = false;
global.pullleft = false;
global.pulldown = false;
alarm[6] = -1;
}

I will not say that the above code is without errors, because something in you model seems to be off. However it should work better then your previous one. So study this bit of code very well. I also have some advices for you:
1) Look into Finite State Machines. And try to implement that instead of a shitload of mutually exclusive booleans.
2) Make use of switch statements. In some cases they are better especially with FSMs.
3) Try to sketch the flow of your code in a flowchart or something. It often shows you what you do wrong.

I haven't looked at the other pieces of code and I guess you will find the same kind of errors there. BUt I think you can solve that for yourself now.
« Last Edit: May 12, 2012, 09:48:40 pm by Niek »
Logged
Re: A Pushing/Pulling code, where i cant see whe...
« Reply #2 on: May 13, 2012, 04:19:53 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
What you want to do here is condense your if statement.  This can made MUCH simpler.  First off, factor out your keyboard code.  Then, organize the rest of the conditions so it makes logical sense.

Code: [Select]

if (keyboard_check_pressed(ord("Z"))
{
   if (collision_point(x,y+6,Push,true,false) or collision_point(x-6,y,Push,true,false) or collision_point(x,y-6,Push,true,false) or collision_point(x,y+6,Push,true,false) or collision_point(x,y,Push,true,false))
      global.cangrab = true
   else
      global.cangrab = false
}
else
    global.cangrab = false


So now you have first, an outter if which checks the keyboard.  Then, an inner if which checks all 5 possible grab conditions.  If any of them evaluate to true, then you're able to grab.  No need to worry about multiple ifs or short-circuiting your logic.
« Last Edit: May 13, 2012, 04:25:11 am by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: A Pushing/Pulling code, where i cant see whe...
« Reply #3 on: May 14, 2012, 06:10:23 pm »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
I could say a lot, but in terms of finding out what is grabbable, you should double-check to make sure that the sprite used as the mask index of the character who is doing the grabbing has its origin in the middle of itself. It doesn't necessarily have to be in the middle but if it is in the top-left 0,0 corner the detection you are trying to achieve might not work the way you want it to.

Also in terms of determining what is grabbable as well as the way you are handling sprites, using the built-in direction variable for your character would be ideal and would simplify your code even more. You could reduce your checking a lot, for example, if you would check for what is grabbable like this:

Code: [Select]
global.cangrab = noone;
switch (direction){
  case 180: global.cangrab = collision_point(x-6,y,Push,true,false); break;
  case 0: global.cangrab = collision_point(x+6,y,Push,true,false); break;
  case 90: global.cangrab = collision_point(x,y-6,Push,true,false); break;
  default: global.cangrab = collision_point(x,y+6,Push,true,false); break;
}

The function collision_point returns either the instance id or noone. The instance id is always greater than 0, and noone is actually a symbolic way of having a -4. This means you can treat those values as true and false.

The 180, 0, and 90 represent direction; on a unit circle they represent degrees corresponding to left, right, and up. I don't mention down because as long as you know it is only possible to check in 4 directions, if you know it is not the first 3 of those directions it must be the last one. The main reason I mention using direction is because you avoid multiple calls to points you are not facing - if you are facing away from what you are trying to grab, then you're not really supposed to be able to pick it up, no? Further though, the collision_point you did looking at x,y is unnecessary - assuming what you are grabbing is solid you should always be outside of it (I could be wrong I don't know your exact setup).

Anyway though I worked on a push/pull system in the past. I don't know when I will release its improved version that I am working on, but the newer version would be documented better and would help you with the finite state stuff Niek mentioned too.
Logged
Re: A Pushing/Pulling code, where i cant see whe...
« Reply #4 on: May 15, 2012, 12:02:19 am »
  • *
  • Reputation: +2/-0
  • Offline Offline
  • Gender: Male
  • Posts: 269
Thank you all so much for the help, i now have a working code thanks to you all. Plus some of my other code has been boosted to better quality too. I would say
More but im on a phone with low battery. Thanks Again,
 whitay.
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.044 seconds with 47 queries.