Author Topic: Looping Actions?  (Read 2275 times)

BaronBlade

  • Spike Dodger
  • Posts: 140
  • bees?
  • OS:
  • Windows 8.1/Server 2012 Windows 8.1/Server 2012
  • Browser:
  • Chrome 46.0.2490.86 Chrome 46.0.2490.86
    • View Profile
  • Playstyle: Keyboard
Looping Actions?
« on: November 26, 2015, 11:02:00 PM »
Hello! I'm trying to recreate the cannon enemies from I Wanna Be the Aura, but I'm having problems with having them launch projectiles. In my cannon object, I have a create event and two alarms.

Create:
Code: [Select]
alarm[0]=0;
Alarm 0:
Code: [Select]
var i;
for(i = 0; i < 3; i++) {
    alarm[1] = 25;
}

alarm[0] = 100;

Alarm 1:
Creates moving instance of the cannon ball object moving upwards at speed 3

Unfortunately, nothing happens when I run the game. Any help would be appreciated. Thanks!
bean!

Kyir

  • The Kid
  • Posts: 293
  • Normal Guy
  • OS:
  • Windows 7/Server 2008 R2 Windows 7/Server 2008 R2
  • Browser:
  • Chrome 46.0.2490.86 Chrome 46.0.2490.86
    • View Profile
  • Playstyle: Keyboard
Re: Looping Actions?
« Reply #1 on: November 27, 2015, 07:32:12 AM »
Though this doesn't exactly answer your question, I'd honestly just say alarms are more trouble than they're worth when it comes to things like this. You would probably have an easier time with setting a variable in create and then just ticking it up/down in step until it reaches a certain point, then executing whatever action you want and resetting the variable.

patrickgh3

  • Spike Dodger
  • Posts: 169
  • stay optimistic! :D
  • OS:
  • Windows 7/Server 2008 R2 Windows 7/Server 2008 R2
  • Browser:
  • Chrome 46.0.2490.86 Chrome 46.0.2490.86
    • View Profile
    • Github
  • Playstyle: Keyboard
Re: Looping Actions?
« Reply #2 on: November 27, 2015, 08:13:36 AM »
Here's a snippet of a basic cannon using alarms: https://klazen.com/gm/QVK

Let me also tell you what's wrong with the code you posted. By default, all alarms start out at 0. Alarms will only trigger if you set them to a value above 0, and then they count down back to 0 later. So, setting alarm[0] to 0 does nothing; the alarm event will never trigger! If you want the alarm to trigger immediately, you can set it to 1.

In your alarm 0 event, you set alarm[1] to 25 three times over, which is unnecessary. The alarm will only trigger once, not three times; that's just how alarms work.

In regards to what Kyir said about emulating alarms yourself, I think which method you use depends on personal preference and context. In one-off objects like this cannon I might prefer to use alarms to keep things simple. In more complex objects, I might prefer to do it manually.

BaronBlade

  • Spike Dodger
  • Posts: 140
  • bees?
  • OS:
  • Windows 8.1/Server 2012 Windows 8.1/Server 2012
  • Browser:
  • Chrome 46.0.2490.86 Chrome 46.0.2490.86
    • View Profile
  • Playstyle: Keyboard
Re: Looping Actions?
« Reply #3 on: November 27, 2015, 09:56:07 AM »
Here's a snippet of a basic cannon using alarms: https://klazen.com/gm/QVK

Let me also tell you what's wrong with the code you posted. By default, all alarms start out at 0. Alarms will only trigger if you set them to a value above 0, and then they count down back to 0 later. So, setting alarm[0] to 0 does nothing; the alarm event will never trigger! If you want the alarm to trigger immediately, you can set it to 1.

In your alarm 0 event, you set alarm[1] to 25 three times over, which is unnecessary. The alarm will only trigger once, not three times; that's just how alarms work.

In regards to what Kyir said about emulating alarms yourself, I think which method you use depends on personal preference and context. In one-off objects like this cannon I might prefer to use alarms to keep things simple. In more complex objects, I might prefer to do it manually.

Alright, I understand what you're saying about alarms. However, when I try to implement that code snippet, GMS throws me this error:

ERROR in
action number 1
of Alarm Event for alarm 0
for object objCannon:

Creating instance for non-existing object: 448
 at gml_Object_objCannon_ObjAlarm0_1 (line 3) - new_cannonball = instance_create(objCannonBall, self.x, self.y)

I do have an object called objCannonBall, which only deletes upon hitting a block, so I'm perplexed.
bean!

Sudnep

  • Global Moderator
  • Spike Dodger
  • Posts: 124
  • OS:
  • Windows 10 Windows 10
  • Browser:
  • Chrome 46.0.2490.86 Chrome 46.0.2490.86
    • View Profile
  • Playstyle: Keyboard
Re: Looping Actions?
« Reply #4 on: November 27, 2015, 09:59:45 AM »
Here's a snippet of a basic cannon using alarms: https://klazen.com/gm/QVK

Let me also tell you what's wrong with the code you posted. By default, all alarms start out at 0. Alarms will only trigger if you set them to a value above 0, and then they count down back to 0 later. So, setting alarm[0] to 0 does nothing; the alarm event will never trigger! If you want the alarm to trigger immediately, you can set it to 1.

In your alarm 0 event, you set alarm[1] to 25 three times over, which is unnecessary. The alarm will only trigger once, not three times; that's just how alarms work.

In regards to what Kyir said about emulating alarms yourself, I think which method you use depends on personal preference and context. In one-off objects like this cannon I might prefer to use alarms to keep things simple. In more complex objects, I might prefer to do it manually.

Alright, I understand what you're saying about alarms. However, when I try to implement that code snippet, GMS throws me this error:

ERROR in
action number 1
of Alarm Event for alarm 0
for object objCannon:

Creating instance for non-existing object: 448
 at gml_Object_objCannon_ObjAlarm0_1 (line 3) - new_cannonball = instance_create(objCannonBall, self.x, self.y)

I do have an object called objCannonBall, which only deletes upon hitting a block, so I'm perplexed.
instance_create(x,y,object)
not instance_create(object,x,y)

BaronBlade

  • Spike Dodger
  • Posts: 140
  • bees?
  • OS:
  • Windows 8.1/Server 2012 Windows 8.1/Server 2012
  • Browser:
  • Chrome 46.0.2490.86 Chrome 46.0.2490.86
    • View Profile
  • Playstyle: Keyboard
Re: Looping Actions?
« Reply #5 on: November 27, 2015, 11:02:35 AM »
instance_create(x,y,object)
not instance_create(object,x,y)

:paraPalm: I guess I should have checked how that function worked. Thanks, sudnep!
bean!