I'm trying to program a simple text rpg in Arduino (it's just a coding project to get experience using Arduino, don't recommend me better platforms to make a game on).
So far, I've constructed a program that asks the user for the name of their character, takes that input and passes it to the character object as an attribute "name", confirms that that's what the user wants to name their character, and then gives them the option to view the character info or start the story. This all works when it's part of the main code in the same file. I'm trying to put it in a library/separate file so I don't clutter up the main code with 200 lines of extra code to be used once.
Whenever I try to do that, however, I get a host of errors. I think it's an issue with how I'm calling the function from the library, since the code itself is copy/pasted from the main file where it works fine. I've tried declaring the function in question (startCampaignWithName) as different return types, but that hasn't worked. It doesn't return anything, so I'm not sure why it's a problem that it's declared void.
If you try to test the commented code in the main Character file, you have to put a space before anything you input into the serial monitor. I'm using space as a start marker and \n as an end marker so the monitor knows what input to use. It's annoying, I know, but space was the least annoying start marker I could think to use.
.h files normally have declarations not function definitions, such as you have. Your .h files should be .cpp file. or you could probably just make them .ino files and the java thing will combine them appropriately.
I've tried that and still get the same error. At this point I have it all in the same file just so I don't have to keep switching between three tabs to find what's wrong
yes i corrected a few and saw many more errors. i wasn't willing to debug code that i think is organized very unconventionally and far from complete. why wasn't Potion.h posted? even if i got thru the errors, i'm not sure i have all the code.
why is there so much code commented out, leaving almost nothing in boo.ino
why put so much code in Character_Naming.h
why if the class declaration in boo.ino and not in a .h if you're going to have other files?
why a class?
instead of writing a bunch of code, compiling it and then posting the code with so many errors, why not write a little bit of code and test it. make sure ti compiles a does what you want?
There's so much code in Character_Naming because that's what I needed to do what I want it to do?
Potions.h doesn't have anything to do with this and works fine. The character class has one function that has to do with it and I forgot to remove that before I posted it, my bad.
Everything compiles when Character_Naming is in the bottom of the Character file, and problems only arise when they're in separate files. That is the problem I'm trying to fix. I realize it says "g was not declared in this scope" and I'm working on that now, that is a separate issue
#include "Character_Naming.h"
class Character {
protected:
int Health;
int Mana;
int Stamina;
int WornArmor;
int MaxHealth;
int MaxStam;
int MaxMana;
String Name;
public:
Character(int h, int m, int s)
: MaxHealth(h), MaxMana(m), MaxStam(s) {
Health = MaxHealth; Mana = MaxMana; Stamina = MaxStam;
}
Character(int h, int m, int s, int a)
: Health(h), Mana(m), Stamina(s), WornArmor(a) {}
String n;
void setName(String n) {
Name = n;
}
void getName() {
Serial.print(Name);
}
void getInfo() {
Serial.print("Name: "); Serial.println(Name);
Serial.print(Health); Serial.println(" Health points");
Serial.print(Mana); Serial.println(" Mana points");
Serial.print(Stamina); Serial.print(" Stamina points");
}
};
void setup() {
Serial.begin(9600);
Serial.println("(Put a space before anything you want to type :))");
Serial.println("Enter your character's name: ");
}
void loop() {
startCampaignWithName();
}
#ifndef CHARACTER_NAMING_H
#define CHARACTER_NAMING_H
static const byte maxDataLength = 30;
char receivedChars[maxDataLength + 1];
boolean newData = false;
String Name;
boolean a = false;
int x;
int y;
int h;
int yeet;
int yoot;
boolean inita = false;
String choose1 = "1";
String choose2 = "2";
int b = 0;
boolean c = false;
boolean initc = false;
boolean d = true;
int z = 0;
boolean e = false;
boolean gh = false;
int dummy = 0;
//other functions
void asker(g)
{
g.setName(receivedChars);
Serial.print("Your name is "); g.getName(); Serial.print("? ");
Serial.println("y/n");
newData = false;
a = true;
}
int decisionPoint() {
String input = receivedChars;
if (input == "y") {
Serial.println("Good!");
newData = false;
inita = true;
a = false;
c = true;
return;
}
else if (input == "n") {
Serial.println("Enter your character's name:");
a = false;
newData = false;
}
else {
Serial.println("what? dcision");
newData = false;
}
}
void Campaign() {
while (z < 1) {
Serial.println("*Shrug emoji*");
z++;
}
initc = true;
}
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = ' ';
char endMarker = '\n';
char rc;
if (Serial.available() > 0)
{
rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx > maxDataLength) {
ndx = maxDataLength;
}
}
else
{
receivedChars[ndx] = '\0'; //terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
};
int startMenu(Character g) {
while (y < 1) {
Serial.println(""); Serial.println("Please select and option:"); Serial.println("[1] View Character Info"); Serial.println("[2] Begin Campaign");
++y;
} e = false;
if (newData) {
String yate = receivedChars;
if (yate == choose1) {
yeet = 1;
while (b < 1) {
g.getInfo();
b++;
d = true;
e = true;
// return yeet;
}
newData = false;
}
else if (yate == choose2) {
yeet = 2;
//Serial.println("campaign yeet");
newData = false;
d = false;
c = false;
initc = true;
// return yeet;
}
else {
while (y < 2) {
Serial.println("What (startmenu)");
Serial.println(yate);
++y;
}
newData = false;
c = true
;
}
}
};
void setCampaignWithName(){//function that actually does the thing
setNameInput();
if(gh == true){
Campaign();
}
}
int setNameInput(Character g) {
recvWithStartEndMarkers();
if (a == false && inita == false) {
if (newData) {
asker(g);
}
}
if (a == true) {
if (newData) {
decisionPoint();
}
}
if (c) {
for ( int j = 30; j > 0; j--) {
if (e == true) {
b = 0;
y = 0;
}
startMenu(g);
if (d == false) {
// Serial.println("It broke!");
gh = true;
break;
}
}
}
};
#endif
Sorry if I was rude earlier, I realize you all are trying to help and I very much appreciate it, I will make an effort to be more amenable in the future