u/Dastarstellar

Today i moved away from std::unique_ptr and tried to undestand the overloading of operators, at first i didnt really undertand but i think im getting there and i think that this might be the best advice that someone has gave me. Today i had to other things so cut a small piece of time that i had to write an HP class. Tell me what you think about it as a first time trying to understand more advanced things. Before you ask me about the operator += that doesnt heal if you are dead, in many turn based combact games if you are dead you have to revive the member with a special item so i will probably be writing a revive function. Heres the class!(i know that theres a bug with the constructor that if you try to give currentHP a value higher than maxHp it wont hesitate and do it, but after 3 hours of studying math i didnt really care and wanted to try for the first time, in the future i will fix it)

class healthPoints{

    private:
        int maxHp;
        int currentHp;

    public:
        healthPoints(int mh, int ch) : maxHp(mh), currentHp(ch) {}

        void operator-=(int damage){
            currentHp -= damage;

            if(currentHp<0){
                currentHp = 0;
            }
        }

        void operator+=(int heal){
            if(currentHp){
                currentHp += heal;
                if(currentHp>=maxHp){
                    currentHp = maxHp;
                }
            }
        }

        explicit operator bool() const{
            return currentHp > 0;
        }
};
reddit.com
u/Dastarstellar — 17 days ago

Today i tried implementing some more advices that you said and i completly modified the main.

After talking about my struggles with some classmates that have more knowledge than me and taking my time to return to the computer and code i think that im understanding this better. I will be honest, i used AI to better understand this but i think that if you use it to understand things and not using it to make things for yourself i think its not that bad. Now that i know how it works i will surely make it my own the next time.

Even if people suggested me to use std::remove_if (even my classmates) i didnt for three main reasons:

  1. I want to focus on smart pointers for now, and not learning too many things at the same time
  2. Because apparantely its really slow, for now it isnt, but because i really wanna get into optimization for my project i think that its important to have the habitude. I will learn it to be a better programmer, but not now
  3. To be honest i didnt have much time today, so trying to undestand a operator that yesterday seemed like learning a new completely language wasnt really exiciting. So i focused more on the smart pointers

Here's the code with some improvements from the last comments, not all of them, beacuse i wanna learn them step by step and not oveload my brain with too many things. Thank you for the support! I really appreciate what you are doing and as 15yo it means so much!

#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include <string>
#include <windows.h>

class Enemy{

    protected:
        std::string name;
        int hp;

    public:
        Enemy(std::string n, int h) : name(n), hp(h) {}

        virtual void attacck() = 0;

        bool isAlive(){
            if(hp<=0){
                hp = 0;
                return false;
            }

            return true;
        }

        void setDamage(int dmg){
            if(isAlive()){
                hp -= dmg;
            }else{
                std::cout<<"Enemy is dead\n";
            }
        }

        void printStats(){
            std::cout<<"TYPE: "<<name<<" | HP:"<<hp<<"\n";
        }


        virtual ~Enemy() = default;
};

class AlphaNULL : public Enemy{

    public:
        AlphaNULL() : Enemy("AlphaNULL", 200) {}

        void attacck() override{
            std::cout<<"AlphaNULL attacks!\n";
        }
};

class AlphaNULL_elite : public Enemy{

    public:
        AlphaNULL_elite() : Enemy("AlphaNULL Elite", 400) {}

        void attacck() override{
            std::cout<<"AlphaNULL Elite attacks violently!\n";
        }
};

//prototypes
void deleteEnemy(std::vector<std::unique_ptr<Enemy>>& v, int& i);

int main(){

    std::vector<std::unique_ptr<Enemy>> enemyParty;
    enemyParty.reserve(20);

    enemyParty.emplace_back(std::make_unique<AlphaNULL>());
    enemyParty.emplace_back(std::make_unique<AlphaNULL_elite>());
    enemyParty.emplace_back(std::make_unique<AlphaNULL_elite>());
    enemyParty.emplace_back(std::make_unique<AlphaNULL>());

    while(!enemyParty.empty()){
        for(int i = 0; i<enemyParty.size(); i++){

            enemyParty[i]->printStats();
            enemyParty[i]->setDamage(20);

            if(!enemyParty[i]->isAlive()){
                deleteEnemy(enemyParty, i);
            }
        }

        Sleep(500);
        system("cls");
    }

    std::cout<<"YOU KILLED ALL THE ENEMIES\n";

    return 0;
}

//function to delete an enemy
void deleteEnemy(std::vector<std::unique_ptr<Enemy>>& v, int& i){
    v[i] = std::move(v.back());
    v.pop_back();
    i--;
}
reddit.com
u/Dastarstellar — 18 days ago

I read your comments today and i started to implement your advices! I tried writing a loop that goes on until the vector doesnt have enemies that are alive, this took me 1 and half a hour to write and im still not sure about it, i even tried using std::remove_if but i didnt understand it. I think that im understanding smart pointers but at the same times i have more questions than answers, i started right away with smart pointers in vectors do maybe thats the thing stopping me, but for my project what im learning its essential. If you could tell me how to understand them better, like exercises and etc. i wuold really appreciate it! So heres the code, in english and without namespace std;, i probably did something wrong writing this so please let me know what are the problems

#include <iostream>
#include <memory>
#include <vector>
#include <string>

class Enemy{

    protected:
        std::string name;
        int hp;
        bool alive;

    public:
        Enemy(std::string n, int h, bool al) : name(n), hp(h), alive(al) {}

        virtual void attacck() = 0;

        void isAlive(){
            if(hp<=0){
                hp = 0;
                alive = false;
            }else
                alive = true;
        }

        void getDamage(int dmg){
            if(alive == true)
                hp -= dmg;
        }

        bool getAlive()const {return alive;}

        virtual ~Enemy() {}
};

class AlphaNULL : public Enemy{

    public:
        AlphaNULL() : Enemy("AlphaNULL", 200, true) {}

        void attacck() override{
            std::cout<<"AlphaNULL attacks!"<<std::endl;
        }
};

class AlphaNULL_elite : public Enemy{

    public:
        AlphaNULL_elite() : Enemy("AlphaNULL Elite", 400, true) {}

        void attacck() override{
            std::cout<<"AlphaNULL Elite attacks violently!"<<std::endl;
        }
};

int main(){

    std::vector<std::unique_ptr<Enemy>> enemyParty;
    enemyParty.reserve(20);

    enemyParty.emplace_back(std::make_unique<AlphaNULL>());
    enemyParty.emplace_back(std::make_unique<AlphaNULL_elite>());

    bool enemyAlive = true;

    while(enemyAlive){
        enemyAlive = false;

        for(auto& x : enemyParty){
            if(x->getAlive() == true && x){
                x->getDamage(20);
                x->isAlive();
                std::cout<<"Enemies are taking damage"<<std::endl;
                enemyAlive = true;
            }
        }
        std::cout<<"TURN FINISHED"<<std::endl<<std::endl;
    }

    std::cout<<"Simulation terminated with success"<<std::endl;

return 0;

}

reddit.com
u/Dastarstellar — 19 days ago

With your explanations from the previuos post i started using unique_ptr with classes and vector. I understood that smart pointers are actually really useful beacuse i can litterally write one line to delete one enemy, i also tried to prevent the segmentation fault with the if in the second for. Is this the standard way in C++? And other than shared_ptr and weak_ptr what should i focus next?
Also, thanks for the replies on my previous post, i really appreciate them!(And yes, my code is in italian because thats where im from!)

#include <iostream>
#include <vector>
#include <memory>

using namespace std;

class Nemico{

    protected:
        int hp;
        string nome;
        bool vivo;

    public:
        Nemico(int h, string n, bool alive) : hp(h), nome(n), vivo(alive) {}

        virtual void attacca() = 0;

        virtual ~Nemico() {}
};

class AlphaNULL : public Nemico{

    public:
        AlphaNULL() : Nemico(200, "AlphaNULL", true) {}

        void attacca() override{
            cout<<"AlphaNULL attacca!"<<endl;
        }
};

class AlphaNULL_elite : public Nemico{

    public:
        AlphaNULL_elite() : Nemico(400, "AlphaNULL_elite", true) {}

        void attacca() override{
            cout<<"AlphaNULL Elite distrugge tutti attaccando!"<<endl;
        }

};

int main(){

    vector<unique_ptr<Nemico>> party;
    party.emplace_back(make_unique<AlphaNULL>());
    party.emplace_back(make_unique<AlphaNULL_elite>());

    for(int i = 0; i<party.size(); i++){
        party[i]->attacca();
    }

    party[0].reset();
    cout<<endl<<endl;

    for(int i = 0; i<party.size(); i++){
        if(party[i])
            party[i]->attacca();
    }

}

reddit.com
u/Dastarstellar — 19 days ago

I'm a 15yo trying to make a game in UE5, i'm still a beginner programmer but i learned classes and raw pointers pretty fast. I tried to understand smart pointers but orher than the garbage collector i dont understand the differencce between raw and smart

reddit.com
u/Dastarstellar — 20 days ago