[QUOTE=honeybuns;20064235]go back to your script kiddie game hacking forums son[/QUOTE]
lol are you implying that i'm using detours to hack games?
Isn't it a sign that your design isn't correct if you need to know the type of something after you've specifically put it in a list of base class pointers?
[QUOTE=Jallen;20057955]My editor for the game I'm making is coming along nicely.
[img]http://i49.tinypic.com/2m4t3eh.jpg[/img]
I made all of the icons on the buttons lol. It's programmer art but I don't think it's too bad (the open icon is a bit crappy)
The GUI on the right uses SFML and the 3D viewport uses irrlicht.
I'm doing input myself with the win32 message system for the mouse (WM_LMOUSEDOWN, WM_LMOUSEUP plus GetCursorPos with some magic done with GetWindowRect to get mouse position local to the window) and direct input for keys.[/QUOTE]
Oh god that makes me wanna get back into C++ again.
For 3 months this summer I learned C++ and DirectX, and built a 3D engine that loaded BSP map files from the game Quake III Arena. It loaded and used textures, the shadow/light-map data from the BSP file and rendered that on the surfaces, collision detection (The only thing I didn't make because the little bit of math there was a year over my head, but I implemented how it dealt with a collision) and it had a 2D and 3D sky-box. I used few tutorials for that project except for learning how the data is stored in the BSP filetype, and the math functions for the collision detection, and a few other little things like walking the BSP tree for visibility detection.
I miss C++ :,(
[QUOTE=majorlazer;20065269]lol are you implying that i'm using detours to hack games?[/QUOTE]
If I got my lazy ass up I'd code something involving Warcraft 3 and hooks.
Not what you think though :p
I just finished my first game, simple but I just started C++ yesterday.
[cpp]#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int userInput = 0;
cout << "Welcome to MCG!\n";
cout << "Question #1: " << "What was this game made with?" << endl;
cout << "#1: C#\n";
cout << "#2: C++\n";
cout << "#3: Lua\n";
cin >> userInput;
if(userInput != 1 && userInput != 2 && userInput != 3)
{
cout << "Fail\n";
system("PAUSE");
return 0;
}
if(userInput == 2)
{
cout << "\nYou win, now get out!\n";
}
else
{
cout << "\nYou suck\n";
}
system("PAUSE");
return 0;
}[/cpp]
[QUOTE=jA_cOp;20064619]Unions can take you a long way in this situation:
[cpp]
enum TokenType {
TOKEN_NUMBER,
TOKEN_INTEGER,
TOKEN_STRING,
//etc
};
class Token {
//for more safety, make these fields private and add getters
public:
TokenType type;
union
{
std::string string;
int integer;
double number;
//etc
};
Token(std::string str) : type(TOKEN_STRING), string(str){}
explicit Token(int i) : type(TOKEN_INTEGER), integer(i){}
explicit Token(double num) : type(TOKEN_NUMBER), number(num){}
//etc
};
#include <iostream>
int main()
{
Token tok1("Hello, world!");
Token tok2(123);
Token tok3(1.5);
//token type guaranteed to match data
//...
}
[/cpp]
edit:
Templating works too, anyway, it's just not as pretty to implement and use.[/QUOTE]
Good idea but it turns out you can't have classes with constructors in unions (so no std::string).
[QUOTE=noctune9;20064462]You probably want to do this:
[code]A = new Token();
if (StringToken* B = dynamic_cast<StringToken>(A))
{
std::cout << B->stringValue;
}
if (NumberToken* B = dynamic_cast<NumberToken>(A))
{
std::cout << B->numberValue;
}[/code][/QUOTE]
Perhaps. In fact I can use a static_cast because I will know what type it will be. The question is, is this [I]the[/I] way to do it?
[QUOTE=MultiPurpose;20064869]Like this?
[cpp]
template<class type>
inline SomeFunction(Token& token, type value)
{
switch(token->type)
{
case TOKEN_NUMBER:
//do specific NumberToken stuff here
token.numberValue = value;
break;
case TOKEN_STRING:
//do specific StringToken stuff here
token.stringValue = value;
break;
}
}
[/cpp]
Nevermind, [B][URL="http://www.facepunch.com/member.php?u=61889"][B]jA_cOp[/B][/URL]'s post seems better. [/B][/QUOTE]
That wouldn't work because Token doesn't have a numberValue or stringValue member.
[QUOTE=gparent;20065276]Isn't it a sign that your design isn't correct if you need to know the type of something after you've specifically put it in a list of base class pointers?[/QUOTE]
Yes, that's why I'm asking you guys. How would you do it?
[QUOTE=Vampired;20066726]Good idea but it turns out you can't have classes with constructors in unions (so no std::string).[/QUOTE]
You can make it a reference or pointer.
[QUOTE=Vampired;20066726]How would you do it?[/QUOTE]
You cast it. There's no other way to do it in C++, essentially you would want to use virtual functions but when you actually need a different value from different classes you need to start casting.
Okay I have settled for the union method from jA_cOp. Thankyou to each of you for your time.
[QUOTE=AteBitLord;20066540]I just finished my first game, simple but I just started C++ yesterday.
[cpp]#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int userInput = 0;
cout << "Welcome to MCG!\n";
cout << "Question #1: " << "What was this game made with?" << endl;
cout << "#1: C#\n";
cout << "#2: C++\n";
cout << "#3: Lua\n";
cin >> userInput;
if(userInput != 1 && userInput != 2 && userInput != 3)
{
cout << "Fail\n";
system("PAUSE");
return 0;
}
if(userInput == 2)
{
cout << "\nYou win, now get out!\n";
}
else
{
cout << "\nYou suck\n";
}
system("PAUSE");
return 0;
}[/cpp][/QUOTE]
Why the funnies? :frown: I worked hard on it and I am new as fuck to this.
[QUOTE=Vampired;20067155]Okay I have settled for the union method from jA_cOp. Thankyou to each of you for your time.[/QUOTE]
Just make sure that you're really dealing with a situation where a union can replace dynamic_cast without causing problems. Unions are dangerous unless treated with care; luckily C++ offers many features to encapsulate them, so they're easier to work with than in C. If the token type isn't supposed to change after the token has been created, make the fields private and provide getters for the type and each data member. That way no client or client code can accidentally change the type/data without changing the other to reflect the change. If they are supposed to be changeable, consider doing the same thing but using setter functions for the data that automatically updates the type. If the class isn't very big anyway, maybe consider just replacing a token with a new one every time it changes.
edit:
Further expanding on that, you could place asserts in the getters to rule out the last union-related bugs. asserts are only in effect in debug mode, so it wouldn't have any effect on performance.
[QUOTE=AteBitLord;20067354]Why the funnies? :frown: I worked hard on it and I am new as fuck to this.[/QUOTE]
A hint, don't use system("pause");
Instead, use cin.get();
Should do the same thing.
[QUOTE=arienh4;20067504]A hint, don't use system("pause");
Instead, use cin.get();
Should do the same thing.[/QUOTE]
So that would also close the game?
[QUOTE=AteBitLord;20067569]So that would also close the game?[/QUOTE]
No, system("pause") just waits for the user to press a key before continuing in the program. When the program reaches the end of the main function, it closes.
I am currently working on a game of life sort of, will be linear, not open but it's a start right? :frown:
[editline]07:11PM[/editline]
I suck so much.:blush:
[QUOTE=jA_cOp;20064619]Unions can take you a long way in this situation:
edit:
Templating works too, anyway, it's just not as pretty to implement and use.[/QUOTE]
How exactly do you use templates in a generic way in an array/container in C++? Wouldn't that require reflection? I tried something for a factory, but gave up in the end.
[cpp]
#include <map>
template< typename T >
struct creator
{
T* operator( ) ( void ) const
{
return new T( );
}
};
class Factory
{
public:
// what goes..........here vv
std::map<std::string, creator> types;
};
int main( )
{
Factory* fact;
creator<Factory> fCr;
fact = fCr( );
}
[/cpp]
Garry(or everyone in general), how do you do your factories?
Made this 3D widget system for BotchED yesterday before my computer pissed me off. Basically works by intersecting rays (cursor position) with cubes, and rays with planes.
[img]http://dl.dropbox.com/u/3590255/Screenshots/Botch/07Feb2010_001.png[/img]
garry are you making source engine v2?
Nah, no lightmaps :(
I get 8 errors when I try to build for testing, can someone help me identify them? The error list shows them and what line but they look fine to me.
[cpp]#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int userInput 0;
cout << "Welcome to the game of life, were you win or die horribly.\n";
cout << "You are born as a male with the name of Habeeb in the town of Facepunch\n";
cout << "Will you cry or stay quiet like a good child?" << endl;
cout << "Cry\n";
cout << "Stay quiet\n";
cin >> userInput;
if(userInput != Cry && userInput != Stay quiet)
{
cout << "Fucking retard, type out the answer";
system ("PAUSE");
return 0;
}[/cpp]
telling us the errors would be nice. You missed a bracket at the end
userInput = 0;
forgot equals sign.
Wow, lots more errors.
Error 1 error C2143: syntax error : missing ';' before 'constant' Line 7
Error 2 error C2065: 'Cry' : undeclared identifier Line 17
Error 3 error C2065: 'Stay' : undeclared identifier Line 17
Error 4 error C2146: syntax error : missing ')' before identifier 'quiet' Line 17
Error 5 error C2059: syntax error : ')' Line 17
Error 6 error C2065: 'quiet' : undeclared identifier Line 18
Error 7 error C2143: syntax error : missing ';' before '{' Line 18
Error 8 fatal error C1075: end of file found before the left brace '{' Line 23
Well you're using an int for userinput and trying to search a string, you dont search strings like that
half assed reply but im kinda busy and wanted to help ya at the same time :/
[QUOTE=honeybuns;20070786]Well you're using an int for userinput and trying to search a string, you dont search strings like that[/QUOTE]
Can you explain it a little better for a noob like me?
Hmm...
[cpp]
#include <iostream>
#include <string> //include string
using namespace std;
int main()
{
string userInput; //should be string because you're inputting text you could also use char*
cout << "Welcome to the game of life, were you win or die horribly.\n";
cout << "You are born as a male with the name of Habeeb in the town of Facepunch\n";
cout << "Will you cry or stay quiet like a good child?" << endl;
cout << "Cry\n";
cout << "Stay quiet\n";
cin >> userInput;
//use "" for strings and string comparisons
if(userInput != "Cry" || userInput != "Stay quiet")
{
cout << "Fucking retard, type out the answer";
cin.get(); //don't use system("PAUSE");
return 0;
}
cin.get();
return 0;
}
[/cpp]
Now when i run it, it crashes with 'Debug Assertion Failure'
What compiler are you using? What kind of project setting is it?
[QUOTE=MultiPurpose;20071094]What compiler are you using? What kind of project setting is it?[/QUOTE]
Empty project and I use Visual C++ 2008
I don't know what the problem is then. Did you mess with anything?
Sorry, you need to Log In to post a reply to this thread.