News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Help with PedIsInAreaXYZ  (Read 6504 times)

0 Members and 2 Guests are viewing this topic.

Offline HairyButtNugget

  • Jr. Member
  • **
  • Posts: 5
  • Learning to mod Bully :)
    • View Profile
Help with PedIsInAreaXYZ
« on: August 20, 2016, 06:07:54 AM »
Hi there. I am very new to modding (only started yesterday) but i have some simple coding experience in python. I want to learn how to mod bully. I learned the basics of how to spawn peds and setting player health etc and I am trying to learn how to make a mission but i cant get the "PedIsInAreaXYZ" command to work properly. I have been changing and adapting my code for many hours but cannot get it to work.

Here is the code at the moment:

Code: [Select]
MissionSetup = function()
PlayerSetHealth(200)
AreaTransitionXYZ(0,270,-110,6.000000953674)
Gary = PedCreateXYZ(130,271,-73,5.992765495)
end

MissionCleanup = function()
end

function main()
repeat
Mission1()
wait(0)
until not Alive
end

Mission1 = function()
if PedIsInAreaXYZ(gPlayer,271,-73,5.992765495,10) then
TextPrintString("Success", 5, 1)
else main()
end
end

I tried to make the code constantly check if the player is in range of Gary by making the main function keep running the "Mission1" function.

I want the code to work like this:

1. Gary spawns at co-ordinates.
2. Player walks over to Gary.
3. Text "Success" is shown.

If anyone can give me some help i would really appreciate it :).

P.S. Sorry if the problem is extremely simple. I'm very new and couldn't find any help anywhere on the internet.

Offline Rambo7

  • "Awesomely Kick-Ass Troll"
  • Full Member
  • ***
  • Posts: 404
  • Gender: Male
  • #ZAMBess
    • View Profile
    • My ModDB Page
Re: Help with PedIsInAreaXYZ
« Reply #1 on: August 21, 2016, 01:13:38 AM »
Code: [Select]
MissionSetup = function()
PlayerSetHealth(200)
AreaTransitionXYZ(0,270, -110, 7)
Gary = PedCreateXYZ(130,275, -110, 7)
end

MissionCleanup = function()
end

function main()
Mission1()

repeat

Wait(0)
until not PedIsValid(gPlayer) or PedIsDead(gPlayer)
end


Mission1 = function()
repeat
Wait(0)
until PedIsInAreaXYZ(gPlayer, 275, -110, 7, 1, 1)
TextPrintString("Gary: Marry Me, Jimmy!!!", 3, 2)
PedAttackPlayer(Gary,3)
PedSetPedToTypeAttitude(Gary,13,0)
end

Your mistakes:
  • You used wait(0) (lowercase w), the correct one is Wait(0) (uppercase W)
    Lua is extremely sensitive with lowercase & uppercase
  • You missed some part of this PedIsInAreaXYZ code
    PedIsInAreaXYZ(ped, X, Y, Z, Radius, Blip)
    eg. PedIsInAreaXYZ(gPlayer, 275, -110, 7, 1, 1)
« Last Edit: August 21, 2016, 02:01:06 AM by Rambo7 »

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Help with PedIsInAreaXYZ
« Reply #2 on: August 21, 2016, 08:21:50 AM »
Here's a new script that'll do what you're looking for with comments explaining it. (The lines starting with -- are comments and are ignored when the script runs).

Code: [Select]
-- Setup/cleanup:
function MissionSetup()
  -- Set the player's position outside:
  PlayerSetPosXYZArea(270,-110,6,0) -- args for this are similar to AreaTransitionXYZ, but it instead takes the area code (0 for outside) last.
 
  -- Create Gary:
  -- While it is entirely up to you how you name variables, we name this one starting with a "g" to indicate it is not a local variable (but is global instead).
  gGary = PedCreateXYZ(130,271,-73,5.9)
 
  -- Wait for the area to load before continuing (we switched to a new area using PlayerSetPosXYZArea).
  while AreaIsLoading() do
    Wait(0)
  end
end
function MissionCleanup()
 
end

-- Main:
function main()
  -- Get Gary's current position and store them in x, y, and z. These variables are also created as local variables, so they can't be used in another function.
  local x,y,z = PedGetPosXYZ(gGary)
 
  -- Wait for the player to be near Gary
  while not PedIsInAreaXYZ(gPlayer,x,y,z,10) do -- call "PedIsInAreaXYZ", check if it returns false, and if so run the lines between "do" and "end"
    Wait(0) -- suspend the script for one frame (0 ms) so other scripts can run and so the game can run (the game is completely frozen until your script waits)
    x,y,z = PedGetPosXYZ(gGary) -- Update the value of x, y, and z incase Gary moved somehow.
  end
 
  -- Show "Success!" after the while loop above has completed, note that the while loop will continue to run until "PedIsInAreaXYZ" returns true.
  TextPrintString("Success!",3,1) -- show the text for 3 seconds
  Wait(3000) -- wait 3 seconds (3000 ms) for the text to go away
end

It's also important to know how the script will run. First, the script will be read from start to finish running any code it finds that isn't contained in a function. If it reads code that is in a function, that function will simply be put into memory so it can be called later. For Bully, it is sort of convention that you don't really call anything outside of any functions, but it's totally up to you how you want to do your scripts. You do at least need a few functions though, as after the script gets read from start to finish normally, Bully will call a few functions from your script. For mission scripts (such as ArcRace1.lur, what I assume you're using here), MissionSetup will be called first, then main after that finishes, and MissionCleanup when the mission ends.

Offline HairyButtNugget

  • Jr. Member
  • **
  • Posts: 5
  • Learning to mod Bully :)
    • View Profile
Re: Help with PedIsInAreaXYZ
« Reply #3 on: August 21, 2016, 10:14:07 AM »
Here's a new script that'll do what you're looking for with comments explaining it. (The lines starting with -- are comments and are ignored when the script runs).

Code: [Select]
-- Setup/cleanup:
function MissionSetup()
  -- Set the player's position outside:
  PlayerSetPosXYZArea(270,-110,6,0) -- args for this are similar to AreaTransitionXYZ, but it instead takes the area code (0 for outside) last.
 
  -- Create Gary:
  -- While it is entirely up to you how you name variables, we name this one starting with a "g" to indicate it is not a local variable (but is global instead).
  gGary = PedCreateXYZ(130,271,-73,5.9)
 
  -- Wait for the area to load before continuing (we switched to a new area using PlayerSetPosXYZArea).
  while AreaIsLoading() do
    Wait(0)
  end
end
function MissionCleanup()
 
end

-- Main:
function main()
  -- Get Gary's current position and store them in x, y, and z. These variables are also created as local variables, so they can't be used in another function.
  local x,y,z = PedGetPosXYZ(gGary)
 
  -- Wait for the player to be near Gary
  while not PedIsInAreaXYZ(gPlayer,x,y,z,10) do -- call "PedIsInAreaXYZ", check if it returns false, and if so run the lines between "do" and "end"
    Wait(0) -- suspend the script for one frame (0 ms) so other scripts can run and so the game can run (the game is completely frozen until your script waits)
    x,y,z = PedGetPosXYZ(gGary) -- Update the value of x, y, and z incase Gary moved somehow.
  end
 
  -- Show "Success!" after the while loop above has completed, note that the while loop will continue to run until "PedIsInAreaXYZ" returns true.
  TextPrintString("Success!",3,1) -- show the text for 3 seconds
  Wait(3000) -- wait 3 seconds (3000 ms) for the text to go away
end

It's also important to know how the script will run. First, the script will be read from start to finish running any code it finds that isn't contained in a function. If it reads code that is in a function, that function will simply be put into memory so it can be called later. For Bully, it is sort of convention that you don't really call anything outside of any functions, but it's totally up to you how you want to do your scripts. You do at least need a few functions though, as after the script gets read from start to finish normally, Bully will call a few functions from your script. For mission scripts (such as ArcRace1.lur, what I assume you're using here), MissionSetup will be called first, then main after that finishes, and MissionCleanup when the mission ends.

Thank you so much. I understand a lot better how everything works now. I was struggling with using while loops before because i kept getting mixed up with the 'end's so i couldn't end the function properly. So i tried creating a way around it with the code i made before.

Thanks for the help!

Offline HairyButtNugget

  • Jr. Member
  • **
  • Posts: 5
  • Learning to mod Bully :)
    • View Profile
Re: Help with PedIsInAreaXYZ
« Reply #4 on: August 21, 2016, 10:16:13 AM »
Code: [Select]
MissionSetup = function()
PlayerSetHealth(200)
AreaTransitionXYZ(0,270, -110, 7)
Gary = PedCreateXYZ(130,275, -110, 7)
end

MissionCleanup = function()
end

function main()
Mission1()

repeat

Wait(0)
until not PedIsValid(gPlayer) or PedIsDead(gPlayer)
end


Mission1 = function()
repeat
Wait(0)
until PedIsInAreaXYZ(gPlayer, 275, -110, 7, 1, 1)
TextPrintString("Gary: Marry Me, Jimmy!!!", 3, 2)
PedAttackPlayer(Gary,3)
PedSetPedToTypeAttitude(Gary,13,0)
end

Your mistakes:
  • You used wait(0) (lowercase w), the correct one is Wait(0) (uppercase W)
    Lua is extremely sensitive with lowercase & uppercase
  • You missed some part of this PedIsInAreaXYZ code
    PedIsInAreaXYZ(ped, X, Y, Z, Radius, Blip)
    eg. PedIsInAreaXYZ(gPlayer, 275, -110, 7, 1, 1)

Thanks for the help. Do you mind explaining what the 'Blip' part means/does? I'm new to all of this :)

Offline Rambo7

  • "Awesomely Kick-Ass Troll"
  • Full Member
  • ***
  • Posts: 404
  • Gender: Male
  • #ZAMBess
    • View Profile
    • My ModDB Page
Re: Help with PedIsInAreaXYZ
« Reply #5 on: August 21, 2016, 10:49:33 AM »
'Blip' part explanation:
0 = no blip
1 = blip (yellow)
2 = arrow (blue)
and so on
You can try it yourself, just change the blip number
BTW this PedIsInAreaXYZ(ped, X, Y, Z, Radius, Blip) code, work pretty unique, if you reach the designated area, the blip will be automatically deleted
so you don't need an extra code to delete the blip

But, if you want to create your own blip & delete it, you can use this code
BlipAddXYZ & BlipRemove
for more information read this thread:
http://bully-board.com/index.php?topic=22886.0

Offline HairyButtNugget

  • Jr. Member
  • **
  • Posts: 5
  • Learning to mod Bully :)
    • View Profile
Re: Help with PedIsInAreaXYZ
« Reply #6 on: August 21, 2016, 11:35:26 AM »
'Blip' part explanation:
0 = no blip
1 = blip (yellow)
2 = arrow (blue)
and so on
You can try it yourself, just change the blip number
BTW this PedIsInAreaXYZ(ped, X, Y, Z, Radius, Blip) code, work pretty unique, if you reach the designated area, the blip will be automatically deleted
so you don't need an extra code to delete the blip

But, if you want to create your own blip & delete it, you can use this code
BlipAddXYZ & BlipRemove
for more information read this thread:
http://bully-board.com/index.php?topic=22886.0

Oh ok. Thank you. I was just looking up how to add maps blips.

Cheers