Okay, I have many questions, mainly about subjects that haven't been discussed well in my school class, although I also have a few questions pertaining to a RPG I've been working on.

1. Threads. I've tried to search for a good tutorial on their basics, but I can't seem to find anything, can anyone explain them more fully to me. The main reason I want to use them is for monsters in my RPG, currently I am having to constantly add to a timer while doing other things to make sure the monsters do not pause while the player swings a sword. I just want to be able to assign each monster their own thread, allowing them to move independently of the rest of the game.

2. I'm having trouble making this RPG very flexible, one reason I know is because I used global variables rather than passing by reference, because of that I will most likely have to remake the majority of the program, but my main problem is, when the player swings their weapon, the 3 coordinates on the 2D array of the 3 spaces in front of the character are checked for enemies, this is where I run into some problems, as I have to brute-force each monster individually in order to check them. This makes it impossible for me to have a variable amount of monsters, as adding more requires me to brute-force even more into the program, which I want to avoid.
I've thought about using classes, and giving each monster their own class, but this is a bit of a waste of memory, and I was wondering if there was any other way I could do this.

3. Probably the main reason I want to avoid using classes is because I'm still a little shaky with them, if anyone can provide a good basic tutorial of classes and when to use them, it would be greatly appreciated.

4. Global Variables, I want to know when to use them, vs. when to pass variables around, I assume there are some basic guidelines on when to use either.

Thanks ahead of time for any answers, if you want to see the RPG so far, here's the sourcecode:
RPG

Space to Talk, ASWD to move, space to swing sword.

Recommended Answers

All 6 Replies

You're coding in C++. Forget about global variables, try and make a class that has all those variables. If you can't fit it in a class or function you're quite possibly doing something wrong. Globals are considered dangerous when you tend to use them as much as you do. It also cleans up your code a lot, 2000 lines is a lot, especially when I think you could make it a lot more readable by splitting all the code up in classes. Having the functions that need the member variables of a class in that same class or friends of that class also eliminates the need to pass every variable and allows you to simply make private "safe" data members.

2, 3, 4 should be solved by learning and using classes. 1 is very advanced and will make your eyes bleed and your program deadlock. ;-) Assigning each monster its own thread would do you little good since you'd have to lock every variable/array it'll access and unlock it... It will probably slow down the program quite a lot.

Well, really try to encapsulate most of the functions and variables into classes. That should be your focus. I like the idea and your work on the RPG though! Very cool. :D


The monster hit algorithm could be something like this right:?

For each monster
   for each hit coordinate
       check if the monster.location == hitcoordinate[n]
       if so do damagerollorsomething

You're coding in C++. Forget about global variables, try and make a class that has all those variables. If you can't fit it in a class or function you're quite possibly doing something wrong. Globals are considered dangerous when you tend to use them as much as you do. It also cleans up your code a lot, 2000 lines is a lot, especially when I think you could make it a lot more readable by splitting all the code up in classes. Having the functions that need the member variables of a class in that same class or friends of that class also eliminates the need to pass every variable and allows you to simply make private "safe" data members.

2, 3, 4 should be solved by learning and using classes. 1 is very advanced and will make your eyes bleed and your program deadlock. ;-) Assigning each monster its own thread would do you little good since you'd have to lock every variable/array it'll access and unlock it... It will probably slow down the program quite a lot.

Well, really try to encapsulate most of the functions and variables into classes. That should be your focus. I like the idea and your work on the RPG though! Very cool. :D


The monster hit algorithm could be something like this right:?

For each monster
   for each hit coordinate
       check if the monster.location == hitcoordinate[n]
       if so do damagerollorsomething

Okay, so I guess it all comes down to classes, and I guess I'm fine not using threads, as you can see, the program functions perfectly well right now, I just thought threads might make it little less annoying.

I think that what I could do is assign each monster a class, and that should allow me to be at least slightly more variable. I began to try and get rid of my global variables by passing them all by reference and such, but after about 15 minutes, I realized that my program was way too messy and not flexible enough, so I'll probably have to go and redo a lot of it.

As for hitting the monsters, the biggest problem is that the look of the monsters can vary, so I can't have a fixed look, meaning I have to check to see if the coordinates of the monster match with the coordinates of the sword swing, so I have to brute-force it, although using classes should reduce that at least a bit more, not to mention when you hit the monsters, they are knocked back and damage is done based on which monster you hit, so I need to know exactly which one it was.

You can have a monster class with a function isHit(const unsigned int x, const unsigned int y); that returns whether the monster is hit. This makes the hit function belonging to the player really short, since it only needs to feed 3 coordinates, the monster does the rest. Like this, you can hide big pieces of code, make sure they work and never look at them again until you want to change them.

Best of luck with it, I might also take a look at it myself if you like? I'm kinda out of projects now, and this seems a nice project with a dedicated developer. Besides, I have some OpenGL skills if you'd like it to be more visual...

Let me know,

You can have a monster class with a function isHit(const unsigned int x, const unsigned int y); that returns whether the monster is hit. This makes the hit function belonging to the player really short, since it only needs to feed 3 coordinates, the monster does the rest. Like this, you can hide big pieces of code, make sure they work and never look at them again until you want to change them.

Best of luck with it, I might also take a look at it myself if you like? I'm kinda out of projects now, and this seems a nice project with a dedicated developer. Besides, I have some OpenGL skills if you'd like it to be more visual...

Let me know,

Wait, I still don't think I see what you mean, it sounds to me like you plan on passing in the coordinates of the sword blade, and then using a function in each monsters class to check if their coordinates match, is this what you're talking about, or am I off?

I kinda want to work on this alone, as this is my first large-scale project, I'm pretty much just doing it for practice, thanks for the offer, and the help so far.

Also, I can't seem to remember off the top of my head the command to create a variable type, I want to have a variable with two possible values, 1 or 0, basically, a binary variable, it's not typecast...

It's called a

bool

in C++ ;)

You can create types with typedef.

typedef <newtype> <oldtype>

IIRC

Okay then, I found a good tutorial for Classes, thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.