Our new Indie Games subforum is now open for business in G&T. Go and check it out, you might land a code for a free game. If you're developing an indie game and want to post about it,
follow these directions. If you don't, he'll break your legs! Hahaha! Seriously though.
Our rules have been updated and given
their own forum. Go and look at them! They are nice, and there may be new ones that you didn't know about! Hooray for rules! Hooray for The System! Hooray for Conforming!
PA Programming Thread - The Best Servers Are The Ones You Don't Use
Posts
Just think, what does every type of character you are making have in common. Let's say, Health and name.
(Doing from memory, so don't shout too much about syntax errors)
Spoiling Code Blocks
public class Adventurer
{
protected String m_name;
protected int m_health;
public Adventurer(String name, int health)
{
m_name = name;
m_health = health;
}
public string getName()
{
return m_name;
}
public int getHealth()
{
return m_health;
}
}
[/Code]
So, that's your base class. Now say you have a Mage, who also has Mana. THen you'd do
public class Mage extends Adventurer { protected int m_mana; public Mage(String name, int health, int mana) { super(name,health) m_mana = mana; } public int getMana() { return m_mana; } } [/CODE] [/Spoiler] Now your Mage has Health, a Name and Mana, and can get them all despite not re-writing the getHealth and getNames. If you have a Rogue who has energy you'd dot he same thing with Energy instead of mana. If you have a BattleMage who also has a Rage stat, you can extend Mage and add Rage. Quick Note on Polymorphism, you can extend only ONE class. So if you are making a MageRogue, you can't say ....extends Mage,Rogue. You'd have to extend either Mage or Rogue, and add the other stats to that. That's the basics of creating a basic class then extending it in different ways. Hope that helps you get started![COde]
public class Mage extends Adventurer
{
protected int m_mana;
public Mage(String name, int health, int mana)
{
super(name,health)
m_mana = mana;
}
public int getMana()
{
return m_mana;
}
}
[/CODE]
If you have a Rogue who has energy you'd dot he same thing with Energy instead of mana. If you have a BattleMage who also has a Rage stat, you can extend Mage and add Rage.
Quick Note on Polymorphism, you can extend only ONE class. So if you are making a MageRogue, you can't say ....extends Mage,Rogue. You'd have to extend either Mage or Rogue, and add the other stats to that.
That's the basics of creating a basic class then extending it in different ways. Hope that helps you get started!
So you would set those classes to to take a string and int and return them. Then in your main class, for me it would be a text area, you would call up those classes to display what was put in, like name = Adventurer.getName() or would it be name = Adventurer(name)? Also, how would that work for an action listener. I have my fight button in my code and I want the action listener to call up a class that will display battle text in a text area (eventually I would like to make it a separate window). BTW your explanation was very good.
Are you particularly attached to OCaml? And what platform is this on? Linux or Windows?
I'm assuming for the time being that home() is always called home() and not something like start() or lookatme() or whatever else I want to call it?
And what is up with return0 on the print command? Why isn't it return 1 for true like everything else seems to be?
home()?
You mean main()?
Also, the normal convention for error returns in C is 0 on success and non-zero on failure. Though printf() actually returns the number of characters it printed and a negative value if it fails. Anyway, 0 on success makes sense if you think about it, because then your error cases look like:
if (func()) { // handle error } // otherwise proceed normally[/CODE] I.e., you only need to do anything special if something bad happens. If you want to branch on success, just stick a ! in front of it. As a side note, while you should generally always check return values, printf() is generally taken for granted. (Note, though, [I]always[/I] check returns from malloc().)[CODE]if (func()) {
// handle error
}
// otherwise proceed normally[/CODE]
I.e., you only need to do anything special if something bad happens. If you want to branch on success, just stick a ! in front of it.
As a side note, while you should generally always check return values, printf() is generally taken for granted. (Note, though, always check returns from malloc().)
SE++ Forum Battle Archive | PST = Pacific Standard Time | DRUNKSTUCK: A Homestuck recap
And that makes sense, thanks for explaining. I guess the guide I'm using could be better in the area of explaining, but the internet (and you folks) vastly make up for any lacking areas.
Thanks!
Well, we all know Ocaml pretty well. We thought about learning Go for kicks or moving over to C++ to get better library support. Right now our stuff builds on anything nix-ish, and you can get it to build under windows with a little elbow grease. The development is mostly happening under linux and OS X though.
Right now the biggest issue has been the libraries. OCaml has opengl and sdl libraries, but they're pretty dated.
Spoiling wall of text and code.
Adventuer playerOne = new Adventuer(DrunkMc,100); [/CODE] Then in your GUI class you would gain access to that player One, say (making up classes here) [CODE]Adventuer currPlayer = gameEngine.getPlayerOne(); textBoxName.setText(currPlayer.getName()); [/CODE] So your button would have an action listener, then using the on click method it would call your main class and say (Replace the .addActionListener / new ActionLIstener with whatever graphic package you are using specific syntax is. [code] fightButton.addActionListener(new ActionListener(){ onClick() { gameEngine.beginFight(currPlayer, Monster); }});Then I would have statistic accessors in the gameEngine class to get the currPlayer's and monsters health and display them just like you displayed the name above.If there's a button that is say, Swing Sowrd you would put in the action listener for that button
gameEngine.attack("sword");
Then you would update the statistics to get the health, mana, rage or whatever post .attack();
So you can see how you click on the Gui, figure out what was clicked (through action listeners) but all the GUI does is tell the game Engine what was clicked and the game engine does it. The game Engine (which is what I keep calling the class that will do all the heavy lifting) could work without the GUI. You want that seperation, you don't want them tied together. So say, 6 months down the road you want to re-do your entire GUI, you don't have to touch the game mechanics.
Also note, and this goes for any new GUI developers out there, do NOT be afraid to tear down your GUI and start over 1,001 times. you learn as you go and things you did in the beginning you realize you can do better, so do it! I've learned GUIs by constantly redesigning and working things that are "done", just because I know I can do it better. Eventually you will be happy with what you have, and despite being able to do better, it's not really worth it. That's when I'm happy!
Let's say I have a QList I allocate in one thread, and add to it. When I try to access it in another thread I get runtime errors. Is there a specific procedure I have to call before I can add/modify/read the QList members without getting a runtime error? I have a mutex but it's not through QT, it's just a bool. I guess this is not enough and I need to do something through QT, but what? Is what I'm doing even possible? I'd hope so. I remember seeing something about moving a variable between threads with a function call.
Invite me: XBox Live | PS3 | Steam
Link to me: Number Sorter | Achievement Generator
I don't believe there is any built-in thread safety for QList. (that you can enable/take advantage of easily I mean)
You want to either wrap it in your own thread-safe class (by adding mutex locks on your calls before performing the operation on the internal QList) or come up with a method of passing data using signals and slots.
The PhalLounge :: Chat board for Phalla discussion and Secret Santas :: PhallAX 2013
Critical Failures IRC! :: #CriticalFailures and #mafia on irc.slashnet.org
Hmm I have gone the mutex route for now, however, I still get the runtime errors. I don't have the code with me as I was working on it at home, but I'll go over the mutex again. For all I know I probably just put the mutex on the inserting portion rather than dataready portion. Thanks infidel.
If you're rolling your own thread code for accessing the QList, I suggest the wrapper pattern to make it much easier to code and maintain and avoid "oh I accessed it wrong in this function woooops."
The PhalLounge :: Chat board for Phalla discussion and Secret Santas :: PhallAX 2013
Critical Failures IRC! :: #CriticalFailures and #mafia on irc.slashnet.org
Straight C. What I've got so far is just
#include <stdio.h> int main(void){ printf("Hello World!"); return 0; }And that's it... I'm still not so sure what the return 0 actually does.
And has anybody worked with Wordpress before and gotten <code> tags to actually keep spaces? This is frustrating....
Basically. I'm sure someone can give a little nicer/more-in-depth answer.
Thanks I am just practicing before I become an official Qt ambassador.
http://en.wikipedia.org/wiki/Exit_status
Not really too helpful other than you're telling the OS, "Hey I'm done, everything looks good." Some OS' (most desktop ones anyways) will clean up after your program too, if you left dangling pointers or other garbage in memory.
Edit: it plays a huge part when you write your own functions, though.
Let's say you have a function (DISCLAIMER: CODE MAY NOT COMPILE):
int LoginUser(char* user, char* pass) { int success = 0; //do stuff here return success; }You could do a whole slew of functions to test for user logins. Maybe the authentication server is down, return 1, maybe the password is incorrect, return 2, maybe the username doesn't exist, return 3.
This let's you do something like"
switch(LoginUser("MyUser","MyPass")) { case 0: printf("Success!"); break; case 1: printf("Authentication Server is Offline"); break; //... so on }That's effectively all you need to concern yourself with at this point yes.
Think about a function call and how it returns a value. Well, programs are actually called, and main() is the function they call, and it returns a number back. (Not technically detailed or correct but all you need to think about it as, yet again.)
What it returns for a program is by convention zero for no-error and non-zero for error, just a convention. Why it is like that is because you may have many different error conditions you wish to relate, but almost always only need one "no error here" distinction, so if we make "no-error" to be zero it makes it very easy to test whether an error occurred or not, computer-wise.
The PhalLounge :: Chat board for Phalla discussion and Secret Santas :: PhallAX 2013
Critical Failures IRC! :: #CriticalFailures and #mafia on irc.slashnet.org
I really wish I could remember which program it was that did that to me. I know I've run into win32 functions where they give back a non-zero int to indicate success.
I can think of a few. Mostly because it makes using the if/else control really easy. Those are usually the libraries that use an excess amount of GetLastError() functions.
Most "standard" functions require you to inspect errno or GetLastError() to get more information.
Zero is often a valid return value (printf, open, etc) anyway. It seems more common to me that an error will result in a negative return value instead of a zero return value.
As I understand it if your function returns a negative value, it will still evaluate as true in an if statement in C, or at least it should because it's "not 0." Anything other than 0 is a success to the if control, IIRC. That's pretty tangential to this, though.
Success/failure and true/false line up only when they're that simple. But, lucky programmers we are, we've got to deal with success, fail - file not found, fail - couldn't get handle to file, fail - hey this is Windows wtf are you using those path separators for, etc.
This is correct, btw. Zero evaluates to false and non-zero to true.
SE++ Forum Battle Archive | PST = Pacific Standard Time | DRUNKSTUCK: A Homestuck recap
If you do:
if(0) { printf("True!"); }It will not evaluate with C, at least on gcc anyways. Edit, and this is why true = 1 as an integer value, usually, and why people return 0 as failure. It makes some weird spooky logical sense. Technically true could be -1 and still evaluate, or -201201.
It made sense in my head, goddamnit. I knew what I meant. :wink:
[strike]strcmp[/strike]strncmp?
strncmp?!?!
STRNCMMMPPPP!!!!
olol wat is this
(hopefully this problem gets ported to the new thread)
For a fun exercise, construct in hardware an instruction that determines whether a given 16-bit value has more ones than zeroes, that has less than twice the cycle time of a 16-bit carry-look ahead (CLA) adder. Naturally, the lower the cycle time the better.
Depends on what you have access to development wise and what you're trying to learn. If you just want more practice writing code, java would probably be just as good as C# + Xna for writing a little game. If you're looking for experience using libraries, then using some sdl libraries from your favorite language might be just as good. Of course, if your goal is to get some practical development experience for consoles, XNA it is.