[GUIDE] Death Counter

dehixem

Member
Hey there, so I found a way to make (and update live) a death counter. It's a fun little thing I wanted to have on my Dark Souls streams... because I die a lot.

What you will be able to do with this guide :
- Bind any key to increment and/or decrement your death counter.
- Have ingame control over the death counter.
- Have your death counter appear on screen for viewers =)

Difficulty to set up : EASY.

Let's get started :

First, you will need the AutoHotKey software, a lightweighted and very low ressource consuming software :)
Install it.

Step 1 :

What you'll do in this step :
- Create the script that will output your number of deaths in a .txt file.
- Bind the keys of your choice to increment or decrement your number of deaths.

Now, create a .txt file in any location (folder) of your choice and name it as you wish (let's say "deathCounterScript.txt").

Note that this code will create a text file with a number in it (useful for OBS), so let's plan that the text file will be in the same folder as deathCounterScript.txt and let's call it deaths.txt.

Open that text file in your favorite text editor and paste the following code :
Code:
;Enter your TXT file path .
filePath = C:\Users\Kevin\Documents\YOUTUBE\PRODUCTION\darksoulsDeathCounter\deaths.txt
FileReadLine, deathVar, %filePath%, 1

;When you press F12 it will increment and update the death counter in the .txt
	F12::
		;Creates txt file if it doesn't exist.
		IfNotExist, %filePath%
			FileAppend,0, %filePath%
		;Created a file.
		;Inputs number of deaths in deathVar variable.
		FileReadLine, deathVar, %filePath%, 1
		Var := ++deathVar
		FileDelete, %filePath%
		FileAppend,%deathVar%, %filePath%
	return

;When you press F11 it will decrement and update the death counter in the .txt
	F11::
		;Creates txt file if it doesn't exist.
		IfNotExist, %filePath%
			FileAppend,0, %filePath%
		;Created a file.
		;Inputs number of deaths in deathVar variable.
		FileReadLine, deathVar, %filePath%, 1
		
		if deathVar = 0
		return

		Var := --deathVar
		FileDelete, %filePath%
		FileAppend,%deathVar%, %filePath%
	return

Note : as for me, I use the F12 key to increment and the F11 key to decrement, you can use any key so feel free to change =)

Dont forget to change "filePath = PATHTOdeaths.txt" to the path of your choice, in my case it is : "filePath = C:\Users\Kevin\Documents\YOUTUBE\PRODUCTION\darksoulsDeathCounter\deaths.txt"

Now, change deathCounterScript.txt's extension to deathCounterScript.ahk (the name can be different if you chose a different name). The .ahk extension stands for AutoHotKey, you'll be able to launch it directly with the software =)

You can now try out for yourself, execute your newly created AHK script, and try pressing F12 ou F11 (note that you cannot go below zero w/ the decremention key). Check the deaths.txt to see that it has been updated :)

You should have something like this at this point :
1544957-JMOOYQ7.png


Step 2 :

Note : You can skip this step if you already are familiar with OBS' textfile input thingy. :3

What you'll do in this step :
- Use OBS to display the number of deaths on your livestream.

Edit : Ok so I totally forgot about this guide and forgot to update it. The next step is really easy and is explained fully over here : viewtopic.php?f=22&t=925#p5268 (CHECK STEP 4 of that tutorial and just replace the .txt path with the .txt that contains the number of deaths :))

Cheers !
 

Kientero

New Member
Ugh... I've tried to make it reset back to 0 with End key... with no luck, programming/scripting is something are just too difficult for me.
I get errors when I think i got it. when there are no errors during reloading the file, it just doesn't work, or does things like adding random numbers like -2452643 and adding 0 after it.
Could you help me with that?
 

GadgetTvMan

New Member
Improved the code. also has a GUI editor for the settings and keys.
Because, you know, death counters need those things.
F12 increases, F11 decreses and F10 resets the counter.
There is an option that is enabled by default to make 0 into nothing.
Also be careful when you set hotkeys. because that key will not work globally if set as a hotkey.
All settings and keys are in the Settings.ini file. if its not there it will create one.
Sorry this isnt a pretty post. I lost track of time and now its 5AM.
I should sleep.
Hope this code is both useful and helps someone to learn AHK/AutoIt.
I also like tea.

OH! Yeah, no longer need to configure the .txt location. it uses the directory the script is in.
Almost forgot that. its literally click and go.
Mwah <3

if you have some script requests. PM me. Cant guarantee ill do them. Since im from the internet, but there is a possibility i will when i get bored.

Please look away from all spelling errors. They are features of sleep deprivation.

Code:
;Ignore The 3 following lines
#Persistent
#InstallKeybdHook
#SingleInstance force

Gosub, StartUpStuffs
;Turns those ugly 0's into Nothing! (When enabled)
;Settings are: 1 = on, 0 = off
IniRead, DisableZeros, %iniLocation%, Settings, DisableZeros

;List of all supported buttons:
;   http://www.autohotkey.com/docs/KeyList.htm
;Configure button used to Increment counter. Default F12
IncreaseCounterButton = %IncButton%
;Configure button used to Decrement counter. Default F11
DecreaseCounterButton = %DecButton%
;Configure button used to Reset counter. Default F10
ResetCounterButton = %RCButton%
;Enter your TXT file path
FileReadLine, counterVar, %filePath%, 1
;------------------------------------------------------------CODE----------------------------------------------------------------
;When you press the button assigned to "IncreaseCounterButton" it will increment and update the death counter in the .txt
;See "IncreaseCounter" for code
   Hotkey, %IncreaseCounterButton%, IncreaseCounter

;When you press the button assigned to "DecreaseCounterButton" it will decrement and update the death counter in the .txt
   Hotkey, %DecreaseCounterButton%, DecreaseCounter
 
;When you press the button assigned to "ResetCounterButton" it will set to null/zero and update the death counter in the .txt
   Hotkey, %ResetCounterButton%, ResetCounter
goto, MoreStartUpStuff
  IncreaseCounter:
        if PopUpIsUp = 0
        {
        ;Creates txt file if it doesn't exist.
        IfNotExist, %filePath%
            FileAppend,0, %filePath%
        ;Created a file.
        ;Inputs number of deaths in counterVar variable.
        FileReadLine, counterVar, %filePath%, 1
        if counterVar =
            counterVar = 0
        counterVar := ++counterVar
        FileDelete, %filePath%
        FileAppend,%counterVar%, %filePath%
        }
    return
   
    DecreaseCounter:
        if PopUpIsUp = 0
       {
        ;Creates txt file if it doesn't exist.
        IfNotExist, %filePath%
            If DisableZeros = 2
                FileAppend,0, %filePath%
            else
                FileAppend,, %filePath%
        ;Created a file.
        ;Inputs number of deaths in counterVar variable.
        FileReadLine, counterVar, %filePath%, 1
        
        if (counterVar = 0 and DisableZeros = 2)
        return

        if (counterVar = 1 and DisableZeros = 1)
            counterVar =
        Var := --counterVar
        FileDelete, %filePath%
        FileAppend,%counterVar%, %filePath%
        }
    return

   ResetCounter:
        if PopUpIsUp = 0
        {
        ;Creates txt file if it doesn't exist.
        IfNotExist, %filePath%
            FileAppend,0, %filePath%
        ;Created a file.
        ;Inputs number of deaths in counterVar variable.
        FileReadLine, counterVar, %filePath%, 1
        if DisableZeros = 1
            counterVar =
        else
            counterVar = 0
        FileDelete, %filePath%
        FileAppend,%counterVar%, %filePath%
        }
    return
    
    MoreStartUpStuff:
        PopUpIsUp = 0
    return

;Label made to configure some stuff at startup. called at the beginning of the file with "Gosub, <label>"
StartUpStuffs:
    ;Sets the variable "iniLocation" to the Settings.ini.
    iniLocation = %A_ScriptDir%\Settings.ini

    ;Checks if the Settings.ini file exists. if it doesnt, it creates and sets the default keys.
    Gosub, InitializeINI
    
    Gosub, ReadKeys
    Gosub, SetFilePath
    
    if PopUp = 1
    {
        PopUpIsUp = 1
        MsgBox, 4, Counter Script, Would you like to configure the hotkeys?
        IfMsgBox No
        {
            MsgBox, 4, Counter Script, Would you like to see these PopUp on next start up? `nDont worry, you can renable this popup in the settings.ini
            IfMsgBox No
                IniWrite, 0, %iniLocation%, Settings, ConfigurationPopupOnStart
            GoSub, Write-ReadKeys
            ;PopUpIsUp = 0
            return
        }
        IfMsgBox Yes
        {
            Gui, Add, Text,, Increment Key:
            Gui, Add, Text,, Decrement Key:
            Gui, Add, Text,, Reset Counter Key:
            Gui, Add, Text,, File Location:
            Gui, Add, Text,, File Name:
            Gui, Add, Text,, Enable(1)/Disable(2) Zeros:
            Gui, Add, DropDownList,Choose%DisableZeros% vDisableZeros xs, 1|2
            Gui, Add, Edit, W80 ym vIncButton, %IncButton% ; The ym option starts a new column of controls.
            Gui, Add, Edit, W80 vDecButton, %DecButton%
            Gui, Add, Edit, W80 vRCButton, %RCButton%
            Gui, Add, Edit, W125 vFileLocation, %FileLocation%
            FileLocation_TT := "Leave blank to use script directory"
            Gui, Add, Edit, W80 vFileName, %FileName%
            Gui, Add, Button,, List of Supported keys
            Gui, Add, Button, default W100 xm, OK  ; The label ButtonOK (if it exists) will be run when the button is pressed.
            Gui, Show, W290, Configuration GUI
            OnMessage(0x200, "WM_MOUSEMOVE")
            
            return
            GuiClose:
                if A_GuiEvent = Normal
                    Gui Destroy
                    PopUpIsUp = 0
                return
            ButtonOK:
                Gui, Submit  ; Save the input from the user to each control's associated variable.
                MsgBox, 4, Counter Script, Would you like to see this PopUp on next start up? `nDont worry, you can renable this popup in the settings.ini
                IfMsgBox No
                    IniWrite, 0, %iniLocation%, Settings, ConfigurationPopupOnStart
                GoSub, Write-ReadKeys
                Gosub, SetFilePath
                PopUpIsUp = 0
                return
            
            ButtonListofSupportedKeys:
            if A_GuiEvent = Normal
                run http://www.autohotkey.com/docs/KeyList.htm
            return
            
            WM_MOUSEMOVE()
            {
                static CurrControl, PrevControl, _TT
                CurrControl := A_GuiControl
                If (CurrControl <> PrevControl){
                        SetTimer, DisplayToolTip, -300 	; shorter wait, shows the tooltip quicker
                        PrevControl := CurrControl
                }
                return
                
                DisplayToolTip:
                try
                        ToolTip % %CurrControl%_TT
                catch
                        ToolTip
                SetTimer, RemoveToolTip, -2000
                return
                
                RemoveToolTip:
                ToolTip
                return
            }
        }
    }
    return
SetFilePath:
    filePath := fileLocation . fileName
    return
InitializeINI:
    IfNotExist, %iniLocation%
    {
        IniWrite, F12,  %iniLocation%, Controls, Incrementkey
        IniWrite, F11,  %iniLocation%, Controls, Decrementkey
        IniWrite, F10,  %iniLocation%, Controls, ResetKey
        IniWrite, 1, %iniLocation%, Settings, DisableZeros
        IniWrite, 1, %iniLocation%, Settings, ConfigurationPopupOnStart
        IniWrite, "", %iniLocation%, Settings, fileLocation
        IniWrite, Counter.txt, %iniLocation%, Settings, fileName
    }
    return
WriteKeys:
    IniWrite, %IncButton%,  %iniLocation%, Controls, Incrementkey
    IniWrite, %DecButton%,  %iniLocation%, Controls, Decrementkey
    IniWrite, %RCButton%,  %iniLocation%, Controls, ResetKey
    iniWrite, %DisableZeros%, %iniLocation%, Settings, DisableZeros
    IniWrite, %fileLocation%, %iniLocation%, Settings, fileLocation
    IniWrite, %fileName%, %iniLocation%, Settings, fileName
    return
ReadKeys:
    IniRead, IncButton, %iniLocation%, Controls, IncrementKey
    IniRead, DecButton, %iniLocation%, Controls, DecrementKey
    IniRead, RCButton, %iniLocation%, Controls, ResetKey
    IniRead, PopUp, %iniLocation%, Settings, ConfigurationPopupOnStart
    IniRead, DisableZeros, %iniLocation%, Settings, DisableZeros
    IniRead, fileLocation, %iniLocation%, Settings, fileLocation
    IniRead, fileName, %iniLocation%, Settings, fileName
    return
Write-ReadKeys:
    Gosub, WriteKeys
    Gosub, ReadKeys
    return
 

GadgetTvMan

New Member
im lost with all this Script stuff,is there an easy way for me to get this working?like a tutorial for dummies?
just follow the first post and ignore
dehixem said:
Note : as for me, I use the F12 key to increment and the F11 key to decrement, you can use any key so feel free to change =)

Dont forget to change "filePath = PATHTOdeaths.txt" to the path of your choice, in my case it is : "filePath = C:\Users\Kevin\Documents\YOUTUBE\PRODUCTION\darksoulsDeathCounter\deaths.txt"
If you want to use my code.
I'll be happy to assist you if you have some trouble
 

Sockie82

New Member
Sorry for posting so long after last post, but I am recently new to OBS, and am trying to setup this to go with my overlays.
I had the first script running to a degree, it would decrease, but not increase when I pressed the assigned hotkeys.
However, the improved script will not work at all. it only tells me "error at line 172. Line text: try Error: This line does not contain a recognized action." and ends program, I tried to remove that line and run again, it said a similar error, but fr line 174, line text: catch. It then exits the program.
Any help would be greatly appreciated as this would be a nice thing to have added to my overlay, to save me tabbing out constantly.

Many thanks
Sockie82
 

GadgetTvMan

New Member
Sorry for posting so long after last post, but I am recently new to OBS, and am trying to setup this to go with my overlays.
I had the first script running to a degree, it would decrease, but not increase when I pressed the assigned hotkeys.
However, the improved script will not work at all. it only tells me "error at line 172. Line text: try Error: This line does not contain a recognized action." and ends program, I tried to remove that line and run again, it said a similar error, but fr line 174, line text: catch. It then exits the program.
Any help would be greatly appreciated as this would be a nice thing to have added to my overlay, to save me tabbing out constantly.

Many thanks
Sockie82

It seems the newer AHK stuff doesnt work the try catch as it used too.
I have the old compiled file with the old AHK version. it works. It was compiled on 23rd October 2013
here is a mirror upload: http://www.mirrorupload.net/file/GXF64DWN/#!CounterScript.zip
I didnt have my notifications enabled for this thread. ill try to fix that now

I also attached the file to this post. Unzip and run the exe. doesnt need anything installed
 

Attachments

  • CounterScript.zip
    533.9 KB · Views: 1,608
I am so confused with this for some reason. Whenever I run the exe in your last post and set up hotkeys I don't know what to do after. I click okay but no text file is made or anything.
 

GadgetTvMan

New Member
I am so confused with this for some reason. Whenever I run the exe in your last post and set up hotkeys I don't know what to do after. I click okay but no text file is made or anything.
The txt file wont be made until you press the increment key.
I would change the code. But I still havent had time to figure out why the original code wont work in the newer ahk.
If after pressing the increment key and no text file is created in the directory, try run the program as admin
 

emptyPROXY

New Member
Thank you, OP! Much appreciated and maybe in the future I will work on something more advanced like what GadgetTvMan made =)
 
Top