Jump to content


Photo

GM8 Variables not working?


  • Please log in to reply
9 replies to this topic

#1 Crasher111

Crasher111

    GMC Member

  • New Member
  • 11 posts

Posted 07 January 2012 - 09:24 AM

So I'm making a 2D TD game and I have the enemies collide with the 'towers' (or in this case, defences)

Each defence has 2 hp, which I have set with a variable. I have a collision event with the 'enemies' (or a green square) which decreases one 'hp'. In the step event, I have it so if it's lower than 1 'hp', than it will destroy itself.

This works when you place it down in the room, but not if you (the player) place it. I have tested it many times, and it still doesn't work. The enemies collide, die, and nothing happens. The code is down below:

CREATE EVENT
DefenseHealth = 2

STEP EVENT
if DefenseHealth<=1
then instance_destroy()

COLLISION WITH ENEMY EVENT
DefenseHealth= -1

Most of my game is GML, including the movement and making of the towers.

STEP EVENT FOR PLAYER (the tower building)
//Defenses Building (press C!!!!)
if keyboard_check_pressed(ord('C'))
DefenseBuild=1

if keyboard_check_released(ord('C'))
DefenseBuild=0
Score = -10

//But we DON'T won't to build things MID AIR don't we? This is what this code here does.
if Ground=0 // <--- If this is set to one, it will actually make it go in the air.
then DefenseBuild=false // <--- THIS ACTUALLY WORKS!

//But what does DefenseBuild mean? Well, all it does is build your 'Defense' (or in this case, a wall). The variable has it's own set of code below
//stating what it does. All if ACTUALLY does is build the 'Defense' in your current position. This is going to upgraded later on.
if DefenseBuild = 1
then action_create_object(obj_defense,obj_player.x,obj_player.y)

Could someone help me? It's essential to have this fixed.

Thanks.

P.S. And yes, that is my code. I've been working on this for a while now.

Edited by Crasher111, 05 November 2012 - 12:52 AM.

  • 0

#2 Zwander

Zwander

    GMC Member

  • New Member
  • 181 posts
  • Version:GM8

Posted 07 January 2012 - 09:34 AM

Erm... I might be on the complete wrong track here (I haven't used the collision event for ages), but try putting the event in the -enemies collision event with the tower- ie:
with (other) {DefenseHealth=-1}

or (if you want it to take two hits)
with (other) {DefenseHealth-=1}

Oh, also...
if keyboard_check_released(ord('C'))
DefenseBuild=0
Score = -10
This bit need to be:
if keyboard_check_released(ord('C'))    
	{    
	DefenseBuild=0    
	Score = -10    
	}
Otherwise it will set the score to -10 every step. Unless that's what you intended.

Edited by Zwander, 07 January 2012 - 09:42 AM.

  • 0

#3 Crasher111

Crasher111

    GMC Member

  • New Member
  • 11 posts

Posted 07 January 2012 - 09:01 PM

Doesn't work. I put down the tower and what happens is that 2 enemies collide with it, and it doesn't disappear. I MAY have a solution to it though.

Thanks anyway.

Edit: Well, that didn't work. All I did was make a new object and in the Create event, change the object to my defence. It's in D&D though, as I dunno what the code is to change an instance.

Edited by Crasher111, 07 January 2012 - 09:04 PM.

  • 0

#4 Zwander

Zwander

    GMC Member

  • New Member
  • 181 posts
  • Version:GM8

Posted 07 January 2012 - 10:24 PM

Doesn't work. I put down the tower and what happens is that 2 enemies collide with it, and it doesn't disappear. I MAY have a solution to it though.

Thanks anyway.

Edit: Well, that didn't work. All I did was make a new object and in the Create event, change the object to my defence. It's in D&D though, as I dunno what the code is to change an instance.


So... is it working?
  • 0

#5 Crasher111

Crasher111

    GMC Member

  • New Member
  • 11 posts

Posted 07 January 2012 - 10:52 PM

No, both yours and my solution didn't work.
  • 0

#6 Zwander

Zwander

    GMC Member

  • New Member
  • 181 posts
  • Version:GM8

Posted 07 January 2012 - 11:28 PM

No, both yours and my solution didn't work.


Ok, then the bug is somewhere else. have you used the de-bugger to check if its health is actually being reduced? We need to isolate the bug.
  • 0

#7 torigara

torigara

    GMC Member

  • GMC Member
  • 6486 posts

Posted 08 January 2012 - 06:31 AM

For starters, you should use the official documented function instance_create rather than undocumented one, action_create_object.

You probably have created two or more defenses at the same position on top of each other. So, you don't see they disappear until all of them get destroyed.
- you set DefenseBuild to 1 when C key is pressed, and reset it to 0 when C is released.
- every step, a defense is created if DefenseBuild is 1.
The logical consequence is that it keeps creating defense as long as C key is held down. You will get multiple copies unless you release the key very quickly.

Edited by torigara, 08 January 2012 - 06:35 AM.

  • 0

#8 Zwander

Zwander

    GMC Member

  • New Member
  • 181 posts
  • Version:GM8

Posted 08 January 2012 - 07:58 AM

You probably have created two or more defenses at the same position on top of each other. So, you don't see they disappear until all of them get destroyed.
- you set DefenseBuild to 1 when C key is pressed, and reset it to 0 when C is released.
- every step, a defense is created if DefenseBuild is 1.
The logical consequence is that it keeps creating defense as long as C key is held down. You will get multiple copies unless you release the key very quickly.


hmm, i didn't think of that,
  • 0

#9 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5417 posts
  • Version:GM:Studio

Posted 08 January 2012 - 08:08 AM

- you have given it 2 hp .. but do the destroy check at 1 hp instead at 0 hp

- collisions can happen many times each second, so you might want to set an alarm in the tower so it can only recive dmg at certain intervals .. might be better to do it in the enemy since multible enemies can attack a wall..

enemy collision with tower:
if alarm[0]<0{ // if alarm 0 is off
  other.DefenseHealth -= 1;
  alarm[0]=room_speed; // 1 second delay til next dmg
}
enemy alarm 0 event:
// leave a comment there to keep countdown active

- your build code can be mashed together into:
if keyboard_check_pressed(ord('C'))
if Ground=0
{
  Score = -10
  instance_create(obj_player.x,obj_player.y,obj_defense)
}

  • 0

#10 Crasher111

Crasher111

    GMC Member

  • New Member
  • 11 posts

Posted 24 October 2012 - 07:22 AM

- you have given it 2 hp .. but do the destroy check at 1 hp instead at 0 hp

- collisions can happen many times each second, so you might want to set an alarm in the tower so it can only recive dmg at certain intervals .. might be better to do it in the enemy since multible enemies can attack a wall..

enemy collision with tower:

if alarm[0]<0{ // if alarm 0 is off
  other.DefenseHealth -= 1;
  alarm[0]=room_speed; // 1 second delay til next dmg
}
enemy alarm 0 event:
// leave a comment there to keep countdown active

- your build code can be mashed together into:
if keyboard_check_pressed(ord('C'))
if Ground=0
{
  Score = -10
  instance_create(obj_player.x,obj_player.y,obj_defense)
}


Sorry for the huge bump, but I just went back to this project and I realized that I found my fix!

Seriously, thanks for this. You're going in the credits.

Could someone please close this?
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users