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


Author Topic: Having trouble getting PedOnDeath triggers to work  (Read 6417 times)

0 Members and 1 Guest are viewing this topic.

Offline Binary101010

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Having trouble getting PedOnDeath triggers to work
« on: March 04, 2016, 10:56:23 AM »
Trying to put together some basic missions for a research study I'm helping with. The current intent is to simply spawn two peds, have one start attacking the other, and have the player intervene to protect the one being bullied. I'd also like a text message to come up when the bully is beaten up.

I've gotten everything working except that last part. When the player intervenes and beats up the bully... nothing happens. No text or anything. What am I missing?

Code: [Select]
ImportScript("\\Library\\LibTable.lua") -- imports the LibTable library
ImportScript("\\Library\\LibPed.lua") -- imports the LibPed library
local l_0_0 = false
 
MissionSetup = function() -- basic mission setup function which is used in almost all bully scripts
  local l_1_0 = 270 -- X coords
  local l_1_1 = -110 -- Y coords
  local l_1_2 = 6.4000000953674 -- Z coords
  local Algernon = Nil -- Initialize bullied char
  local Damon = Nil -- Initialize bully
 
  DisablePunishmentSystem(true) -- Necessary to keep the prefects from interfering
  PlayerSetHealth(200) -- gives the player 200 health
  AreaTransitionXYZ(0, l_1_0, l_1_1, l_1_2) -- Moves player to outside boys' dorm
  TextPrintString("Algernon is one of the geeks at school. He needs protection from Damon, the jock who has been bullying him.", 4, 1) -- Posts red text on screen
  Algernon = PedCreateXYZ(4,265,-110,6.4000000953674) -- Spawn bully target
  Damon = PedCreateXYZ(12,262,-110,6.4000000953674) -- Spawn bully
  PedRecruitAlly(Algernon,gplayer) -- Second character assigned to bodyguard first
  PedAttack(Damon, Algernon, 1) -- Bully attacks target
  PedShowHealthBar(Algernon, true, "N_Algernon", true) -- Display Algernon's health bar
end

MissionCleanup = function()
end -- end statement
 
 F_MissionCheck = function()
 if PedIsDead(Damon) then
  TextPrintString("Congratulations! Algernon is safe for now thanks to your intervention.") -- This is where I need help, this text never appears
  end
  end
 
main = function() -- Main mission function
  repeat
  F_MissionCheck()
  Wait(2000)
  until not Alive
  end

Offline Bellic19

  • Sr. Member
  • ***
  • Posts: 933
  • Gender: Male
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #1 on: March 04, 2016, 11:08:31 AM »
You haven't included a font size and shit. Try this instead

Code: [Select]
ImportScript("\\Library\\LibTable.lua") -- imports the LibTable library
ImportScript("\\Library\\LibPed.lua") -- imports the LibPed library
local l_0_0 = false
 
MissionSetup = function() -- basic mission setup function which is used in almost all bully scripts
  local l_1_0 = 270 -- X coords
  local l_1_1 = -110 -- Y coords
  local l_1_2 = 6.4000000953674 -- Z coords
  local Algernon = Nil -- Initialize bullied char
  local Damon = Nil -- Initialize bully
 
  DisablePunishmentSystem(true) -- Necessary to keep the prefects from interfering
  PlayerSetHealth(200) -- gives the player 200 health
  AreaTransitionXYZ(0, l_1_0, l_1_1, l_1_2) -- Moves player to outside boys' dorm
  TextPrintString("Algernon is one of the geeks at school. He needs protection from Damon, the jock who has been bullying him.", 4, 1) -- Posts red text on screen
  Algernon = PedCreateXYZ(4,265,-110,6.4000000953674) -- Spawn bully target
  Damon = PedCreateXYZ(12,262,-110,6.4000000953674) -- Spawn bully
  PedRecruitAlly(Algernon,gplayer) -- Second character assigned to bodyguard first
  PedAttack(Damon, Algernon, 1) -- Bully attacks target
  PedShowHealthBar(Algernon, true, "N_Algernon", true) -- Display Algernon's health bar
end

MissionCleanup = function()
end -- end statement
 
 F_MissionCheck = function()
 if PedIsDead(Damon) then
  TextPrintString("Congratulations! Algernon is safe for now thanks to your intervention.", 4, 1)
   Wait(5000)
  end
  end
 
main = function() -- Main mission function
  repeat
  F_MissionCheck()
  Wait(2000)
  until not Alive
  end

That should work. Welcome to the board BTW!
« Last Edit: March 04, 2016, 11:13:35 AM by gembo555 »

Offline Binary101010

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #2 on: March 04, 2016, 12:26:53 PM »
Well, the lack of the font info was definitely a problem. Can't believe I missed that as I got it right on the text at the beginning of the mission. And nobody else working on this project with me has even as much of a clue about LUA as I do (which isn't much).

However, it looks like there's a different problem now, as the "Congratulations" test is appearing immediately on the beginning of the mission. So I have to figure there's something about the PedIsDead trigger that's not working right. Any ideas?

Offline Bellic19

  • Sr. Member
  • ***
  • Posts: 933
  • Gender: Male
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #3 on: March 04, 2016, 12:51:59 PM »
alittle more info please. is it appearing then disappearing or is it constant?

Offline Binary101010

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #4 on: March 04, 2016, 01:15:05 PM »
Looks like it stays on screen for somewhere between 3-4 seconds, disappears for 3 seconds, comes back for another 3-4 seconds, disappears for 3 seconds, and so on.

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #5 on: March 04, 2016, 04:33:24 PM »
Patched up your script and added several comments either starting with 'EDIT' or 'NOTE'.

Code: [Select]
-- EDIT: Removed imported scripts and declaration of l_0_0, there is no need to import those scripts and l_0_0 is never used

MissionSetup = function() -- basic mission setup function which is used in almost all bully scripts
  -- EDIT: Renamed variables to 'X', 'Y', and 'Z' as these are more descriptive names
  local X = 270 -- X coords
  local Y = -110 -- Y coords
  -- NOTE: There is no need to make your coordinates this specific, but there is nothing wrong with it either. I suggest only using 1 or 2 decimal places for simplicity.
  local Z = 6.4000000953674 -- Z coords
 
  -- EDIT: Made 'Nil' lowercase, LUA is a case sensitive language and nil should be 'nil'.
  local Algernon = nil -- Initialize bullied char
  -- EDIT: Made 'Damon' a local variable.
  Damon = nil -- Initialize bully
 
  DisablePunishmentSystem(true) -- Necessary to keep the prefects from interfering
  PlayerSetHealth(200) -- gives the player 200 health
  AreaTransitionXYZ(0, X, Y, Z) -- Moves player to outside boys' dorm
  TextPrintString("Algernon is one of the geeks at school. He needs protection from Damon, the jock who has been bullying him.", 4, 1) -- Posts red text on screen
  Algernon = PedCreateXYZ(4,265,-110,6.4000000953674) -- Spawn bully target
  Damon = PedCreateXYZ(12,262,-110,6.4000000953674) -- Spawn bully
  -- EDIT: 'gplayer' to 'gPlayer'.
  PedRecruitAlly(Algernon,gPlayer) -- Second character assigned to bodyguard first
  -- EDIT: Lowered Damon's attitude towards the player faction (13) if it was high so that Damon will be able to fight you if you intervene.
  if PedGetPedToTypeAttitude(Damon,13) > 2 then
    PedSetPedToTypeAttitude(Damon,13,2)
  end
  PedAttack(Damon, Algernon, 1) -- Bully attacks target
  -- NOTE: The function 'PedGetName(ped)' can also be used to get the ped's name, but it is ultimatly up to you.
  PedShowHealthBar(Algernon, true, "N_Algernon", true) -- Display Algernon's health bar
end

MissionCleanup = function()
 
end -- end statement

-- EDIT: Removed F_MissionCheck as the code fits better in the main function

main = function() -- Main mission function
  repeat
    -- EDIT: Adjusted the call to 'Wait' so that the script only gets suspended for 1 frame (0 milliseconds) each time the repeat loop runs.
    Wait(0)
  until PedIsDead(Damon) -- NOTE: This means the repeat loop will keep running until 'PedIsDead' returns true (when Damon is KO'd)
  -- EDIT: Added time and style. 'TextPrintString' takes 3 arguments: textString, time (seconds), and style (1 for top of the screen and 2 for the bottom)
  TextPrintString("Congratulations! Algernon is safe for now thanks to your intervention.",2,1)
  -- NOTE: Added a call to 'Wait' so the script is suspended for 2000 milliseconds (2 seconds) to give the script time to print the text before the script is terminated
  Wait(2000)
  -- EDIT: Added call to 'MissionSucceed'
  MissionSucceed()
end

-- EDIT: Fixed the indendting through-out the whole script.

Notice that I say gplayer was wrong and that you should use gPlayer. You may be wondering "but it worked fine with gplayer, so why is it wrong?" and there is a reason for that. In your original script, gplayer was never created, so when you used it, it's value was nil, meaning it didn't have a value. In many of Bully's functions that expect a number as an argument, nil gets defaulted to 0, which just so happens to be the player's ped. So really both ways work, but gPlayer is the right way to do it, since gPlayer is a global variable (global to all scripts) referring to the player. Oh and if it confused you that the player is 0, peds in LUA are simply numbers referring to them, it's called a ped handle. If you want me to explain more about this I can but I won't ramble about it too long now.

Welcome to the board by the way, enjoy your stay. It's nice to see some more interest in modding these days.
« Last Edit: March 09, 2016, 03:28:58 PM by DaBOSS54320 »

Offline AlphaTech

  • LostInSpace
  • Sr. Member
  • ***
  • Posts: 758
  • Gender: Male
  • The name's AlphaTECH, whatca need help with?! :)
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #6 on: March 04, 2016, 11:31:02 PM »
You really should check my mission scripting tutorial.
So basically you do this,
 
To tell you a few things make sure to follow my scripting tutorial entirely. The argument with if is always used to ask the game wether something is valid or not.
 
Anything can be used for checking

Code: [Select]
if DuckisGoose = false  and DuckisFound = false and PedIsValid(Goose) then
--Duck Found
end

With false the argument is always set to true if your trying to run something once. Running false multiple times like this

Code: [Select]
function CallPreppiesToHelp()
PreppiesHelp = false
end

function CallPreppiesToHelp()
PreppiesHelp = false
PedAddPedToIgnoreList(Preppy1)
end

function CallPreppiesToHelp2()
PreppiesHelp = true
PedAttack(Preppy1)
PreppiesHelp = false
end





Code: [Select]
function MissionSetup()
AreaTransitionXYZ(0, 270, -110, 6.4000000953674)
  DisablePunishmentSystem(true)
  AreaDisableAllPatrolPaths()
  DisablePunishmentSystem(false)
Algernon = PedCreateXYZ(4,265,-110,6.4000000953674) -- Spawn bully target
  Damon = PedCreateXYZ(12,262,-110,6.4000000953674) --Spawn bully
SpawnCallToBully()
end

function SpawnCallToBully()
  local x,y = 4,256
  local r1 = x + 1.5
  local r2 = y + 1.5
  local r3 = x - 1.5
  local r4 = y - 1.5
  repeat
    Wait(0)
  until PedInRectangle(gPlayer,r1,r2,r3,r4)
  Wait(1500)
 TextPrintString("Algernon is one of the geeks at school. He needs protection from Damon, the jock who has been bullying him.", 4, 1)
Wait(4000)
PedRecruitAlly(Algernon,gPlayer)
 PedShowHealthBar(Algernon, true, "N_Algernon", true)
PedAttack(Damon, Algernon, 1)
IsBully = false
end

function BullyDefeated()
if PedIsDead(Damon) and IsBully = false then
IsBully = true
TextPrintString("Congratulations! Algernon is safe for now thanks to your intervention.",4,1)
Wait(6000)
MissionSucceed(true, false, false)
CameraSetWidescreen(true) 
Wait(4000)
CameraFade(500, 0)
Wait(500)
CameraAllowChange(true)
SoundSetAudioFocusPlayer()
CameraReturnToPlayer()
CameraReset()
CameraSetWidescreen(false)
  PlayerSetHealth(800)
end

main = function()
repeat
BullyDefeated()
Wait(0)
until not Alive
end



« Last Edit: March 04, 2016, 11:55:03 PM by AlphaTech »

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #7 on: March 05, 2016, 04:54:56 AM »
Code: [Select]
if DuckisGoose = false  and DuckisFound = false and PedIsValid(Goose) then
--Duck Found
end

THIS ISN'T RIGHT! Would it kill ya to test your scripts before posting them? You need TWO equal signs for comparison, one equal sign is for assignment. I'm sorry but you do this constantly. Please, make sure you know what you're talking about before posting all this invalid code and confusing newbies.

Offline Binary101010

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #8 on: March 09, 2016, 01:18:28 PM »
DaBOSS54320, thank you for the very helpful notes. I will keep all of your advice in mind as I am working on the project.

I tried running the script with your modifications through the game. Unfortunately, I seem to be having a similar problem to before. The "congratulations" text is appearing on the screen immediately after starting the mission. I am also getting the standard yellow "You Passed" text for mission success immediately after that. So it would appear that the mission success trigger is working correctly, but the PedIsDead(Damon) trigger is firing off immediately at the beginning of the mission before I've had a chance to even lay a hand on him.

Offline AlphaTech

  • LostInSpace
  • Sr. Member
  • ***
  • Posts: 758
  • Gender: Male
  • The name's AlphaTECH, whatca need help with?! :)
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #9 on: March 09, 2016, 02:11:06 PM »
Code: [Select]
if DuckisGoose = false  and DuckisFound = false and PedIsValid(Goose) then
--Duck Found
end

THIS ISN'T RIGHT! Would it kill ya to test your scripts before posting them? You need TWO equal signs for comparison, one equal sign is for assignment. I'm sorry but you do this constantly. Please, make sure you know what you're talking about before posting all this invalid code and confusing newbies.

My fucking bad I made a mistake Christ I will refix the script just didn't have time to.  >:( I also added a blip for you to use so players and yourself could see where to go to.

I tried running the script with your modifications through the game. Unfortunately, I seem to be having a similar problem to before. The "congratulations" text is appearing on the screen immediately after starting the mission. I am also getting the standard yellow "You Passed" text for mission success immediately after that. So it would appear that the mission success trigger is working correctly, but the PedIsDead(Damon) trigger is firing off immediately at the beginning of the mission before I've had a chance to even lay a hand on him.

So here is the fix, also all peds must be spawned before running the PedIsDead
Code: [Select]
function MissionSetup()
AreaTransitionXYZ(0, 270, -110, 6.4000000953674)
  DisablePunishmentSystem(true)
  AreaDisableAllPatrolPaths()
  DisablePunishmentSystem(false)
Algernon = PedCreateXYZ(4,265,-110,6.4000000953674) -- Spawn bully target
  Damon = PedCreateXYZ(12,262,-110,6.4000000953674) --Spawn bully
MissionBlip = BlipAddXYZ(12,262,-110,6.4000000953674,0)
SpawnCallToBully()
end

function SpawnCallToBully()
  local x,y = 4,256
  local r1 = x + 1.5
  local r2 = y + 1.5
  local r3 = x - 1.5
  local r4 = y - 1.5
  repeat
    Wait(0)
  until PedInRectangle(gPlayer,r1,r2,r3,r4)
  Wait(1500)
BlipRemove(MissionBlip)
 TextPrintString("Algernon is one of the geeks at school. He needs protection from Damon, the jock who has been bullying him.", 4, 1)
Wait(4000)
PedRecruitAlly(Algernon,gPlayer)
 PedShowHealthBar(Algernon, true, "N_Algernon", true)
PedAttack(Damon, Algernon, 1)
IsBully = false
end

function BullyDefeated()
if PedIsDead(Damon) and IsBully == false then
IsBully = true
TextPrintString("Congratulations! Algernon is safe for now thanks to your intervention.",4,1)
Wait(6000)
MissionSucceed(true, false, false)
CameraSetWidescreen(true) 
Wait(4000)
CameraFade(500, 0)
Wait(500)
CameraAllowChange(true)
SoundSetAudioFocusPlayer()
CameraReturnToPlayer()
CameraReset()
CameraSetWidescreen(false)
  PlayerSetHealth(800)
end

main = function()
repeat
BullyDefeated()
Wait(0)
until not Alive
end
Try running my script and looking at my tutorials it should work correctly now I just had an error with the (=) their should always be (==) instead of one looking like this. if PedIsDead(Damon) and IsBully == false then not like this if PedIsDead(Damon) and IsBully = false then
All blips are used like this TestBlip = BlipAddXYZ(X,Y,Z,0) (0 is the small Yellow X appearing on the map) (The blip's location should always be the same as the objective location not any wear else. If you script the blip in another location you may mess up player's in finding certain things.)
« Last Edit: March 09, 2016, 03:50:35 PM by AlphaTech »

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #10 on: March 09, 2016, 03:22:55 PM »
Oh I see the problem. Also I am sorry AlphaTech for being rather hypocritical as I did not test my script either until now.

Notice in MissionSetup you first make Damon a local variable.
 local Damon = nil -- Initialize bully

This makes it so that Damon can only be used in that function, and when the function returns/ends, the variable will no longer exist until next time it is created.

So the solution is simple, make Damon a global variable by simply removing local. This will make it so you can use Damon in the main function. Algernon is fine since you do not use him anywhere else other than in MissionSetup.

I will modify my original post with the fixed version of the script. I apologize for the trouble.

EDIT: I also made it so Damon's attitude towards the player changes if it is too high, this is also now in my original/edited post.
« Last Edit: March 09, 2016, 03:27:56 PM by DaBOSS54320 »

Offline Binary101010

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #11 on: March 23, 2016, 12:05:09 PM »
Yep, that was the problem! Removed the local from the lines defining the other peds and everything worked exactly as expected. I now have a fully working one-part mission. Now to expand it out into two or three parts. I'll definitely check back in if I need help. Thanks again!

Offline DaBOSS54320

  • Hero Member
  • ****
  • Posts: 3,398
  • Gender: Female
    • View Profile
Re: Having trouble getting PedOnDeath triggers to work
« Reply #12 on: March 23, 2016, 02:47:24 PM »
Sounds great! Good luck!