Quantcast

Jump to content

» «
Photo

[.NET] Clarification on Tick, KeyUp, and KeyDown.

8 replies to this topic
OfficerJohnson
  • OfficerJohnson

    Crackhead

  • Members
  • Joined: 07 Jan 2018
  • United-States

#1

Posted 3 weeks ago

Hello again. Another need for clarification about C#. As usual, right to the point.

Thanks to the tips I've been given so far to fast track my understanding of the structure of a script, I've looked into a few. I notice the structure 90% contains a Tick, a KeyDown, and a KeyUp. I'm assuming these are from the Script.cpp class?

I'm also seeing this:

public [script_name] {
KeyUp += onKeyUp;
KeyDown += onKeyDown;
Tick += onTick;
}
I'm guessing this is a way to call these values through Encapsulation? Is it necessary to use all three? Even if you just want to create a script based solely on each frame?

I have somewhat a clue what each of these do but I wouldn't mind if another user here explained each.

We have member functions for checking if controls are pressed. If we want to check if keyboard keys are pressed, we use KeyUp/Down? When we invoke the Tick, we are asking for whatever we code to be checked performed every frame?
  • Bob_74 likes this

Jitnaught
  • Jitnaught

    Grass tastes bad

  • Members
  • Joined: 24 Aug 2013
  • United-States

#2

Posted 3 weeks ago Edited by Jitnaught, 3 weeks ago.

Yes the main events of SHVDN are Tick, KeyDown, and KeyUp. None of them are required, although since you are not supposed to run native functions in the constructor (public [script_name]), let's just say that having at least one of the events is required.

 

What you're seeing in the constructor is that the events are being subscribed to. SHVDN internally invokes the event handler whenever each event happens (key is pressed, it's been [Interval] since last tick), and the event handler calls each function that is subscribed to it. Since you subscribed onKeyUp, onKeyDown, and onTick, those functions will be called whenever that event handler is invoked.

 

The KeyDown and KeyUp events will tell you when a key is pressed/unpressed, but they won't tell you at a specific time if a key is pressed or not. Meaning it's not useful if you're wanting to know if a certain key is pressed at any time after the KeyUp event or between the KeyDown and KeyUp event (although you could set a boolean when a key is pressed/unpressed then check that booleans value to see if the key is pressed or not).

 

Yes, when you subscribe to the Tick event it is basically asking for your code to be called every [Interval] milliseconds (which is by default 0).

 

Also you can subscribe to events anywhere you want, it doesn't have to be in the constructor. Additionally you can unsubscribe from them by replacing += with -=

  • Bob_74 likes this

Bob_74
  • Bob_74

    Crackhead

  • Members
  • Joined: 05 Jan 2018
  • None

#3

Posted 3 weeks ago

Also you can subscribe to events anywhere you want, it doesn't have to be in the constructor. Additionally you can unsubscribe from them by replacing += with -=

I don't think you can subscribe the main class Tick event from another class since it needs an instance of your main class (inheriting "Script") to subscribe this event

Do I get it right? I wanted to do so but I ended up subscribing an event from my other class inside my main class:

class MainClass : Script
{
    internal MyOtherClass otherClassInstance;

    public MainClass()
    {
        otherClassInstance = new MyOtherClass();
        Tick += otherClassInstance.MyEvent;
    }
}

I didn't try to inherit "Script" on my other classes since I don't know how SHVDN would react (I assume it is looking for a script inherited class as an entry point?).

 

Can you tell me if this is the right way to use the Tick event or if there is a simpler way (ie: inheriting "Script" in other classes)?


OfficerJohnson
  • OfficerJohnson

    Crackhead

  • Members
  • Joined: 07 Jan 2018
  • United-States

#4

Posted 3 weeks ago

Okay. Let me add onto this. So, I'm getting a bit confused following the logic of the Boolean value when testing onTick and KeyUp/Down events.

I'll use an example from a script I've been reviewing:

bool boostActive = false;

Now, I get the logic behind playing with bool values. What I don't get is when they are used to create the decision making.

if (boostActive && isControlPressed()) {
// do something;
}

if (!boostActive) {
// do something else;
}
If I have read the logic of decision making statements correctly, the first one will enter the if statement if both condition 1 and 2 both return TRUE, right? But in this case, boostActive is set to false? So IF false && true = false, right? Therefore, these statements would be skipped?

For the other, IF true, then we can do the other statements in this decision making, right? Well if I'm going to use key pressing logic correctly, I'm gonna need to make sure I'm not confusing myself further.

Jitnaught
  • Jitnaught

    Grass tastes bad

  • Members
  • Joined: 24 Aug 2013
  • United-States

#5

Posted 3 weeks ago

Okay. Let me add onto this. So, I'm getting a bit confused following the logic of the Boolean value when testing onTick and KeyUp/Down events.

I'll use an example from a script I've been reviewing:

bool boostActive = false;

Now, I get the logic behind playing with bool values. What I don't get is when they are used to create the decision making.

if (boostActive && isControlPressed()) {
// do something;
}
if (!boostActive) {
// do something else;
}

If I have read the logic of decision making statements correctly, the first one will enter the if statement if both condition 1 and 2 both return TRUE, right? But in this case, boostActive is set to false? So IF false && true = false, right? Therefore, these statements would be skipped?

For the other, IF true, then we can do the other statements in this decision making, right? Well if I'm going to use key pressing logic correctly, I'm gonna need to make sure I'm not confusing myself further.

Yes the first if statement would be skipped if boostActive or isControlPressed return false. Sorry I can't help with your other questions because I don't understand them.

 

I don't think you can subscribe the main class Tick event from another class since it needs an instance of your main class (inheriting "Script") to subscribe this event

 

Do I get it right? I wanted to do so but I ended up subscribing an event from my other class inside my main class:

[snip]

 

I didn't try to inherit "Script" on my other classes since I don't know how SHVDN would react (I assume it is looking for a script inherited class as an entry point?).

 

Can you tell me if this is the right way to use the Tick event or if there is a simpler way (ie: inheriting "Script" in other classes)?

 

Yes you would need a reference (variable) to the instance of the class to be able to subscribe to events. I don't think classes need to inherit Script to be able to have a function within it be subscribed with.

If you're only wanting one Tick event then that would work, although it's a bit messy imo. Better to keep it all in one class. If you're wanting multiple Tick events (totally separated), for example if you wanted to do 2 things at the same time, you would have to create another class that inherits Script and subscribe to that class's Tick event.

  • nm710 and Bob_74 like this

Bob_74
  • Bob_74

    Crackhead

  • Members
  • Joined: 05 Jan 2018
  • None

#6

Posted 3 weeks ago

Yes you would need a reference (variable) to the instance of the class to be able to subscribe to events. I don't think classes need to inherit Script to be able to have a function within it be subscribed with.
If you're only wanting one Tick event then that would work, although it's a bit messy imo. Better to keep it all in one class. If you're wanting multiple Tick events (totally separated), for example if you wanted to do 2 things at the same time, you would have to create another class that inherits Script and subscribe to that class's Tick event.

I didn't try to create an other class inheriting "Script" since NativeUI uses the Tick event from the main class and doesn't seem to inherit "Script" itself.
I'll try to inherit "Script" in one of my classes. Would it create another thread to run its Tick or are Ticks on the same thread even from different inherited "Script" classes?

I thought threading was missing from SHVDN whereas RPH deals with it very well but if each instance of "Script" has its thread, it wouldn't be a problem anymore!

Jitnaught
  • Jitnaught

    Grass tastes bad

  • Members
  • Joined: 24 Aug 2013
  • United-States

#7

Posted 3 weeks ago

SHVDN is missing an easy way to create new threads. Every Script-inherited class will have it's own thread.

  • Bob_74 likes this

Bob_74
  • Bob_74

    Crackhead

  • Members
  • Joined: 05 Jan 2018
  • None

#8

Posted 3 weeks ago Edited by Bob_74, 3 weeks ago.

Every Script-inherited class will have it's own thread.

I've just tried and, damn... I wish I knew that when I started designing my mod!

It will make the code a lot more simpler!

 

Thanks a lot, really!

  • Jitnaught and nm710 like this

OfficerJohnson
  • OfficerJohnson

    Crackhead

  • Members
  • Joined: 07 Jan 2018
  • United-States

#9

Posted 3 weeks ago

Thanks, Jitnaught. You have been a big help lately!

Thanks to you too, Bob.
  • Jitnaught and nm710 like this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users