0

So I'm trying to change some bytes in a game's memory. The function by itself works completely fine, but when I put it into a class it doesn't do basically anything.

main.cpp

int main()
{
    while (!GetAsyncKeyState(VK_END))
    {
        // this is the function I'm talking about
        if (vars::infAmmo)
        {
            mem::nop((void*)0x7FF65564B5E0, 8);
        }
        else
        {
            mem::patch((void*)0x7FF65564B5E0, (void*)"\x41\x89\x84\x8E\x94\x01\x00\x00", 8);
        }

        Sleep(100);
    }

    return 0;
}

^^ this works completely fine

After that, I decided to put it into a class, to organize my code a little. Then I encountered the issue of it not working.

misc.cpp

#include "pch.h"

void CMisc::infAmmo()
{
    if (vars::infAmmo)
    {
        mem::nop((void*)0x7FF65564B5E0, 8);
    }
    else
    {
        mem::patch((void*)0x7FF65564B5E0, (void*)"\x41\x89\x84\x8E\x94\x01\x00\x00", 8);
    }
}

void CMisc::run()
{
    infAmmo();
}

misc.h

#pragma once

class CMisc
{
private:
    void infAmmo();
public:
    CMisc() {};
    ~CMisc() {};
public:
    void run();
};

main.cpp

CMisc misc{};

int main()
{
    while (!GetAsyncKeyState(VK_END))
    {
        misc.run();

        Sleep(100);
    }
    return 0;
}

^^ basically doesn't do anything

Sorry, if it's something obvious, but I just can't find out what is wrong.

  • Can you post the error messages? Also, what does `run()` actually look like? – mattlangford Jun 20 '21 at 15:12
  • @mattlangford there's no error, it just doesn't do anything in game. The function is supposed to stop ammo from decrementing, but rather than that, it doesn't do anything. I've noticed, that it kind of works, as it keeps writing the bytes after the else statement, but it never updates for the nop function. – tonda240705 Jun 20 '21 at 15:14
  • 1
    Where does `0x7FF65564B5E0` come from? – mattlangford Jun 20 '21 at 15:15
  • @mattlangford it's just the address of the ammo decrement function, that can be found in the game's memory (Cube 2: Sauerbraten) – tonda240705 Jun 20 '21 at 15:18
  • The code looks correct to me, other than I'm not quite sure what `vars` is. I'm assuming it is correct, since it works when you call it directly in the `main` function. But, depending on what it is and how it's defined, there may be some odd interactions when you move this code that calls it into a new source code file. More generally, though, what I'd say is that you aren't benefiting in any way from adding a class here. That doesn't help improve modularity or organization, because you aren't either (A) saving state, or (B) modeling an object. Just define a free function. Then test that. – Cody Gray - on strike Jun 20 '21 at 15:20
  • also the run() is just calling the infAmmo() as I've stated above ``` – tonda240705 Jun 20 '21 at 15:20
  • My guess would be, `vars::infAmmo` means something different in misc.cpp than it does in main.cpp. How is `vars::infAmmo` defined? – Igor Tandetnik Jun 20 '21 at 15:56
  • @IgorTandetnik `vars::infAmmo` is located in header `variables.h`. The variable by itself is defined as `static bool infAmmo { false };`... after that I just have an if statement to change the current value of it `if(GetAsyncKeyState(VK_F2) & 1) vars::infAmmo = !vars::infAmmo` – tonda240705 Jun 21 '21 at 10:24
  • This way, every translation unit (basically, the source file) has its own independent copy of `infAmmo` variable. You change this variable in one translation unit - but that change is not visible to others. That's why the code worked while everything was in one file, and stopped working when split in two. See also: [How do I use extern to share variables between source files?](https://stackoverflow.com/q/1433204) – Igor Tandetnik Jun 21 '21 at 13:10
  • @IgorTandetnik oh, yeah.. that would make sense, thank you! – tonda240705 Jun 21 '21 at 16:13

0 Answers0