Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!   (84,864 visits to this link)

    2. News

    1. GTA Online

      1. Find Lobbies & Players
      2. Guides & Strategies
      3. Vehicles
      4. Content Creator
      5. Help & Support
    2. Crews

      1. Events
      2. Recruitment
    1. Grand Theft Auto Series

    2. GTA Next

    3. GTA V

      1. PC
      2. Guides & Strategies
      3. Help & Support
    4. GTA IV

      1. Episodes from Liberty City
      2. Multiplayer
      3. Guides & Strategies
      4. Help & Support
      5. GTA Mods
    5. GTA Chinatown Wars

    6. GTA Vice City Stories

    7. GTA Liberty City Stories

    8. GTA San Andreas

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    9. GTA Vice City

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    10. GTA III

      1. Guides & Strategies
      2. Help & Support
      3. GTA Mods
    11. Top Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    12. Wiki

      1. Merchandising
    1. GTA Modding

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    3. Featured Mods

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Red Dead Redemption 2

    2. Red Dead Redemption

    3. Rockstar Games

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Programming
      5. Movies & TV
      6. Music
      7. Sports
      8. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Forum Support

    2. Site Suggestions

OfficerJohnson

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

Recommended Posts

OfficerJohnson

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?

Share this post


Link to post
Share on other sites
Jitnaught

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 -=

Edited by Jitnaught

Share this post


Link to post
Share on other sites
Bob_74

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)?

Share this post


Link to post
Share on other sites
OfficerJohnson

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.

Share this post


Link to post
Share on other sites
Jitnaught

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.

Share this post


Link to post
Share on other sites
Bob_74

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!

Share this post


Link to post
Share on other sites
Jitnaught

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

Share this post


Link to post
Share on other sites
Bob_74

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!

Edited by Bob_74

Share this post


Link to post
Share on other sites
OfficerJohnson

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

 

Thanks to you too, Bob.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.