### Adding time delay to certain functions

Scripting questions, discussions, etc

#### Adding time delay to certain functions

Duion
Posts: 1047
Joined: Sun Feb 08, 2015 1:51 am

I would like to add a time delay to certain functions the player can execute by pressing a button on the keyboard, mouse etc, since that way they can be executed as fast as the player can click or press.
For example functions like switching weapons, toggling iron sight aiming mode etc.
Its not a functional problem, but more a cosmetic one, since it does not look and feel very realistic, if you can execute certain tasks in no time.

Ok for weapons this is usually solved with a state machine, but there are other functions, like the aiming function I added that lets you switch to iron sight aiming mode by pressing right mouse button:
https://github.com/Duion/Uebergame/blob ... nd.cs#L783

This is based on some resource from the old forum, it quickly exchanges the weapon images and adds zoom to simulate aiming mode, the problem is just, that it can be pressed infinitely fast and therefore can look ugly and unrealistic if the player presses the right mouse button very fast.
It is not part of the state machine, since that would require source code changes by adding that functionality to the weapon class, but I don't have the time and skill to do that, so has anyone an idea what I could use as a workaround?
Scheduling does not work, since it jut delays the execution, but does not limit the input speed.
Only idea I have is to lock it and automatically unlock after a certain amount of time with a schedule, but this system is prone to errors and I don't know if it works well with very small delays like 50-300ms.

#### Re: Adding time delay to certain functions

irei1as
Posts: 76
Joined: Fri Feb 27, 2015 5:13 pm
I think the easiest way would be to set a global variable with getSimTime() when you do something with a cooldown.

$start_of_cooldown = getSimTime(); Then when you use the sight use a conditional to check if the actual getSimTime() is bigger than that variable with the desired increase. %aaa=getSimTime(); if (%aaa <$start_of_cooldown + 2000) return; //2000 ms cooldown, example
-Here do the other thigs-

The problem with this approach shows when the states have different cooldowns between actions. It can become very complex really fast.

#### Re: Adding time delay to certain functions

marauder2k9
Posts: 162
Joined: Wed Feb 18, 2015 7:36 am
i think most of the time there are 2 more states in the state machine for the weapon that act as transitions and play an animation. The time it takes to play the animation becomes the delay then

- state 1 : Iron sights transition
{
play enter iron sights transition animation
}

- state 2 : iron sights on
{
iron sights on
}

- state 3 : iron sights off
{
play iron sights out transition
}

#### Re: Adding time delay to certain functions

Duion
Posts: 1047
Joined: Sun Feb 08, 2015 1:51 am

I just wrote that I need something for things that are not part of the state machine, I could do it with a state machine already.
The problem comes if you use functions, triggers, callbacks etc that are outside of the state machine, which happens quite a lot.
The state machine itself is also prone to errors, since you often have to add some kind of delay to certain states otherwise they cannot execute correctly, but this often gives an opportunity to hack the system.
You can even interrupt script functions, if you are able to jam buttons fast enough.

However I will try the cooldown thing, maybe I use some kind of global cooldown that is used for all actions.

In pro gaming there is something called APS or APM which means actions per second or minute and pro gamers can do up to 10 actions per scond and that over longer periods, so in peak periods it can be even higher.
The problem with that is, it is unrealistic for a shooter kind of game, since a normal human reaction time is at least 200ms which is 5 APS and then there is more delay added since a human has to execute the actions as well.
In games like counter strike you often see people switching weapons all the time, since you move a little faster with knife pulled, which is a stupid and illogical concept by itself and these symptoms I try to prevent.
I think in most games you can do as many actions as fast as you can press buttons, but it is unrealistic.

#### Re: Adding time delay to certain functions

Duion
Posts: 1047
Joined: Sun Feb 08, 2015 1:51 am

Well I found out that what I want to do is possible through the state machine in a state I overlooked so far, it is calle "preactivate" which by default has no timeout value.
If you add a timeout value there and set waitfortimeout to true, then you will have a delay before you can switch to the next weapon, but there is a big problem with this method, since if you add a timeout value in that state, the weapon will only switch in place after that delay is over.
What it looks like that the weapon in first person starts out in the middle of the screen and only properly is put in the hand of the player when the preactivate state finishes, probably that is why it has no timeout value by default.

#### Re: Adding time delay to certain functions

Duion
Posts: 1047
Joined: Sun Feb 08, 2015 1:51 am

With my paintball players that I made myself this method works, since they use real first person view and there cannot go anything wrong there it seems.
However this only works for adding delay to switching weapons, not for activating iron sight zoom, which is weird since iron sight zoom is a wepon switch as well, since I have two weapon images, the iron sight weapon is a derivative of the original one with eye offset added.
I will look further into it since mounting weapons through script is different than selecting them.

#### Who is online

Users browsing this forum: No registered users and 3 guests