From f5c649124e10d98793a04c1936849ab55f529cb6 Mon Sep 17 00:00:00 2001 From: Resxt <55228336+Resxt@users.noreply.github.com> Date: Sun, 27 Feb 2022 00:08:01 +0100 Subject: [PATCH] actions_on_button_press 1.1 - Splitted the old script into 2 different scripts - Fixed camera_thirdPerson changing for all players when a player presses the button by turning it into a vote instead --- actions_on_button_press/README.md | 26 +-- .../actions_on_button_press.gsc | 98 ------------ .../camera_switch_vote_on_button_press.gsc | 148 ++++++++++++++++++ .../suicide_on_button_press.gsc | 46 ++++++ 4 files changed, 209 insertions(+), 109 deletions(-) delete mode 100644 actions_on_button_press/actions_on_button_press.gsc create mode 100644 actions_on_button_press/camera_switch_vote_on_button_press.gsc create mode 100644 actions_on_button_press/suicide_on_button_press.gsc diff --git a/actions_on_button_press/README.md b/actions_on_button_press/README.md index 5e924de..604fdfb 100644 --- a/actions_on_button_press/README.md +++ b/actions_on_button_press/README.md @@ -1,17 +1,11 @@ # Actions On Button Press -This script shows how you can execute functions and change dvars for a player when that player presses a certain button while showing him instructions on his screen using the key defined in his game. +These scripts allow players on the server to press a button (displayed on screen) to trigger a function +Note that if you combine several scripts you will have to change the texts positions so that they don't overlap by changing the last value in `setPoint()` +You can also change the text's position in `setPoint()` and the size and font type in `createServerFontString()` -* The first button lets the player toggle the `camera_thirdPerson` dvar (used to play in third person while keeping the crosshair on and is also better/more configurable than `cg_thirdPerson` in my opinion) -Note that `camera_thirdPerson` is a special case and needs to be changed with `SetDynamicDvar()` but in most cases like with `cg_thirdPerson` you'd use `SetDvar()` instead. - -* The second button lets the player suicide using the `Suicide()` function. - -If you use `+melee` and the player's melee key is set to V it'll show V on his screen, if he uses another key it'll show his key. -If you want to use another button simply change `level.third_person_button` and/or `level.suicide_button` values and it will update the button pressed to start the function and the text displayed on the player's screen. - -I used actionslot 6 and 7 because (to my knowledge) they are not used by anything else in the game -Here is a non-exhaustive list of buttons you can use +Changing the button in `Init()` will change both the button displayed on screen and the button used to trigger the function. +Here is a non-exhaustive list of buttons you can use ``` "+usereload" "weapnext" @@ -31,3 +25,13 @@ Here is a non-exhaustive list of buttons you can use "+stance" "+breathe_sprint" ``` + +## suicide_on_button_press.gsc + +The player dies when pressing the button + +## camera_switch_vote_on_button_press.gsc + +Allows the players to toggle their vote to change the `camera_thirdPerson` dvar on the server (players vote no by default) +When everyone votes yes the server changes the dvar and resets the vote counts. +Bots are ignored for the votes. \ No newline at end of file diff --git a/actions_on_button_press/actions_on_button_press.gsc b/actions_on_button_press/actions_on_button_press.gsc deleted file mode 100644 index c0e2e49..0000000 --- a/actions_on_button_press/actions_on_button_press.gsc +++ /dev/null @@ -1,98 +0,0 @@ -#include maps\mp\gametypes\_hud_util; - -Init() -{ - level.third_person_button = "+actionslot 6"; - level.suicide_button = "+actionslot 7"; - - DisplayButtonsText(); - - level thread OnPlayerConnect(); -} - -OnPlayerConnect() -{ - for(;;) - { - level waittill("connected", player); - - player thread OnSuicideButtonPressed(level.suicide_button); - player thread OnCameraToggleButtonPressed(level.third_person_button); - - player thread OnPlayerSpawn(player); - } -} - -OnPlayerSpawn(player) -{ - self endon("disconnect"); - - for(;;) - { - self waittill("spawned_player"); - wait 1; - - player DisableDof(); - } -} - -OnCameraToggleButtonPressed(button) -{ - self endon("disconnect"); - level endon("game_ended"); - - self notifyOnPlayerCommand("third_person_button", button); - while(1) - { - self waittill("third_person_button"); - - if (GetDvar("camera_thirdPerson") == "0") - { - SetDynamicDvar( "camera_thirdPerson", 1); - } - else if (GetDvar("camera_thirdPerson") == "1") - { - SetDynamicDvar( "camera_thirdPerson", 0); - } - } -} - -OnSuicideButtonPressed(button) -{ - self endon("disconnect"); - level endon("game_ended"); - - self notifyOnPlayerCommand("suicide_button", button); - while(1) - { - self waittill("suicide_button"); - - self Suicide(); - } -} - -DisplayButtonsText() -{ - suicide_text = level createServerFontString( "Objective", 0.65 ); - suicide_text setPoint( "RIGHT", "RIGHT", -4, -227.5 ); - suicide_text setText("^1Press [{" + level.suicide_button + "}] to suicide"); - - third_person_text = level createServerFontString( "Objective", 0.65 ); - third_person_text setPoint( "RIGHT", "RIGHT", -4, -220 ); - third_person_text setText("^1Press [{" + level.third_person_button + "}] to toggle the camera"); -} - -EnableDof() -{ - self setDepthOfField( 0, 110, 512, 4096, 6, 1.8 ); -} - -DisableDof() -{ - self setDepthOfField( 0, 0, 512, 512, 4, 0 ); -} - -Debug(text) -{ - print(text); -} \ No newline at end of file diff --git a/actions_on_button_press/camera_switch_vote_on_button_press.gsc b/actions_on_button_press/camera_switch_vote_on_button_press.gsc new file mode 100644 index 0000000..889e5fe --- /dev/null +++ b/actions_on_button_press/camera_switch_vote_on_button_press.gsc @@ -0,0 +1,148 @@ +#include maps\mp\gametypes\_hud_util; + +Init() +{ + level.camera_switch_vote_button = "+actionslot 6"; + + DisplayButtonsText(); + + level thread DisplayVoteCount(); + level thread OnPlayerConnected(); +} + +OnPlayerConnected() +{ + for(;;) + { + level waittill("connected", player); + + player.pers["camera_switch_vote"] = false; + + player thread OnCameraSwitchVoteButtonPressed(level.camera_switch_vote_button); + + player thread OnPlayerSpawned(player); + } +} + +OnPlayerSpawned(player) +{ + self endon("disconnect"); + + for(;;) + { + self waittill("spawned_player"); + + player DisableDof(); + } +} + +OnCameraSwitchVoteButtonPressed(button) +{ + self endon("disconnect"); + level endon("game_ended"); + + self notifyOnPlayerCommand("camera_switch_vote_button", button); + while(1) + { + self waittill("camera_switch_vote_button"); + self.pers["camera_switch_vote"] = !self.pers["camera_switch_vote"]; + if (self.pers["camera_switch_vote"]) + { + CustomPrintLn("You voted to switch the camera"); + } + else + { + CustomPrintLn("You removed your vote to switch the camera"); + } + } +} + +DisplayButtonsText() +{ + camera_switch_vote_text = level createServerFontString( "Objective", 0.65 ); + camera_switch_vote_text setPoint( "RIGHT", "RIGHT", -4, -227.5 ); + camera_switch_vote_text setText("^1Press [{" + level.camera_switch_vote_button + "}] to toggle vote for camera switch"); +} + +DisplayVoteCount() +{ + level.camera_switch_vote_count_text = level createServerFontString( "Objective", 0.65 ); + level.camera_switch_vote_count_text setPoint( "RIGHT", "RIGHT", -4, -220 ); + + while(true) + { + yes_votes = 0; + human_players = []; + + foreach (player in level.players) + { + if (player.pers["camera_switch_vote"]) + { + yes_votes++; + } + + if (!isDefined(player.pers["isBot"])) + { + human_players[human_players.size] = player; + } + else + { + if (!player.pers["isBot"]) + { + human_players[human_players.size] = player; + } + } + } + + level.camera_switch_vote_count_text setText("^1Votes to switch camera: " + yes_votes + "/" + human_players.size); + + if (yes_votes == human_players.size && human_players.size > 0) + { + // Logic here + setDvar( "camera_thirdPerson", !getDvarInt( "camera_thirdPerson" ) ); + + // Mention changes to all players (if there is more than one player) + if (human_players.size > 1) + { + if (getDvarInt( "camera_thirdPerson" ) == 0) + { + CustomPrintLn("Everyone voted to switch to 1st person"); + } + else + { + CustomPrintLn("Everyone voted to switch to 3rd person"); + } + } + + // Reset votes variables + yes_votes = 0; + + foreach (player in human_players) + { + player.pers["camera_switch_vote"] = false; + } + } + + wait 0.01; + } +} + +EnableDof() +{ + self setDepthOfField( 0, 110, 512, 4096, 6, 1.8 ); +} + +DisableDof() +{ + self setDepthOfField( 0, 0, 512, 512, 4, 0 ); +} + +CustomPrintLn(text) +{ + IPrintLn("^1" + text); +} + +Debug(text) +{ + print(text); +} \ No newline at end of file diff --git a/actions_on_button_press/suicide_on_button_press.gsc b/actions_on_button_press/suicide_on_button_press.gsc new file mode 100644 index 0000000..47b7be8 --- /dev/null +++ b/actions_on_button_press/suicide_on_button_press.gsc @@ -0,0 +1,46 @@ +#include maps\mp\gametypes\_hud_util; + +Init() +{ + level.suicide_button = "+actionslot 7"; + + DisplayButtonsText(); + + level thread OnPlayerConnected(); +} + +OnPlayerConnected() +{ + for(;;) + { + level waittill("connected", player); + + player thread OnSuicideButtonPressed(level.suicide_button); + } +} + +OnSuicideButtonPressed(button) +{ + self endon("disconnect"); + level endon("game_ended"); + + self notifyOnPlayerCommand("suicide_button", button); + while(1) + { + self waittill("suicide_button"); + + self Suicide(); + } +} + +DisplayButtonsText() +{ + suicide_text = level createServerFontString( "Objective", 0.65 ); + suicide_text setPoint( "RIGHT", "RIGHT", -4, -227.5 ); + suicide_text setText("^1Press [{" + level.suicide_button + "}] to suicide"); +} + +Debug(text) +{ + print(text); +} \ No newline at end of file