Jump to content


Photo
- - - - -

Advanced Alarm System Tutorial - Easy To Learn


  • Please log in to reply
20 replies to this topic

#1 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 07 January 2009 - 12:28 AM

  • Title: Advanced Alarm System (plus tutorial)
  • Description: Extending, and re-writing the alarm system in GML
  • GM Version: The example is in GM6, but can be ported to GM5 or GM7, if need be
  • Registered: No! :'D
  • File Type: zipped gm6 file
  • File Size: less than 1mb
  • File Link: DOWNLOAD
Additional Info
Those who are too lasy/tired to read the whole post can simply see the inner-workings of the example, and view the comments within the codes.

Here we go!
Since all of this example has comments (see if you can find some easter-eggs! - but keep them too yourself please) I will only be talking about some basic principles that you will have to follow (yes, this uses gml - and lots of it, but it is worth it) - so please stay with the topic and learn something cool ;)

Before you add ANY code to your game, make sure you have the ENTIRE script collection from the example file (located up top, in the download section), otherwise you will end up with missing script errors!

Always have the scripts in the example within the game you are making, and make sure not to use the same variables
(Otherwise your game will end up glitching, and un-desired performance will become an issue)

Alarms
Every object has a max of 32000 alarms this time, this is possible due to using arrays (and 4 short, quick and easy to use scripts) to simulate alarms. They are set-up as so (using the alarm_init(...) function):
alarm_init(50)
alarm_set(0,30)
This initializes 50 alarms that currently do nothing, and alarm[0] has 30 steps set (which doesn't do anything yet) - To make things happen (aka, make alarms go off) you have to go into the step event and SIMULATE your chosen alarm:

if alarm_step(0,1)
{
	// whee it worked :D Now create a blue ring! D<
	//effect_create_above(ef_ring,mouse_x,mouse_y,1,c_blue)
		instance_create(mouse_x,mouse_y,obj_effect)

	// sets alarm[0] back to 30
	alarm_set(0,30)
}
This code snippet checks, every alarm event related to alarm[0] to create a blue ring effect (using an object, for unregistered GMC member's) at the mouse's position - then sets the alarm back to it's initial value.
Because of the formatting, it also allows you to change the SPEED at which the alarm value counts down at, by simply changing this part:
if alarm_step(0,1)
to:
if alarm_step(0,2)
In other words, you are making it count-down twice as fast. You can also set it to 30 even - which makes an instant alarm event to call.

The other function that can be called is alarm_return(...), which returns the value at whatever alarm you decided to "watch". This was the sole reason to coding this engine.
Credit is not needed, but it would be nice to know where it is being used.

NOTE!:
If you want to globalise the alarms (aka, global.alarm[0]), then make sure you go through each of the 4 scripts and change:
alarm_number[...]
To:
global.alarm_number[...]

The alarm countdown speed is a very interesting function, where you could make it so that the alarm alternates between slow and fast countdowns each alarm event o___O
Another example is Halo's plasma rifle - at first it's firing rate is fairly mediocre, but then it quickens after a short period (another possibility that you could take advantage from this engine).

Also, the array limit of 32000 is only limited to each object, so it is possible to have a nearly infinite amount of alarms to deal with.

BUT WHO WOULD HAVE THAT MANY ALARMS ANYWAY???!!! \|'D

Edited by RamboFox, 21 April 2009 - 11:39 AM.

  • 0

#2 nickyboy115

nickyboy115

    GMC Member

  • New Member
  • 4 posts

Posted 07 January 2009 - 02:00 AM

It is only for pro. ;)
  • 0

#3 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 07 January 2009 - 02:35 AM

It is only for pro. :)

It's been done - all you need to do is remove the effect and replace it with a particle system (or something that is unreg, okay)

:)
  • 0

#4 pedrosorio

pedrosorio

    GMC Member

  • GMC Member
  • 971 posts

Posted 07 January 2009 - 02:40 AM

It is only for pro. :)


effect_create_above(ef_ring,mouse_x,mouse_y,1,c_bl
ue)

Only because of this. The alarm itself has nothing that lite version can't use.
  • 0

#5 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 07 January 2009 - 06:40 AM

It is only for pro. :)


effect_create_above(ef_ring,mouse_x,mouse_y,1,c_bl
ue)

Only because of this. The alarm itself has nothing that lite version can't use.

Well, after some minor editing - I show you the unregistered version (now it uses an object to draw the effect from a sprite).

You may kick me now for my lazy-ness (for not having an unregistered version beforehand), but I hope you'll find this better. :)

Still under 1mb *phew* D:
  • 0

#6 Marchal_Mig12

Marchal_Mig12

    The Rhouan

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

Posted 08 January 2009 - 04:38 AM

min() & max() functions are pretty slow due to the amount of arguments game maker need to treats. In order to optimize your engine you should use if statement instead of min() & max().

Mig
  • 0

#7 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 10 January 2009 - 05:18 AM

min() & max() functions are pretty slow due to the amount of arguments game maker need to treats. In order to optimize your engine you should use if statement instead of min() & max().

Mig

For future post, please test the examples thoroughly and test them against their' inbuilt counterparts - I have finally tested it against the inbuilt alarm system and lo behold, it is faster:

ALS [Advanced Alarm Systems]: 612 FPS max (With drawing the timer, calculating and creating the effect
IAS [Inbuilt Alarm System]: 590 FPS max (WITHOUT drawing the timer, or calculating, while creating the effect)

My system is apparently 22 fps faster than the inbuilt one! >>
(And that's with room_speed set at 9999 - my computer is crappy, so all my games are made to be speed-compatible)

Edited by RamboFox, 10 January 2009 - 05:35 AM.

  • 0

#8 Marchal_Mig12

Marchal_Mig12

    The Rhouan

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

Posted 10 January 2009 - 06:23 AM

I mean you should be using IF instead of min and max functions!

Mig
  • 0

#9 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 10 January 2009 - 06:45 AM

I mean you should be using IF instead of min and max functions!

Mig

Well, how would you limit the variables, while keeping the code clean and compact?
  • 0

#10 Marchal_Mig12

Marchal_Mig12

    The Rhouan

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

Posted 10 January 2009 - 06:56 AM

val = argument0
if alarms < val then
  val = alarms

INSTEAD OF

val=min(argument0,alarms)

I tested your example and ran it for 2 minutes and it averaged me 634 fps when setting 5 alarms each step while Game Maker built in alarm system averaged 792 fps when setting 5 alarms each step.

Mig

Edited by Marchal_Mig12, 10 January 2009 - 06:57 AM.

  • 0

#11 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 10 January 2009 - 07:04 AM

val = argument0
if alarms < val then
  val = alarms

INSTEAD OF

val=min(argument0,alarms)

I tested your example and ran it for 2 minutes and it averaged me 634 fps when setting 5 alarms each step while Game Maker built in alarm system averaged 792 fps when setting 5 alarms each step.

Mig

Well, that's interesting considering that the gml version (what I coded) averaged 615 fps for my computer, whereas the inbuilt function averaged around 590 :)

Edited by RamboFox, 10 January 2009 - 07:04 AM.

  • 0

#12 Marchal_Mig12

Marchal_Mig12

    The Rhouan

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

Posted 10 January 2009 - 07:07 AM

Well I would be interested in seeing how you made the test, how many time you ran both. And as I said, fps depends on many thing, it is not precise at all. Every gm built in functions are faster than everything we try to reproduce using gml. This is a fact.

Mig
  • 0

#13 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 10 January 2009 - 07:24 AM

Well I would be interested in seeing how you made the test, how many time you ran both. And as I said, fps depends on many thing, it is not precise at all. Every gm built in functions are faster than everything we try to reproduce using gml. This is a fact.

Mig

True, but GML is faster than D&D - which might be the trick, because not only in the step event is there the simulation of the alarm, but also the instance create effect (all in one block) whereas with the inbuilt function - it has to check each step and then create the effect.

Anyway, the point of me creating this tutorial is to extend the limited alarm system (11 alarms max, and no alarm value returning, without having an extra variable to keep track of).

Another reason to why it's faster for me is because of my processor architecture, or the way my computer deals with it's memory xD

[EDIT]:
I ran both examples (one with my alarm system, the other with the inbuilt alarm system) separately, and long enough for the fps to stabilize.

Edited by RamboFox, 10 January 2009 - 07:25 AM.

  • 0

#14 Marchal_Mig12

Marchal_Mig12

    The Rhouan

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

Posted 10 January 2009 - 07:26 AM

I used your example without creating any effects though. I think we should still be using gm in built alarm system... Or Mark wouldn't made one if it would of been slower.

Mig
  • 0

#15 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 10 January 2009 - 07:39 AM

Yeah, it'd be the D&D that slows it down.. Wait - if you didn't create an effect, you simply set the alarm[0] back up to 30 with the inbuilt one, right?

Are you also drawing the value of the gml version too? Because, that was the whole reason to creating this - to return the alarm variable. Also, the alarm system that I wrote (I only just realized lol) is compatible with delta time (which can be useful to allot of GMC members)
  • 0

#16 Marchal_Mig12

Marchal_Mig12

    The Rhouan

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

Posted 10 January 2009 - 07:59 AM

You're right, it is useful when using delta time, but instead of using script, we should be able to make our own alarm system in order to be the most efficient possible. (Scripts are slow).

Mig
  • 0

#17 Campadrenalin

Campadrenalin

    GMC Member

  • New Member
  • 59 posts

Posted 14 January 2009 - 01:36 AM

Now I'm not really experienced in DLLs, but I know they're capable of making and storing objects. If someone could write an extension that handled this stuff, it would go incredibly fast.
  • 0

#18 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 14 January 2009 - 05:48 AM

Good point Campadrenalin, I plan on porting this into a *.gex so that the functions are fully integrated ^_^

Dll's are not my strong point though - so you'll have to contact someone who does know c++ or something (like Roach)
  • 0

#19 grumpymonkey

grumpymonkey

    GMC Member

  • GMC Member
  • 1011 posts

Posted 14 January 2009 - 02:03 PM

your alarms are not faster, they just end faster. GM's built in alarms end at -1, not 0. so for them to be accurate they should be set 1 step less, for example
instead of alarm[0]=30, you would use alarm[0]=29.

i tested the framerate i got by using both with a room speed of 9999, and this is what i found:

- both of them made it drop by about 10fps
- yours always finished 1 step first
- using both of them repeteadly, the MOST that my framerate dropped was by 40

both alarms decrease their value by 1 every step, so there really isnt any difference of their speed. yours is just always one step ahead of the built in alarms, if you made them end at -1 instead of 0, you would see that they are the same speed.
overall, i think i might end up using yours since only having 11 alarms is really annoying >.<

i give this a 4/5

EDIT:
try putting this in the draw event to see what i did:
if mouse_check_button_pressed(mb_left){alarm[0]=30; alarm_set(0,30); io_clear(); exit;}
draw_set_color(c_lime)

draw_text
(
		 mouse_x,
		 mouse_y,
		 string(alarm_return(0))
)
draw_text(mouse_x+30,mouse_y,alarm[0])
score=fps
and this in the alarm 0 event:
instance_create(mouse_x+60,mouse_y,obj_effect)
if is_auto
{alarm[0]=5}
then set the roomspeed to something small, like 1, or 5 so you can see how they change

Edited by grumpymonkey, 14 January 2009 - 02:08 PM.

  • 0

#20 melancor

melancor

    GMC Member

  • New Member
  • 104 posts

Posted 14 January 2009 - 03:39 PM

I too have felt sometimes that 12 alarm events per object might not be enough... but then I realized that... if I need more alarms than that, then there's probably something wrong with my programming style/structure. Consider that "real" programming languages don't even provide alarms.

Anyway - it's clean coded, functional... and I guess useful for some ppl. Give him Credit ^_^

Edited by melancor, 14 January 2009 - 03:41 PM.

  • 0

#21 RamboFox

RamboFox

    Tainted Fortune

  • New Member
  • 992 posts

Posted 21 April 2009 - 11:30 AM

I too have felt sometimes that 12 alarm events per object might not be enough... but then I realized that... if I need more alarms than that, then there's probably something wrong with my programming style/structure. Consider that "real" programming languages don't even provide alarms.

C++, nor it's wrapper to turn it into a game support alarms - everything has to be scratch-coded D:

Luckily, here we have a simple system known as gml :snitch:
(Also, legal bump)

Woot first comment on second page!

Edited by RamboFox, 21 April 2009 - 11:30 AM.

  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users