Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Save Variables in .txt file without tabbing out of actual window
#1
Hey,

i would be happy to get some help to create a Macro for a friend who streams on Twitch and unses OBS for a text overlay. I already got some things up and running. The Macro counts Kills, Chickendinners and other things related to pubg when the player presses the hotkeys.

Code:
Copy      Help
int k ;;killcounter actual match
int c ;; chickendinner counter today
int m ;; match counter
int d ;; death counter
k = 0
c = 0
m = 0
d = 0
 1
rep() 0.05;
    ;; Count Rounds
    if (ifk(CS) and ifk(m)) ;; Add +1 to Rounds if Control Shift and m is pressed
        wait 0.25
        m=m+1
        out F"Rounds: {m}"
        OnScreenDisplay F"Round {m}"
    ;; Count Kills
    if (ifk(CS) and ifk(k))
        wait 0.25
        k=k+1
        out F"Kills: {k}"
        OnScreenDisplay F"Kills {k}"
    ;; Count Chickendinners
    if (ifk(CS) and ifk(c))
        wait 0.25
        c=c+1
        out F"Chickendinners: {c}"
        OnScreenDisplay F"Chickendinners {c}"
    ;; Count Deaths
    if (ifk(CS) and ifk(d))
        wait 0.25
        d=d+1
        out F"Deaths: {d}"
        OnScreenDisplay F"Deaths: {d}"
    ;; Reset all Counters with Concrol Shift r
    if (ifk(CS) and ifk(r))
        k = 0
        c = 0
        m = 0
        d = 0
        OnScreenDisplay "Reset all counters"
    ifk(F12) ret ;;end if F12 is pressed
    goto 1
    break

;; The folder where the txt file should be saved
;; str sData.getfile("$documents$\Kills.txt") ;;get file data
;; .txt file content:
;; Matches: 0
;; Kills: 0
;; Chickendinners: 0
;; Deaths: 0

;; The OnScreenDisplay is just for testing purpose. ;)


Now i want the macro to save the variables into a predefined .txt file when he presses a key. The best solution would be if the txt file gets saved without defocusing the game. But its also important that the macro can be saved as an exe because he doesnt own a QM license. If it is possible to save the variables to .txt without defocusing the game it would be great if it is saved instantly when he uses one of the functions. This would mean his Stream overlay will be updated instantly. If that isnt possible, he would be ok with a save hotkey.

I searched a lot but i have no idea how to write this. It took me two hours to find out how to write this macro.   Blush

Thanks in advance for your help!
#2
Why getfile? Do you want to add the variable values to the previously saves values (saved = saved + new)? Or just save the variable values (saved = new)?
#3
Quote:Why getfile? Do you want to add the variable values to the previously saves values (saved = saved + new)? Or just save the variable values (saved = new)?

Hey Gintaras. Yes, exactly. The best solution would be if the macro can read and add the numbers behind the text. In case the game or PC crashes, the values are still saved.
#4
Macro Macro109
Code:
Copy      Help
int k c m d
k=1; c=2; m=3; d=4 ;;example values

str sData sFile.expandpath("$documents$\Kills.txt")
int k2(k) c2(c) m2(m) d2(d)
if FileExists(sFile)
,sData.getfile(sFile)
,ARRAY(str) a
,if(findrx(sData "^Matches: (\d+)[]Kills: (\d+)[]Chickendinners: (\d+)[]Deaths: (\d+)[]$" 0 0 a)<0) end "invalid file format"
,m2+val(a[1]); k2+val(a[2]); c2+val(a[3]); d2+val(a[4]);

sData=F"Matches: {m2}[]Kills: {k2}[]Chickendinners: {c2}[]Deaths: {d2}[]"
sData.setfile(sFile)

out sData ;;delete this line
#5
Thanks very much for your help but it only works partially. It saves wrong numbers and i dont know why. I already tried to solve it with wait but this doesnt help.
This is the Code im using:


Code:
Copy      Help
int k ;;killcounter actual match
int c ;; chickendinner counter today
int m ;; match counter
int d ;; death counter
k = 0
c = 0
m = 0
d = 0
1
rep() 0.05;
    ;; Count Rounds
    if (ifk(CS) and ifk(m)) ;; Add +1 to Rounds if Control Shift and m is pressed (wait 0.25s to not count twice on one keypress
        wait 0.25
        m=m+1
        out F"Rounds: {m}"
        OnScreenDisplay F"Round {m}"
    ;; Count Kills
    if (ifk(CS) and ifk(k))
        wait 0.25
        k=k+1
        out F"Kills: {k}"
        OnScreenDisplay F"Kills {k}"
    ;; Count Chickendinners
    if (ifk(CS) and ifk(c))
        wait 0.25
        c=c+1
        out F"Chickendinners: {c}"
        OnScreenDisplay F"Chickendinners {c}"
    ;; Count Deaths
    if (ifk(CS) and ifk(d))
        wait 0.25
        d=d+1
        out F"Deaths: {d}"
        OnScreenDisplay F"Deaths: {d}"
    ;; Reset all Counters with Concrol Shift r
    if (ifk(CS) and ifk(r))
        wait 0.25
        k = 0
        c = 0
        m = 0
        d = 0
        OnScreenDisplay "Reset all counters"
    ifk(F12) ret ;;end if F12 is pressed
    wait 0.25
    str sData sFile.expandpath("$documents$\Kills.txt")
    int k2(k) c2(c) m2(m) d2(d)
    if FileExists(sFile)
        sData.getfile(sFile)
        ARRAY(str) a
        if(findrx(sData "^Matches: (\d+)[]Kills: (\d+)[]Chickendinners: (\d+)[]Deaths: (\d+)[]$" 0 0 a)<0) end "invalid file format"
        m2+val(a[1]); k2+val(a[2]); c2+val(a[3]); d2+val(a[4]);
        sData=F"Matches: {m2}[]Kills: {k2}[]Chickendinners: {c2}[]Deaths: {d2}[]"
        sData.setfile(sFile)
    goto 1
    break
#6
Pasted with incorrect indentation. Also probably now the logic is incorrect. I don't know how exactly you want it to behave. Maybe need to move the file loading/parsing part to the beginning.

Also, your code with rep/ifk is unreliable. Instead use registered hotkeys or keyboard hook.

Macro Macro121
Code:
Copy      Help
__RegisterHotKey hk1.Register(0 1 MOD_CONTROL|MOD_SHIFT 'M')
__RegisterHotKey hk2.Register(0 2 MOD_CONTROL|MOD_SHIFT 'K')
__RegisterHotKey hk3.Register(0 3 MOD_CONTROL|MOD_SHIFT 'C')
__RegisterHotKey hk4.Register(0 4 MOD_CONTROL|MOD_SHIFT 'D')
__RegisterHotKey hk5.Register(0 5 MOD_CONTROL|MOD_SHIFT 'R')
__RegisterHotKey hk6.Register(0 6 MOD_CONTROL VK_F12) ;;Ctrl+F12, because Windows does not allow to register F12

int k ;;killcounter actual match
int c ;; chickendinner counter today
int m ;; match counter
int d ;; death counter
k = 0
c = 0
m = 0
d = 0

;at startup get previously saved values
str sData sFile.expandpath("$documents$\Kills.txt")
if FileExists(sFile)
,sData.getfile(sFile)
,ARRAY(str) a
,if(findrx(sData "^Matches: (\d+)[]Kills: (\d+)[]Chickendinners: (\d+)[]Deaths: (\d+)[]$" 0 0 a)<0) end "invalid file format"
,m=val(a[1]); k=val(a[2]); c=val(a[3]); d=val(a[4]);

MSG z
rep
,if(GetMessage(&z 0 0 0)<1) break
,sel z.message
,,case WM_HOTKEY
,,sel z.wParam
,,,case 1
,,,m+1
,,,OnScreenDisplay F"Round {m}"
,,,
,,,case 2
,,,;...
,,,
,,,case 3
,,,;...
,,,
,,,case 4
,,,;...
,,,
,,,case 5 ;;reset
,,,k=0; c=0; m=0; d=0
,,,OnScreenDisplay "Reset all counters"
,,,
,,,case 6 ;;end
,,,ret
,,,
,,sData=F"Matches: {m}[]Kills: {k}[]Chickendinners: {c}[]Deaths: {d}[]"
,,sData.setfile(sFile)
,,
,,out sData ;;delete this line
,
,DispatchMessage &z


Forum Jump:


Users browsing this thread: 1 Guest(s)