Home   Help Search Login Register  

Author Topic: Fired and random in a script  (Read 3031 times)

0 Members and 1 Guest are viewing this topic.

Offline The-Architect

  • Former Staff
  • ****
  • Bite my shiny metal...
    • Bob's Un-official Flashpoint Page
Fired and random in a script
« on: 14 Mar 2005, 19:16:23 »
Ok, I can't wait for answers to my Car stereo script request so I decided to make one myself. Its designed just for what I need it for though.
This is my first attempt at a script proper so go easy on me.

I have two units involved in this script apart from my group. The idea is that whilst I'm in a Resistance vehicle, I'm not going to get shot at and I can hear their radio chatter. Once I leave it though, I'm back to being a bad guy again.

I want to do two things with it.

1. Have it so that if I fire the weapon on the vehicle, I'm detected.

2. Have one of two bits of radio dialogue played randomly whilst I'm in the vehicle.

Also, if ther'es a way to list my group with just a line instead of all the Him1, Him2 etc, I'd appreciate some pointers on that too.

Here's what I have so far.


me1 setcaptive true
him1 setcaptive true
him2 setcaptive true
him3 setcaptive true
him4 setcaptive true
him5 setcaptive true

#Begin
~10
? not(me1 in btr60): goto "End"
rtr1 globalchat "Report."
~3
? not(alive btr): goto "Alert"
btr globalchat "All Clear."

~10
? not(me1 in btr60): goto "End"
rtr1 globalchat "Check in."
~3
? not(alive btr): goto "Alert"
btr globalchat "Everything's quiet."
goto "Begin"

#Alert
rtr1 globalchat "I repeat. Report at once!"
~5
rtr1 globalchat "We have a problem."
alarm = true
goto "End"

#End
me1 setcaptive false
him1 setcaptive false
him2 setcaptive false
him3 setcaptive false
him4 setcaptive false
him5 setcaptive false
exit

Thanks.
« Last Edit: 14 Mar 2005, 19:16:56 by The-Architect »
James Andrew Wilkinson 1977 - 2005 R.I.P.
"If it ain't the friggin' incoming it's the friggin' outgoing. Only difference is who gets the friggin' grease, and that ain't no friggin' difference at all."

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Fired and random in a script
« Reply #1 on: 14 Mar 2005, 19:48:26 »
3.   "_x setCaptive true" forEach units grpName

1.  Add an eventHandler "fired" to the vehicle.   The EH launches a script.   The script checks to see if me1, or any member of the group or whatever, is in the vehicle and if so, the consequences occur.

2.   Here is a script from Un-Impossible: t's a little confusing at first, but very easy once you look.     The relevant parts are in bold.     Variables are used to ensure that each line of random chat is played at most once.     Another variable is used to ensure that there can be only one line of chat for each pass of the loop.  

Hope it helps.  Borrow and adjust.



; I have amended and improved this and added voices.  macguba


;firstaid.sqs
;by General Barron
;aw_barron@hotmail.com
;Based on Morphine Injection Script by nt3n

;This script gives a unit a firstaid kit to use on himself, via the action menu.
;AI in the player's group can use this script, but the player has to tell them to
;use it on themselves via the radio menu. The kit can only be used one time.
;To use the script, put this in the unit's init field:
; this addaction ["Use first aid kit", "firstaid.sqs"]

_unit = _this select 0

;exit if somebody else used this unit's action
? _unit != _this select 1 : hint localize "STR_SCRIPT_2"; exit

;exit if unit isn't properly hurt
? getdammage _unit <= 0.1 : hint localize "STR_SCRIPT_3"; exit

;exit if unit is in a vehicle
? vehicle _unit != _unit : hint localize "STR_SCRIPT_4"; exit

_unit groupchat localize "STR_SCRIPT_5"
_unit say "medic1"
_unit removeAction (_this select 2)

; these variables are used to ensure that each chat line is used only once
; and so that the chat lines do not overlap
_chat=false
_chat1=true
_chat2=true
_chat3=true

_i=0

#working

_i=_i+1
? _i >=4 : _i=0; _chat=true

;exit if unit has been killed
? not (alive _unit) : exit

;end script if unit has been damaged while healing himself
? getdammage _unit > _health : goto "interrupt"

;heal unit slightly, and loop if not yet finished healing
_unit setdammage (getdammage _unit - 0.05)

_unit switchMove "Medic"

_health = getdammage _unit

~1.5

;reassuring chat

? _chat and _chat1 and (random 1 < 0.1) : _unit groupchat localize "STR_SCRIPT_6"; _unit say "medic3"; _chat1=false; _chat=false

? _chat and _chat2 and (random 1 < 0.1) : _unit groupchat localize "STR_SCRIPT_7"; _unit say "medic4"; _chat2=false; _chat=false

? _chat and _chat3 and (random 1 < 0.1) : _unit groupchat localize "STR_SCRIPT_8"; _unit say "medic5"; _chat3=false; _chat=false



;loop

? getdammage _unit > 0 : goto "working"

~0.5

_unit groupchat localize "STR_SCRIPT_9"
_unit say "medic2"


exit



#interrupt
_unit groupchat localize "STR_SCRIPT_10"
_unit say "medic6"

exit
           
« Last Edit: 14 Mar 2005, 19:50:23 by macguba »
Plenty of reviewed ArmA missions for you to play

Offline The-Architect

  • Former Staff
  • ****
  • Bite my shiny metal...
    • Bob's Un-official Flashpoint Page
Re:Fired and random in a script
« Reply #2 on: 14 Mar 2005, 19:52:35 »
Cheers Macca.

So you're saying that I have to start another script from my first, thus having two running at the same time? It would be easier I guess, that way I can keep looping the fired one.

That bit about the radio. I don't understand that. Remember this is the first time I've tried to script.
James Andrew Wilkinson 1977 - 2005 R.I.P.
"If it ain't the friggin' incoming it's the friggin' outgoing. Only difference is who gets the friggin' grease, and that ain't no friggin' difference at all."

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Fired and random in a script
« Reply #3 on: 14 Mar 2005, 20:01:24 »
The only bit you need a second script for is the firing of the gun.  Everything else you can do in one script.    The second script starts only if the gun is fired, and is started automatically by the eventhandler.  (Not the first script.)  I'd leave that bit till the end:  eventhanders are not difficult but it is an extra bit to learn.  Rather than trying to do everything at once do it one step at a time and get this script working first.

If you just want one of two bits of radio chat played at a certain time, try this.  (Sorry my example was probably a bit too complicated.)  

... script comes along
? random 2 < 1 : loon1 sideChat "blah1"; goto "jump"
loon1 sideChat "blah2"
#jump
.... script continues

If the random number is less than 1, blah1 gets played and the script jumps over the next line of code.   If the random number is 1 or more, the first line of blah gets skipped and the second line is played.
Plenty of reviewed ArmA missions for you to play

Offline ACF

  • Members
  • *
  • Llama?? Ain't that French for tanks?
Re:Fired and random in a script
« Reply #4 on: 14 Mar 2005, 21:34:10 »
Here's an alternative approach. It attempts to dispense with a second script for unmasking the player.

Run this [very unchecked script] at the start of the mission and it will set up the EH then wait for the vehicle to be entered. It's commented so where I've got it wrong you can at least see what I was trying to do...  Here, the radiosamples live in an array from which they are deleted once they've been randomly played.  A possible refinement is to only 'hide' the units that are in the vehicle, but that is a complication.


Code: [Select]
; initialise alarm global variable
alarm = false

; setup radio samples
_radiosamples = ["sample1","sample2","sample3" ... "sampleN"]

; add EH to vehicle to
vehicleconcerned AddeventHandler ["FIRED",{alarm = true; {_x SetCaptive false} ForEach Units Group player}]

; wait for player to get in the vehicle
@(player In vehicleconcerned)

; 'hide' the group
{_x SetCaptive true} ForEach Units Group player

#loop
~(random 10)
; checkplayer is still in vehicle after pause
?(Vehicle player == player): Goto "done"
; select a random index in the array
_i = random (Count _radiosamples)
; make it an integer
_i = _i - (_i Mod 1)
; pick a sample
_sample = _radiosamples Select _i
; replace the picked sample
_radiosamples Set [_i,"temp"]
; delete the picked sample
_radiosamples = _radiosamples - ["temp"]
; play the sample
?(!alarm): vehicleconcerned GlobalChat _sample
?(alarm): vehicleconcerned GlobalChat "We have a problem!"
~5
; if samples remain, no alarm and player is still in the vehicle then go round again
?(Count _radiosamples > 0 && player In vehicleconcerned && !alarm): Goto "loop"

#done
; wait for player to leave vehicle and unhide the group
@(Vehicle player == player)
{_x SetCaptive false} ForEach Units Group player

exit

Offline The-Architect

  • Former Staff
  • ****
  • Bite my shiny metal...
    • Bob's Un-official Flashpoint Page
Re:Fired and random in a script
« Reply #5 on: 15 Mar 2005, 01:26:36 »
Ok, none of that made any sense whatsoever. I got confused trying to add you guys' ideas to my script. I told you I'm a noob at this. After much trawling in the forums I found some stuff that would help. Here's what I've got now.

"_x setcaptive true" foreach units mygrp

#Begin
btr addeventhandler ["fired", {[mygrp] exec "btrfired.sqs"}]
_random = random 2
?(_random> 0) && (_random <= 1) : goto "radio1"
?(_random> 1) && (_random <= 2) : goto "radio2"


#Radio1
~10
? not(me1 in btr60): goto "End"
rtr1 globalchat "Report."
playsound "report"
~3
? not(alive btr): goto "Alert"
playsound "Allclear"
btr globalchat "All Clear."

#Radio2
~10
? not(me1 in btr60): goto "End"
rtr1 globalchat "Check in."
playsound "checkin"
~3
? not(alive btr): goto "Alert"
playsound "everythingsquiet"
btr globalchat "Everything's quiet."
goto "Begin"

#Alert
playsound "irepeatreportatonce"
rtr1 globalchat "I repeat. Report at once!"
~5
playsound "wehaveaproblem"
rtr1 globalchat "We have a problem."
~10
alarm = true
goto "End"

#End
"_x setcaptive false" foreach units mygrp

exit

Now All I need to do is find out what's going on with the fired eventhandler. I think I've called the script in question but nothing is happening.

In the script called btrfired.sqs I have this written.

"_x setcaptive false" foreach units mygrp
~10
Alarm = true
exit

I'm nearly there if I can just figure out this bit.  :D
James Andrew Wilkinson 1977 - 2005 R.I.P.
"If it ain't the friggin' incoming it's the friggin' outgoing. Only difference is who gets the friggin' grease, and that ain't no friggin' difference at all."

Offline ACF

  • Members
  • *
  • Llama?? Ain't that French for tanks?
Re:Fired and random in a script
« Reply #6 on: 15 Mar 2005, 02:09:14 »
Couple of thoughts:

1) Add the eventhandler BEFORE #Begin otherwise you are adding an extra EH every time the script loops. This will call your "btrfired.sqs" multiple times.

2) Each EH will also activate every time the weapon if fired, starting more instances of "btrfired.sqs".  The solution is to have as the first line of "btrfired.sqs":

btr RemoveAllEventHandlers "FIRED"

which will do what it says on the tin.It may also be worth asdding this line after #End if you don't want alarm to go true if the btr is fired by anyone else.

3) Put a line like:
Hint "btrfired.sqs is running"
in "btrfired.sqs" just to check it is being called - that will eliminate or identify if the EH is the problem or there's one with whatever 'alarm' activates.

4) Long shot - change the name of the variable 'alarm' it might be conflicting with a class name for the alarm sound effect.

Keep at it!

Offline The-Architect

  • Former Staff
  • ****
  • Bite my shiny metal...
    • Bob's Un-official Flashpoint Page
Re:Fired and random in a script
« Reply #7 on: 15 Mar 2005, 02:46:14 »
Excellant. I tried what you said and found that the script wasn't being called. It turns out that I was using the wrong unit to start the event handler.

Now what I need to do is stop the radio messages if the fired eventhandler has started the script.

Here's what I have.

"_x setcaptive true" foreach units mygrp
btr60 addeventhandler ["fired", {me1 exec "onfiring.sqs"}]


#Begin
_random = random 2
?(_random> 0) && (_random <= 1) : goto "radio1"
?(_random> 1) && (_random <= 2) : goto "radio2"


#Radio1
~10
? not(me1 in btr60): goto "End"
rtr1 globalchat "Report."
playsound "report"
~3
? not(alive btr): goto "Alert"
playsound "Allclear"
btr globalchat "All Clear."

#Radio2
~10
? not(me1 in btr60): goto "End"
rtr1 globalchat "Check in."
playsound "checkin"
~3
? not(alive btr): goto "Alert"
playsound "everythingsquiet"
btr globalchat "Everything's quiet."
goto "Begin"

#Alert
playsound "irepeatreportatonce"
rtr1 globalchat "I repeat. Report at once!"
~5
playsound "wehaveaproblem"
rtr1 globalchat "We have a problem."
~10
alarm = true
goto "End"

#End
"_x setcaptive false" foreach units mygrp

exit

Then the script started by the EventHandler is this,

hintcadet "You have been detected."
btr60 RemoveAllEventhandlers "fired"
? _weapon = "NOR_BTR60_KPVT" : goto "Busted"
? _weapon = "NOR_BTR60_PKT" : goto "Busted"

#Busted
"_x setcaptive false" foreach units mygrp
~10
Alarm = true
exit
James Andrew Wilkinson 1977 - 2005 R.I.P.
"If it ain't the friggin' incoming it's the friggin' outgoing. Only difference is who gets the friggin' grease, and that ain't no friggin' difference at all."

Offline ACF

  • Members
  • *
  • Llama?? Ain't that French for tanks?
Re:Fired and random in a script
« Reply #8 on: 15 Mar 2005, 08:58:52 »
Use 'alarm' as a condition; either:

1) When you jump within your script, or just before the playsounds, check to see if alarm is true; if it is, jump to #Busted instead. This is perhaps the neater solution.

2) More of a bodge - like I had in my earlier offering, make the PlaySounds conditional:
?(!alarm): "PlaySound whatever"
The script should still end itself when the player exits, it won't loop forever.



Offline Clayborne

  • Members
  • *
Re: Fired and random in a script
« Reply #9 on: 26 Dec 2009, 15:49:31 »
Hi, sorry to dig this up but it is sort of relevant to something I'm doing.

I was looking for a script like this that would randomly select a member of a group, and then make him 'say' a line of dialogue chosen randomly from a pre-defined list. Basically it's for idle radio chatter. It doesn't matter if the same line is spoken twice, just that different members in the group get a chance to say them.

I'm fairly sure I could manage this using triggers and such, but I'd rather it was a sort of automated thing and I'm not sure how to do the random group member thing. Any help would be appreciated!

EDIT

This is what I've got

Code: [Select]
#beg

_grp = _this select 0

_aunits = units _grp

#assign

_r = random count units _grp

_r = _r - _r % 1

speaker = _aunits select _r

#Begin
_random = random 3
?(_random> 0) && (_random <= 1) : goto "radio1"
?(_random> 1) && (_random <= 2) : goto "radio2"
?(_random> 2) && (_random <= 3) : goto "radio3"

#Radio1
~10
speaker say "recon1"
goto "beg"


#Radio2
~10
speaker say "sweepsector"
goto "beg"


#Radio3
~10
speaker say "checkzone"
goto "beg"



exit


This seems to work ok, but is there a better way of doing it? Also, how would I make the delay between each speaker random? Like between 10 and 30 seconds?
« Last Edit: 26 Dec 2009, 19:36:19 by bedges »

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Fired and random in a script
« Reply #10 on: 26 Dec 2009, 19:47:19 »
Code: [Select]
_grp = _this

#assign

; wait between 10 - 30 seconds
~(10 + random 20)

; choose a loon at random - make sure he's alive
_r = random ({alive _x} count units _grp)
_r = _r - _r % 1
_speaker = (units _grp) select _r

; choose a random goto marker
_saywhat = random 3
_saywhat = _saywhat - _saywhat % 1
goto format ["radio%1", _saywhat]


#radio1
_speaker say "recon1"
goto "assign"


#radio2
_speaker say "sweepsector"
goto "assign"


#radio3
_speaker say "checkzone"
goto "assign"

exit

A wee bit tidier.

If you're passing just one argument to a script, you just need _this. Call using groupname exec "scriptname.sqs", no brackets required.

You might also want to consider a global variable to determine if the script should continue looping - if stuff starts exploding around them you might want them to shut up  ;)

Offline Clayborne

  • Members
  • *
Re: Fired and random in a script
« Reply #11 on: 26 Dec 2009, 22:13:09 »
Thanks for the reply. Unfortunately your script stops working after a few loops for some reason, while mine keeps going. I would like to use your less cluttered version though if you can see what the issue is.

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Fired and random in a script
« Reply #12 on: 27 Dec 2009, 01:22:06 »
It's probably because random will sometimes return 0, so the amended script should include something to take that into account, e.g.

Code: [Select]
; choose a random goto marker
_saywhat = random 3
_saywhat = _saywhat - _saywhat % 1
? _saywhat == 0 : _saywhat = 1
goto format ["radio%1", _saywhat]

or you could create a #radio0 goto marker. Either way, good luck :good:

Walter_E_Kurtz

  • Guest
Re: Fired and random in a script
« Reply #13 on: 27 Dec 2009, 01:26:19 »
In its current form, bedges' _saywhat randomisation returns possible values of 0, 1 or 2.

Thus, you options are to:

1. Renumber your radio messages: #radio0, #radio1 and #radio2

2. Or, add the line _saywhat = _saywhat + 1 before the goto.

Offline bedges

  • Administrator
  • *****
    • OFPEC The Editing Center
Re: Fired and random in a script
« Reply #14 on: 27 Dec 2009, 01:35:19 »
^ what he said ;)