Jump to content
download500

How exactly do I make a plugin, similar to a callout's base.process to where I can press a button at any time to perform an action?

Recommended Posts

You know how if you put something in base.process on a callout it will check every tick to see if a button is down, and if it is, runs an action?

 

What do I need to do or edit so that I can edit the base.process of Rage, that is, make a button (or random event) that is checked for every tick?

 

I wanted to make a custom backup script and this would be useful. If someone has a clean template (similar to the API callout example) that I could just paste my script into that would be AWESOME!

Share this post


Link to post
Share on other sites

Could you specify what you're actually trying to do in terms of the backup? You seem to be a little off the mark with ticks and base.process because thats not needed for this.

Do you have experience with C#?

Would it be helpful if I send you a basic project with comments on where to complete your logic?

Share this post


Link to post
Share on other sites

I have experience in that I've successfully made a normal callout with the ability to press a button at any time and an event occurs.

 

Basically I want to spawn a cop and a cop car 3000f away from my active location, and have him drive to me, and then get out once he arrives and hold position.

 

I know how to script this as I have done it in my callout, but the problem is this type of backup (a permanent cop acting as a sentry) is only available when my callout occurs. I would like to always be available.

 

so yes if you have an example project that's a template for a plugin, where I could paste in code that would be activated any time the button "Y" (or whatever button I choose) is pressed, that would be awesome

Share this post


Link to post
Share on other sites

Make sure to extract the files before opening the project or you'll get an error.

Open the project, then open Main.cs, you'll see my comment at the bottom as to where you can add your code to achieve what you want. I've set up the while loop and delegation so that you don't need to worry about that. Just literally add in your code for the backup and it should work.

APIExample.zip

Edited by AlconH

Share this post


Link to post
Share on other sites

Awesome thanks I will check it out.

 

Can I make as many instances of 

 

  

                        if (Game.IsKeyDown(Keys.F1)) //Change F1 to whatever key you want....
                        {
                            //Code to be executed upon keypress goes here......
                        }
                        GameFiber.Yield();

 

within Functions_OnOnDutyStateChanged(bool onDuty)  as I want, i.e. one for each hotkey I make? Or should I be putting them in separate .cs sheets?

Share this post


Link to post
Share on other sites

You can insert as many as you want to, but for code organization, its better to put them all in a separate function, to keep clutter out of OnDutyStageChanged.

 

void Functions_OnOnDutyStateChanged(bool OnDuty)
{
    MonitorKeybinds();
    //other code can go above/below this line...this way, Functions_OnOnDutyStateChanged is finished with what it's doing, and your code is much cleaner
}

void MonitorKeybinds()
{
    GameFiber.StartNew(() =>
    {
        while (true)
        {
            if (Game.IsKeyDown(Keys.F1))
            {
                //code here
            }
            
            GameFiber.Yield();
        }
    });
}

 

Share this post


Link to post
Share on other sites

Where do I declare my variables like private vehicle v1;

 

? I tried doing it under main, and my script says the variable is not declared.

 

I ended up putting it just under the game.iskeydown as:

 

ped cop1;

vehicle v1;

 

will see if that works

Edited by download500

Share this post


Link to post
Share on other sites
47 minutes ago, download500 said:

Awesome thanks I will check it out.

 

Can I make as many instances of 

 

  


                        if (Game.IsKeyDown(Keys.F1)) //Change F1 to whatever key you want....
                        {
                            //Code to be executed upon keypress goes here......
                        }
                        GameFiber.Yield();

 

within Functions_OnOnDutyStateChanged(bool onDuty)  as I want, i.e. one for each hotkey I make? Or should I be putting them in separate .cs sheets?

You can put as many as you like into the code, it will just spawn x many backup requests.

20 minutes ago, download500 said:

Where do I declare my variables like private vehicle v1;

 

? I tried doing it under main, and my script says the variable is not declared.

 

I ended up putting it just under the game.iskeydown as:

 

ped cop1;

vehicle v1;

 

will see if that works

You put it under:

public class Main : Plugin
    {

Share this post


Link to post
Share on other sites
You put it under:

public class Main : Plugin
    {

I'm getting "An object reference is required for the non-static field" when I put it there.

 

I have it like this, is it incorrect?

 

    public class Main : Plugin
    {
        /// <summary>
        /// Constructor for the main class, same as the class, do not rename.
        /// </summary>
        /// variables below?
        /// 

        private Ped cop1;
       private Vehicle v1;
      private  Blip b1;
      private  Random rand1 = new Random();
     private   int r;
    private    int v;
    private    int unit = 1;

 

Share this post


Link to post
Share on other sites

Got it thanks. Also, how do I have it determine whether I am in the city or county?

 

For example if I am in the city I want it to spawn one car, but the county another, etc.

Share this post


Link to post
Share on other sites
7 minutes ago, download500 said:

Got it thanks. Also, how do I have it determine whether I am in the city or county?

 

For example if I am in the city I want it to spawn one car, but the county another, etc.

if (Rage.Native.NativeFunction.CallByName<uint>("GET_HASH_OF_MAP_AREA_AT_COORDS", Game.LocalPlayer.Character.Position.X, Game.LocalPlayer.Character.Position.Y, Game.LocalPlayer.Character.Position.Z) == Game.GetHashKey("city"))
{
    //player is in city
}
else
{
    //player is in the country side
}

There's a code snippet...you can also use the WorldZone functions in the LSPDFR API.

Edited by Stealth22

Share this post


Link to post
Share on other sites

Thanks. Suppose I spawn multiple instances of this, using the same ped "cop1"

 

I could end up with 5 cops on the scene that were call called "cop1" because I pressed the button 5 times.

 

How do I dismiss ALL of them, even if they were a previous instance?

 

would 

if (cop1.Exists()) cop1.Delete(); work? or no, because the previous associatons lost their attachment to the name cop1?

And if that is not possible, how would I delete the nearest vehicle to me?

Edited by download500

Share this post


Link to post
Share on other sites

You would have to maintain a list of cop peds (or ped handles would work, for memory management) that you spawn. An array, or preferably a Generic List, would work. 

What exactly are you trying to do? Rebuild the Code 2 backup function, without having the cops driving away? 

Share this post


Link to post
Share on other sites
54 minutes ago, Stealth22 said:

You would have to maintain a list of cop peds (or ped handles would work, for memory management) that you spawn. An array, or preferably a Generic List, would work. 

What exactly are you trying to do? Rebuild the Code 2 backup function, without having the cops driving away? 

 

It's actually pretty complex, I have completed it to do everything except dismiss properly. All working so far.

 

I have:

 

Code 2 travel with code 3 silent once they get within 50 feet of the stop point. Far more realistic as in real life, cops typically travel without lights and siren (but at an aggressive pace) and then flip the lights on silently as they approach their destination.

Code 3 travel, with sirens going silent with 100f of destination

Code 2 stealth (WIP) Cops will go silent sirens until about 100f out, and then cut emergency lights AND headlights off (if possible, havent figured this out yet) to do a stealth roll up, great for night time.

 

You can currently dispatch local units (county or city) in any of those methods (except the stealth)

You can also dispatch CHP or a detective (all with applicable uniformed peds) as code 2 with code 3 arrival within 50f.

 

As of now, you just stand where you want them and it saves that position for their arrival.

All units spawn from a random distance away, anywhere from 500f to 3000f. They have no blip, though they do call out over the radio when they depart and when they arrive.

They currently just open their car door and stand behind it. I mainly made this as when roads are blocked off in real life, cops will typically post up in the roadblock position and direct traffic, which is a lot more realistic than simply stopping traffic.

Also they can have their task be switched from just standing to "aim gun at" if you want to convert them to a felony traffic stop.

 

If I can figure out how to dismiss them all and turn off their headlights I will be golden.

 

 

You would have to maintain a list of cop peds (or ped handles would work, for memory management) that you spawn. An array, or preferably a Generic List, would work. 

I'm way more familiar in VB NET (my only experience with C# is what I have done for LSPDFR), but I can figure it out using a code converter.

I am assuming I'd generate a new ped object for each dispatch, and then have it add the newly created object to a list of peds, to where during the delete sequence I could just do a "for each ped in list, if exists delete" ? 

Edited by download500

Share this post


Link to post
Share on other sites
18 hours ago, download500 said:

I'm way more familiar in VB NET (my only experience with C# is what I have done for LSPDFR), but I can figure it out using a code converter.

I am assuming I'd generate a new ped object for each dispatch, and then have it add the newly created object to a list of peds, to where during the delete sequence I could just do a "for each ped in list, if exists delete" ? 

Yup. Either a list (System.Collections.Generic.List) of Peds, or a list of Handles (myPed.Handle).

If you call Ped.Delete, it will just poof them into thin air. You'll probably want to call Dismiss instead, or make them get back into the car and drive away.

Share this post


Link to post
Share on other sites
3 hours ago, Stealth22 said:

Yup. Either a list (System.Collections.Generic.List) of Peds, or a list of Handles (myPed.Handle).

If you call Ped.Delete, it will just poof them into thin air. You'll probably want to call Dismiss instead, or make them get back into the car and drive away.

Thanks, hopefully I can tackle that this weekend.

How would I go about setting a radius around the cop once he arrives to where traffic cannot pass? Basically I have noticed when I post him up in a road, cars still drive around him.

 

How can I set a radius around him that will be impassable to all but emergency vehicles?

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

×