Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Tier 2 AGK object instantiation help required. (I just can't figure it out!)

Author
Message
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 28th Dec 2015 18:54 Edited at: 28th Dec 2015 18:56
I think the title pretty much sums my issue up. I'm trying to learn tier 2 as I want to create a OO program, (And I have to use c++ instead of Java which I know) and I just can't seem to figure out how to create a new object.

Using the example template, I created a new class called Screen, the constructor sets up some default values, and I just want to display them in the app::loop section.

Here's Screen.h


Here's an extract of the cpp file:


And here's the main part.


Any ideas what I'm doing wrong because for the life of me I can't figure it out..
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 28th Dec 2015 19:02 Edited at: 28th Dec 2015 19:07
Your object "screen" is being created on the stack. Once the scope is changed anything allocated on the stack within that scope is destroyed/deallocated (in this case "screen" is destroyed when leaving the app::Begin function). Add a "Screen *screen" member to your app class header. In the app::Begin method change "Screen screen( "Test" );" to "screen = new Screen("Test");" and then in the app::End method add "delete screen;". This will allocate the screen object on the heap and keep it alive for the duration of the application (though it's best to "delete"/free memory right after you're done using it).


EDIT: It shouldn't even be able to compile right now, because the compiler won't find the "screen" object within the scope of app::Loop.

Sean
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 28th Dec 2015 21:34
Similar to what Hockeykid said I like to make a separate class for the game itself (let's say MyGame), and manage any of its assets within that class. I would have a Screen* in the MyGame class, and instantiate the pointer later. Going from Java to C++ can be a challenge, because you go from managed to very much not managed.
-mrradd-
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 28th Dec 2015 22:44
Quote: "Similar to what Hockeykid said I like to make a separate class for the game itself (let's say MyGame), and manage any of its assets within that class. I would have a Screen* in the MyGame class, and instantiate the pointer later. Going from Java to C++ can be a challenge, because you go from managed to very much not managed."


Yes, that's another way to do it that keeps things a bit more organized. I think one of the biggest issues I have with AGK's tier 2 library is that it doesn't really encourage OOP (everything is a function call) and lacks an "app management system." I had expected that someone would make an open source project by now to make an "app management system" for AGK's library. By "app management system" I mean a hierarchy of classes that help handle your app like "Scene" objects having "nodes" and nodes being different game objects like sprites, particles, text, etc. From there the scene handles the allocation and deallocation of all of its child nodes. The Scene objects could have a hierarchical parent that manages them too (from there loading a scene, adding scene transitions, etc would become much easier).



Sean
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 28th Dec 2015 23:23

@Mobiius Instead of normal pointers look into unique_ptr with C++11 (VS 2015), or, if you have to, auto_ptr for non C++11 compilers. It affords you some of the managed capabilities as Java, and helps prevent some of the awesome side effects that managing pointers can have if done manually. There is also something call shared_ptr, but I'm not so sure how that behaves. Also stay away from #pragma if you can. Instead use #ifndef STUFF_H #define STUFF_H #endif. #pragma is windows only. If you were to port your stuff somewhere else, like Android Studio or Linux, it wouldn't work (someone correct me if I'm wrong here).

@Hockeykid
Quote: "...I think one of the biggest issues I have with AGK's tier 2 library is that it doesn't really encourage OOP (everything is a function call) and lacks an "app management system." I had expected that someone would make an open source project by now to make an "app management system" for AGK's library. By "app management system" I mean a hierarchy of classes that help handle your app like "Scene" objects having "nodes" and nodes being different game objects like sprites, particles, text, etc. From there the scene handles the allocation and deallocation of all of its child nodes. The Scene objects could have a hierarchical parent that manages them too (from there loading a scene, adding scene transitions, etc would become much easier)."

I actually thought the same exact thing, and was actually working on something very similar to what you described not too long ago. I haven't really gotten far at all with it do to my constantly saying to my self, "Oh, I can do that better!" and refactoring everything. I actually put it aside and just started using BASIC since I wanted to get to market fast haha. I'm all for coding everything up myself (kind of why I like AGK), but having a standardized way would be nice.

Sorry to piggy back this thread, but have either of you done anything with the new Android Studio projects? I can't get the template to work with the emulator, or my Note 2, but the interpreter is fine... https://forum.thegamecreators.com/thread/216124
-mrradd-
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 29th Dec 2015 13:44 Edited at: 29th Dec 2015 13:46
Thanks for your help, but I'm still non-functioning.

I now have this in my header:


And my main method contains this..


screen is still undefined.. Do I have to use namespace Screen? (Namespace Screen doesn't work anyway..)
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 29th Dec 2015 14:13
When using pointers you can't use the dot operator for calling methods and data members. You need to use ->. So it would look like screen->someMethod(). Try that out.
-mrradd-
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 29th Dec 2015 14:17
Thanks, but my issue is that the screen object is undefined. It can't see screen to start with.
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 29th Dec 2015 14:35
I just noticed that screen is defined inside the Screen class, so app still doesn't know about it outside of Begin(). Did you mean for Screen to have a Screen pointer? Try defining a Screen object (or pointer) data member inside the app class if you aren't already doing that.

-mrradd-
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 29th Dec 2015 14:56
To be honest, I don't know what I want. lol

I just want to instantiate a new unique screen object. Eventually, there will be a list of screen objects, and each screen can have multiple element object within them. I'm basically converting my tier 1 UI engine to tier 2.
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 29th Dec 2015 15:26 Edited at: 29th Dec 2015 15:28
@Mobiius
Quote: "To be honest, I don't know what I want. lol"

I feel you on that one haha.

What I like to do is this, it works for me anyway.


Ususally I am not a huge fan of looking at inline code, but for this example it doesn't matter.
-mrradd-
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 30th Dec 2015 20:08
Thanks, I'll give it a try after the match, seeing as it is my birthday!
Hockeykid
DBPro Tool Maker
16
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 30th Dec 2015 22:38
You should be defining screen ("Screen *screen") inside of the "app" class header. not in the "Screen" class.


Sean

Login to post a reply

Server time is: 2024-05-06 22:26:55
Your offset time is: 2024-05-06 22:26:55