Storing strings/integers to be used in a different class from seperate file.

Hello forum hope you're all having a sweet day, Im new to C++ and am trying to make a text based game in the console (I know the argument already, I know what I'm getting into.)

Also the title of this posts pretty iffy but didn't know what to call it, any suggestions on that are welcome.

I want to make a class, or classes? (Any input welcome)
That stores a template for an NPC, say in a header file I make a class called NPC1, in a separate source file Ill have have a declaration for it to output its name and health when a condition is met. But I want to store separate data for multiple different NPC's and have NPC1 (or NPC2 or NPC3 for having multiples up at once) call the data from elsewhere so I don't have to write code for 1000 different creatures. If I wanted to change the level or health or stats of an NPC it would be as easy as going into a file and changing a number, not having to dig through multiple functions of the same source code.

This code is just a general outline of what Im trying to do, I dont think it even compiles.

I know how the basics of functions, classes, headers, and the very basic data types (int, string, bool)

Any advice at all on this subject is greatly appreciated, just keep in mind I'm new I most likely will not understand things if you try to explain it too complicated or with no explanation.

As always thank you all!




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Main Source


 #inlcude <iostream>
#include <string>
#include "Npc.h"

using namespace std;



int main
{
	
	cout << "Welcome player"
	cout << "An enemy has appeared!"
	
	G_Npc::G_NPC;
	G_NPC::G_Npc1();
	
	
	
return 0;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// NPC.cpp
#include "Npc.h"
#include <iostream>

using namespace std;

G_Npc::G_Npc1
{
	string name;
	int Level; //(Needs values from a different file)
	int Health; //(Needs values from a different file)
	
	cout << name << " has approached\n" << Level << Health 
  return 0;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//NPC.h
#ifndef NPC_H
#define NPC_H
#include <iostream>
#include <string>

class G_Npc
{
public:
	void G_Npc1();
	
};

#endif




1
2
3
4
5
// File to store data for variables

?
?
?

Last edited on
Hello, I recommend this video tutorial to you: https://www.youtube.com/watch?v=tVWckBaB5xo
You'll find plenty of helpful things throughout the video.
why not let the constructor accept health and level param ?
example :

snake.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Snake::Snake(int init_size, int pdot_size, sf::Vector2f& pmap) :
    dot_size(pdot_size),
    map(pmap),
    direction(Direction::up),
    Cooldown(sf::milliseconds(100))
{
    for (int i = 0; i < init_size; ++i)
    {
        snake.push_back(sf::CircleShape(dot_size / 2));
        if (i == 0) snake.back().setFillColor(sf::Color::Blue);
        else snake.back().setFillColor(sf::Color::Green);
        int x_temp = (map.x / 2);
        int y_temp = (map.y / 2) + (i * dot_size);
        snake.back().setPosition(x_temp, y_temp);
    }
}


main.cpp

1
2
3
4
 const int init_size = 3;
 const int dot_size = 10;
 bool game_over = false;
Snake snake{init_size, dot_size, map_size};


codes taken from
https://github.com/Flaze07/snake-CPP-
Last edited on
so...have your problem been solved
Sorry for the late replies was busy until now.

Flaze your replies a little complicated to understand right now but Ill look into the things I dont understand I'm sure its useful.

MultiMedia thanks seems like a pretty sweet video, going to take the few hours to watch it now.

Thank you guys Ill get back to you in a few hours.
good to know :D
Okay so I've done some work, having a hard time understanding but getting it, but having a problem.

Im using MSVS and there is no option to compile, under build it only says "Run code analysis"

Im probably going to make a new thread for this if I'm having more issues after I figure out why I cant compile as the code has changed drastically.
Topic archived. No new replies allowed.