Quantcast
Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
    1. Welcome to GTAForums!

    1. GTA Online

      1. The Diamond Casino Heist
      2. Find Lobbies & Players
      3. Guides & Strategies
      4. Vehicles
      5. Content Creator
      6. Help & Support
    2. Red Dead Online

      1. Frontier Pursuits
      2. Find Lobbies & Outlaws
      3. Help & Support
    3. Crews

      1. Events
    1. Red Dead Redemption 2

      1. PC
      2. Gameplay
      3. Missions
      4. Help & Support
    2. Red Dead Redemption

    1. Grand Theft Auto Series

    2. GTA 6

    3. GTA V

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

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
      5. GTA IV 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 SA Mods
    9. GTA Vice City

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

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

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

    3. Mod Showroom

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

      1. DYOM
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    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
    3. Gangs

    1. News

    2. Forum Support

    3. Site Suggestions

Sign in to follow this  
jitsuin

Coding Help

Recommended Posts

jitsuin

Hi, Im trying to write a script that perform some action and i want this to occur only if i am in a certain position and distance from the ped. Like I have to be next to them and face to face with them or maybe i have to be facing their back. Also how can I set this action to occur only if niko is let's say crouching or sprinting?

 

I refered to the sh documentation but many of the entries have no information describing it's functions. I'm trying to find examples in other scripts but other people's scripts are dependent on so many things where mine at first is going to be very simple. Can anyone point me to some resources??

 

Thanks!!

 

jitsuin

Edited by jitsuin

Share this post


Link to post
Share on other sites
Symbiote

Here's how I would do some of those things... If anyone else can share better ways, that would help me out too!

 

Only within a certain distance from the ped:

 

if (Player.Character.Position.DistanceTo(thePed.Position) <= theMaxDistance)

 

Only when the ped can't see you:

 

if (!GTA.Native.Function.Call<bool>("HAS_CHAR_SPOTTED_CHAR_IN_FRONT", thePed, Player.Character))

 

Only when crouching:

 

if (Game.isGameKeyPressed(GameKey.Crouch))

 

Only when sprinting (here I would use the player's speed, because Niko might be sprinting even when the sprint key isn't pressed):

 

float playerSpeed = Math.Abs(Player.Character.Velocity.X) + Math.Abs(Player.Character.Velocity.Y);if (playerSpeed > 6.0F)

 

Edited by AngryAmoeba

Share this post


Link to post
Share on other sites
jitsuin

Thanks for the reply, gonna test these now smile.gif

Share this post


Link to post
Share on other sites
Symbiote

I just realized that checking the crouch key wouldn't work if you have crouching set to "Toggle". That means there's a bug in my Weapon Weight script... confused.gif

Share this post


Link to post
Share on other sites
jitsuin

How do i define "theped"

 

i THINK for theMaxDistance i use

 

 

int theMaxdistance = 0;

 

 

thanks

 

for crouching maybe i can just set up the speed, so it wouldnt necessarily be based on crouching but the speed u walk while crouching, maybe that would help with the weapon weight script??

Edited by jitsuin

Share this post


Link to post
Share on other sites
Symbiote

If you want to get the nearest ped within a certain distance:

 

float maxDistance = 2.0F;Ped thePed = World.GetClosestPed(Player.Character.Position, maxDistance);if (Exists(thePed)) {   // Do stuff with thePed}

 

 

A couple notes:

- Always make sure peds, vehicles, etc. exist before doing stuff with them.

- Distances are floats. If you use an int, it'll automatically be converted to a float when necessary. However, to store a float in your int variable, you'll have to manually convert it (this is called "casting"). Like so:

 

int someDistance = (int)Player.Character.Position.DistanceTo(someVehicle.Position);

 

You'll save a tiny amount of memory by using an int, but it would probably be easier just to use a float. For floats, you have to put an F at the end: 32.87F or 32.87f

Edited by AngryAmoeba

Share this post


Link to post
Share on other sites
jitsuin

awesome, thank you

 

edit:

 

noting all of this, this is helping me understand alot smile.gif

 

going to compile soon and test

Edited by jitsuin

Share this post


Link to post
Share on other sites
jitsuin

alright got evrything working, now i have alot of tweaking to make everything look right, thanks again AngryAmoeba

Share this post


Link to post
Share on other sites
jitsuin

OK the next thing I tried to do but failed is having an increased wanted level of 1 star if a policeman sees me performing some action.

 

I tried to use the following to increase my wanted level if a cop saw me:

 

 

float copDistance = 20.0F;Ped theCop = World.GetClosestPed(Player.Character.Position, copDistance);if (theCop.RelationshipGroup == RelationshipGroup.Cop && !GTA.Native.Function.Call<bool>("HAS_CHAR_SPOTTED_CHAR_IN_FRONT", theCop, Player.Character)){    GTA.Native.Function.Call("ALTER_WANTED_LEVEL", Player, 1);}{    Wait(0);}

 

 

any ideas? i also dont understand how the game measures distance so 20.0f was a guess. i think i also forgot to check if a cop exists on that but not sure if it is needed.

Edited by jitsuin

Share this post


Link to post
Share on other sites
Symbiote

- Unfortunately, on patch 1.0.4.0, RelationshipGroup doesn't work. Instead, use this: theCop.PedType == PedType.Cop

 

- Set the wanted level like this: Player.WantedLevel = 1;

 

- For reference, the width of a two-lane street is about 16.0F.

 

- Yes, you have to check if the ped exists first.

 

- What is the Wait(0) for? (Also, the braces around it aren't associated with anything. Maybe you forgot to put "else"?)

Edited by AngryAmoeba

Share this post


Link to post
Share on other sites
jitsuin

i thought wait(0) was like a placeholder to do nothing. Basically I dont want anything to happen if the IF statement is false.

 

Do i always need an "else" for the false result? I thought just starting the brackets far the false result was enough and i didnt need to write "else"

Edited by jitsuin

Share this post


Link to post
Share on other sites
Symbiote

When the if statement is false, the computer will skip right past the whole block (everything inside the braces). So without an else statement, nothing would happen.

 

If you wanted it to do something when the if statement is false, you would need an else:

 

if (condition){   // Do stuff if condition is true}else{   // Do stuff if condition is false}

 

Edited by AngryAmoeba

Share this post


Link to post
Share on other sites
jitsuin

Gotcha!

 

ok i have this now...

 

 

float copDistance = 20.0F;Ped theCop = World.GetClosestPed(Player.Character.Position, copDistance);if (Exists(theCop) && theCop.PedType == PedType.Cop && !GTA.Native.Function.Call<bool>("HAS_CHAR_SPOTTED_CHAR_IN_FRONT", theCop, Player.Character)){     Player.WantedLevel +=1;}

 

 

so i dont have to write the false result because i dont need any action?

 

edit: just added in exist check

Edited by jitsuin

Share this post


Link to post
Share on other sites
Symbiote

That's right, a false result with no else statement will do nothing.

 

Small tip:

Player.WantedLevel += 1;

can also be written as

Player.WantedLevel++;

 

Similarly, if you wanted to subtract 1, you could use

Player.WantedLevel--;

Share this post


Link to post
Share on other sites
jitsuin

thanks for the ++ and -- tip. Ive only been studying C# a couple days. I have an organized mind so i am surviving...lol.

Share this post


Link to post
Share on other sites
jitsuin

testing it out now, next i am going to want to do a similiar thing if peds witness the crime then ther will be a probability they report it to the police. I already have a good idea of what needs to be done. Pretty much going to do the same thing but leave the PedType part out. I just need to research adding in the probability factor.

Share this post


Link to post
Share on other sites
Symbiote

For probability, you'll need to generate a random number.

 

I got this function from HippieCommunist's Ticket script:

 

int randomNum(int min, int max) {   Random random = new Random();   return random.Next(min, max + 1);}

 

Then you can use the function like this:

 

int percentChance = 20;if (randomNum(1, 100) <= percentChance) {   // Do stuff}

 

Share this post


Link to post
Share on other sites
jitsuin

cool, im already using a random number in another part of the script so i probably use that result, thanks!

Share this post


Link to post
Share on other sites
jitsuin

also i was testing if a cop was going to see me and i dont think it was working, would changing 20.0F to like 100.0F help, maybe 20.0F wasn't enough distance??

Share this post


Link to post
Share on other sites
jitsuin

basically i have it like this and its not working

 

 

if (press a button and ped is near me)      {       play animation;           (insert cop code from post above)      }

 

 

and it is not working even when the animation is playin, so even though i know statement is a true value and i will be in front of a cop sitting in a car (i think this might be an issue) and they pay no attention to me. Do i need to do some kind of IsSittingInVehicle kind of check?

Edited by jitsuin

Share this post


Link to post
Share on other sites
Symbiote

Well, because you're using GetClosestPed(), if anyone is closer to you than the cop, it won't work. You could get an array of all the peds in a range, then loop through them like this:

 

Ped[] nearbyPeds = new Ped[0];try {   nearbyPeds = World.GetPeds(Player.Character.Position, range);}catch (Exception ex) {   // This is for debugging   Game.DisplayText("Error getting nearby peds: " + ex.Message); }foreach (Ped p in nearbyPeds) {   // Use your cop code here}

 

 

 

i will be in front of a cop sitting in a car (i think this might be an issue) and they pay no attention to me. Do i need to do some kind of IsSittingInVehicle kind of check?

No, they can see out of the car fine, within a range of 60.0F I think.

 

If you use HAS_CHAR_SPOTTED_CHAR instead of HAS_CHAR_SPOTTED_CHAR_IN_FRONT, the cop can see really far (not sure how far) and 360º around.

Edited by AngryAmoeba

Share this post


Link to post
Share on other sites
jitsuin

ok going to try this out now

Share this post


Link to post
Share on other sites
jitsuin

thanks for the clarification on the 60.0F, going to use that

 

edit:

 

compiled and testing...

Edited by jitsuin

Share this post


Link to post
Share on other sites
jitsuin

ok this is what i have and i dont think it is working:

 

 

if (push button near ped){         do stuff;         Ped[] nearbyPeds = new Ped[0];         try         {              nearbyPeds = World.GetPeds(Player.Character.Position, 60.0F);         }         catch (Exception ex)         {              Game.DisplayText("Error getting nearby peds: " + ex.Message);         }         foreach (Ped p in nearbyPeds)         {              float copDistance = 100.0F;              Ped theCop = World.GetClosestPed(Player.Character.Position, copDistance);               if (Exists(theCop) && theCop.PedType == PedType.Cop && !GTA.Native.Function.Call<bool>("HAS_CHAR_SPOTTED_CHAR", theCop, Player.Character))              {                    Player.WantedLevel++;              }         }}

 

Edited by jitsuin

Share this post


Link to post
Share on other sites
jitsuin

i think i fixed it, gonna double check

Share this post


Link to post
Share on other sites
jitsuin

yup got it fixed, ill post the code if anyone wants to know

Share this post


Link to post
Share on other sites
jitsuin

ok i have the script working really nice now, i have a bug though when im trying to add a blip to a pedestrian and im trying to make the pedestrian im marking hostile or scared from you. This is the code im using and it doesn't seem to be changing the peds relationship and sometimes it puts the blip on niko and not the ped.

 

 

Ped[] nearbyPeds = World.GetPeds(Player.Character.Position, 2.5F);foreach (Ped p in nearbyPeds){    if (Exists(p) && GTA.Native.Function.Call<bool>("HAS_CHAR_SPOTTED_CHAR_IN_FRONT", p, Player.Character) && p.PedType == PedType.CivMale || p.PedType == PedType.CivFemale || p.PedType == PedType.Fireman || p.PedType == PedType.Paramedic || p.PedType != PedType.Cop || p.PedType != PedType.Criminal)      {            int chance = 100; //set to 100 for testing            if (randomRatChance(0, 100) <= chance)            {                    Game.DisplayText("You were reported to the police...kill the rat! >:D");                    p.ChangeRelationship(RelationshipGroup.Civillian_Male, Relationship.Hate);                     Wait(3000)                     Blip.AddBlip(p);                     Wait(5000);                     Player5WantedLevel++;              }        }}

 

 

oh and i was only trying to test relationship change with civ males, i need to include the other peds still, and sometime if i spam the button that triggers this code my wanted level will go up 5 stars, how do i limit to only allow that method run once?? do i need to add a wait or interval somewhere?

 

i tried to stop the blip from being assigned to niko by using this in the code:

 

 

if (Exists(p) && p != Player.Character && ...

 

 

blip still getting added to niko though, i must be using a method wrong

 

edit: just tested again and it does change relationship of ped but it just makes him hostile towards other peds, how can i mae him scared of niko?

Edited by jitsuin

Share this post


Link to post
Share on other sites
Symbiote

The biggest problems that I can see are in your if statement. First, you need to parenthesize your conditions. For example:

 

if (Player.WantedLevel > 0 && (Player.Character.isRagdoll || Player.Character.Health < 100)){   // This is true if...   // Player is wanted AND ragdoll   // OR   // Player is wanted AND injured}

 

Doing this for your conditions will make your "p != Player.Character" condition work properly.

 

Also, if you evaluate the peds with something like

condition1 || condition2 || condition3 || p.PedType != PedType.Criminal

then the whole thing will be true for any ped who isn't a criminal.

 

Next, you say the player's wanted level should only be 1, but the statement Player.WantedLevel++ adds 1 star every time it executes. If you never want to give more than 1 star, you should do this instead:

 

if (Player.WantedLevel == 0){  Player.WantedLevel = 1;}

 

This way, it'll only go up to 1 star, and if the player already has 1 or more stars, it won't do anything.

 

EDIT: For making peds fight or flee from the player, look at these methods in the script hook documentation: Ped.Task.FightAgainst() and Ped.Task.FleeFromChar()

Edited by AngryAmoeba

Share this post


Link to post
Share on other sites
jitsuin

studying all this now. Ok i see exactly what you are saying about the parenthesis and what you said about adding != Criminal at the end makes sense now that u pointed it out. Must have been a brain fart. I am starting to get use to C#, the only really knowledge i am relying on are my microsoft excel skills, lol, i work on that alot and basically pulling the fundamentals from that. i appreciate you helping to connect the two.

 

this will solve all my problems smile.gif

 

edit: made all the necessary changes, time to test

Edited by jitsuin

Share this post


Link to post
Share on other sites
jitsuin

Wow! it is working beautifully now that i used correct parenthesis! lol, facepalm for me wink.gif

 

this is for you AngryAmoeba

cookie.gifcookie.gifcookie.gifcookie.gifcookie.gifcookie.giflol.gif

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

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