GMS 2.3+ Trying to set a timer for a screen shake

So I have my view system set up so that I can just put the code "global.cameraShake = 0.3;" wherever I like and it will make a little screenshake. It works and I have it setup so that whenever the player is on the rm_start menu this screenshake is in effect. However, I don't want the screen to just shake indefinitely. I want it to shake at random intervals if possible. I've tried setting up an alarm to get it to shake at regular intervals with no avail. I was doing this:

Step:

if (room = rm_start){
alarm_set(1, 60);
}

Alarm 1:

global.cameraShake = 0.3;

Shouldn't that at least make the camera shake every second? And if I do get this working how would I randomize it?
 

Trandoxiana

Member
To be honest, I don't use alarms, (I probably should) so I don't know exactly how they work, but I can tell you that you can use a function like random_range() to generate a random number (irandom_range()to be inclusive). So you could write something like
GML:
random_shake = irandom_range(1, 60);
if (random_shake == 60)
{
    global.cameraShake = 0.3
}
(in the step event, of course)
Sorry about my lack of knowledge about alarms, but that is how I would do it, and I think it is a pretty simple solution. You can check the docs for more information on the random functions if you want it.
 

Nidoking

Member
if (room = rm_start){
alarm_set(1, 60);
}
This is in your step event. It sets the alarm to 60 every step while you're in the rm_start room. Now, if you set it to 60 every step, how many steps will it take to get to zero and activate the event? (Hint - it's too big to be a number.) Try only setting it if it's not already running (i.e. if it's less than zero).
 

kburkhart84

Firehammer Games
1. If its me, instead of putting this code in a controller object and then checking if you are in the rm_start room, I would say its better to just have an object that only does the stuff that is needed for the rm_start, which at the least is the screenshake.

2. I'm guessing what is happening is that you have nothing in there that doesn't set the alarm. So every single step, you are setting the alarm to 60, meaning it never gets the change down to execute its code, which would be why the screenshake doesn't happen.

3. There is nothing wrong with using alarms for this. Assuming simply setting global.cameraShake to 0.3 will make the screenshake effect happen, which will then die down on its own(maybe controlled in some other object, you could have the following code in the step event for the rm_start controller. I would also add variables for minTime and maxTime which correspond to time between shakes.

Code:
if(alarm[1] == 0)
{
    var timeToWait = irandom_range(minTime, maxTime);//maybe minTime should be 30, and max should be 90, so it would be a good variety of different wait times
    alarm[1] = timeToWait;// or you can use alarm_set(1, timeToWait);
}
 
1. If its me, instead of putting this code in a controller object and then checking if you are in the rm_start room, I would say its better to just have an object that only does the stuff that is needed for the rm_start, which at the least is the screenshake.

2. I'm guessing what is happening is that you have nothing in there that doesn't set the alarm. So every single step, you are setting the alarm to 60, meaning it never gets the change down to execute its code, which would be why the screenshake doesn't happen.

3. There is nothing wrong with using alarms for this. Assuming simply setting global.cameraShake to 0.3 will make the screenshake effect happen, which will then die down on its own(maybe controlled in some other object, you could have the following code in the step event for the rm_start controller. I would also add variables for minTime and maxTime which correspond to time between shakes.

Code:
if(alarm[1] == 0)
{
    var timeToWait = irandom_range(minTime, maxTime);//maybe minTime should be 30, and max should be 90, so it would be a good variety of different wait times
    alarm[1] = timeToWait;// or you can use alarm_set(1, timeToWait);
}
So for some reason this still has not worked. Can't figure it out. Here's what I put in the persistent object(obj_game) controller I use for room events:

Alarm 1:

if (room = rm_start){
global.cameraShake = 0.03;
}

And in the step event I put your code:

if(alarm[1] == 0)
{
var timeToWait = irandom_range(30, 90);
alarm[1] = timeToWait;
}
 
To be honest, I don't use alarms, (I probably should) so I don't know exactly how they work, but I can tell you that you can use a function like random_range() to generate a random number (irandom_range()to be inclusive). So you could write something like
GML:
random_shake = irandom_range(1, 60);
if (random_shake == 60)
{
    global.cameraShake = 0.3
}
(in the step event, of course)
Sorry about my lack of knowledge about alarms, but that is how I would do it, and I think it is a pretty simple solution. You can check the docs for more information on the random functions if you want it.
This worked for me. Thank you. And if there was a way I didn't have to use alarms I wouldn't either. lol They come in handy sometimes though.
 
Top