• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
iLoveGameProgramming

Forward Declaration of Class breaks the game

21 posts in this topic

Hello, I've been stuck on this for couple hours now. Help would be hugely apprenticed :)
 
I've cut most of the code out to make it less confusing but I'm having problem passing CMainGame class into a MainCharacters class function. If I do forward declaration of the CMainGame it doesn't error out, but crashes as soon as I use one of the functions from the CMainGame.
 

 

// MainCharacter.h ----------------------------------------------------------------------------------------------------------------
#pragma once
 
#include "stdafx.h"
#include <engine.h>
#include "collisionListener.h"
#include "MainGame.h"
 
class CMainGame; //if this forward declaration of class is added it fixes it and game runs with out errors until I run one of the functions from this class in Update function.
 
class CMainCharacter
{
public: 
CMainCharacter(void);
CMainCharacter(CPhysics* Physics, collisionListener* theCollisionListener);
~CMainCharacter(void);
 
void Update(CMainGame* theGame); // error C2061: syntax error : identifier 'CMainGame'
private:
CSprite* MainCharacter;
CSprite* Sword;
};
 
//MainGame.h  ----------------------------------------------------------------------------------------------------------------
#pragma once
#include <engine.h>
#include <Mathematics.h>
#include "Enemy.h"
#include "MainCharacter.h"
#include "collisionListener.h"
 
class CMainGame : public engine, Mathematics
{
public:
 
CMainGame();
~CMainGame(void);
 
virtual void OnDraw();
virtual void OnUpdate();
 
private:
collisionListener* theCollisionListener;
 
CMainCharacter* MainCharacter;
 
};
----------------------------------------------------------------------------------------------
 
Thank you for help in advance!
 
0

Share this post


Link to post
Share on other sites

Did you tried debugging it and see where it crash? My guess is a null or uninitialized pointer somewhere.

2

Share this post


Link to post
Share on other sites

Yep, use the debugger. Is the theGame pointer valid when you call CMainCharacter::Update?

 

Also, you will want a virtual destructor for CMainGame since you have virtual functions in the class.

 

And why is a CMainGame derived from Mathematics? The game IS A Mathematics??? I suspect that is an interface though (i.e. all virtual functions, so the game implements Mathematics), but I can't see any reason why maths functions would be in a virtual interface...

Edited by Paradigm Shifter
2

Share this post


Link to post
Share on other sites

Mathematics class just contains random math functions like getting distance, random numbers etc.

 

Oh I didn't know I need virtual constructor when I have virtual functions. Thank you I'll add that.

 

 

void CMainCharacter::Update(CMainGame* theGame)
{
SDL_GetMouseState(&Mouse_x, &Mouse_y);
 
if (theGame->isKeyHold(SDLK_d)) //breaks on this line
{
....
}
 
I've been dubbging it and the thing is theGame isn't null, it has values. It shouldn't break in that function because I have this in CMainGame's Update function:
 
if (isKeyHold(SDLK_ESCAPE))
{
Quit();
}
 
//and that works 

 

0

Share this post


Link to post
Share on other sites

What value does theGame pointer have? It might be initialized to 0xCDCDCDCD or something like that, in which case, it's not null, but is not pointing to valid memory location either.

Did you make sure those pointers are set to NULL when your program start?

Edited by Vortez
2

Share this post


Link to post
Share on other sites

Is that the same as the 'this' pointer from any of your CMainGame functions while they are executing?

 

How are you calling CMainCharacter::Update?

1

Share this post


Link to post
Share on other sites
They are different pointer values.. What does that mean? I am passing "this"...
 
void CMainGame::OnUpdate()
{
 
MainCharacter->Update(this);
 
if (isKeyHold(SDLK_ESCAPE))
{
Quit();
}
}
0

Share this post


Link to post
Share on other sites

Hmm, I dunno. The this pointer passed to MainCharacter::Update should be the same as the theGame pointer received inside CMainCharacter::Update...

 

Have you tried doing a full rebuild?

1

Share this post


Link to post
Share on other sites

How do you create your CMainGame object? Put a breakpoint in your CMainGame constructor and your CMainGame::OnUpdate funtion and make sure the constructor is called before CMainGame::OnUpdate is called.

 

Looks like you aren't creating the CMainGame object before calling methods on it.

2

Share this post


Link to post
Share on other sites

<twilight zone music playing in the background>

 

0x00000026? that's weird... blink.png

Edited by Vortez
2

Share this post


Link to post
Share on other sites
// This is literally how its created
 
#include "stdafx.h"
#include <engine.h>
#include "MainGame.h"
 
int main(int argc, char* argsv[])
{
 
CMainGame* theGame = new CMainGame();
 
theGame->Run();
 
return 0;
}
 
when I put breakpoints in the constructor and on update, first it breaks at constructor. And everything exists then, it is not null. When I press F5 it goes to the Update and then its 0x0000026 again. Could it be because of this in MainCharacter.h ?
 
 
class CMainGame;
 
class CMainCharacter
{
public: 
 
But when I remove that line I get that:  
error C2061: syntax error : identifier 'CMainGame'
0

Share this post


Link to post
Share on other sites

Just as a curiosity but you do include CMainGame in the cpp file of CMainCharacter right? Because as soon as you want to use any of the function of CMainGame in CMainCharacter it needs to know the type properly so you can't call any function on it in the header.

 

Also are you compiling debug or release, because if you are in release the Camera function and AI update function calls before hand can invalidate the debuggers view of the this pointer, and you will have to inspect the disassembly to see which register is holding the this pointer to obtain valid values for it.

Edited by NightCreature83
1

Share this post


Link to post
Share on other sites

Just as a curiosity but you do include CMainGame in the cpp file of CMainCharacter right? Because as soon as you want to use any of the function of CMainGame in CMainCharacter it needs to know the type properly so you can't call any function on it in the header.

No CMainGame is included in .h file, and CMainCharacter .cpp only includes CMainCharacter.h. Ive just added it to cpp and still does the same thing
 
Also I was compiling in Release mode, my bad :(. In debug mode still crashing. The pointers in Update are working correctly in Debug mode... So it is not null or 0x000026... The constructor, update in main game and update in main character, the pointer values are all the same.
 
I'm not giving up until this is going to work... <twilight zone music playing in the background> 
that music doesn't help either hehe
0

Share this post


Link to post
Share on other sites

basically all I am trying to do is add controls functions to the MainCharacter class. I might be doing it wrong but in my engine I just inherit controls class into the main engine class. Ill just stop inheriting it and create it manually in my game, and pass that into the MainCharacter class.

0

Share this post


Link to post
Share on other sites

I have just managed to fix it :) Thank you all for help. Problem wasn't with controls but with something else in my class :x Something very silly

0

Share this post


Link to post
Share on other sites

Could you explain what the problem was so that people looking at the same problem in the future can find the solution?

 

 

 

I've rewritten controls a little bit but this was the problem: 

 

CMainCharacter::CMainCharacter(CPhysics* Physics, collisionListener* theCollisionListener, controls* passedControls) 
{
theControls = passedControls;

theCollisionListener = theCollisionListener;

....

 

theCollisionListener was set to itself instead of 

this->theCollisionListener theCollisionListener;

 

Then it was breaking on 

 

if (theControls->isKeyHold(SDLK_a))  ///this line!

{
if (!theCollisionListener->GetAllowJump()) //but actual problem was on this line since this was null....
{
 
so it completely threw me off, and I was looking for the problem with controls...
 
Over 7 hours wasted trying to solve this stupid silly mistake!
Edited by iLoveGameProgramming
0

Share this post


Link to post
Share on other sites

Don't name your parameters the same thing as your class members then.

 

If you don't like m_thing, mThing, thing_ etc. you could use theListener for the member and aListener for the argument (parameter) to a function.

Edited by Paradigm Shifter
0

Share this post


Link to post
Share on other sites

And stop debugging in release mode as it has all kinds of optimisations on and shuffles the code around quite a bit to make it faster or smaller executable size. This means that the crash you had doesn't necessarily have to happen on the line that the debugger is indicating. Looking at the callstack becomes important then to figure out where it was coming from and what it was supposed to do.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0