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.

Dark GDK / Why does this go back to the previous Menu screen?

Author
Message
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 17th Feb 2012 05:03
Hi all,

Ok, I'll try to explain this as best I can... So, I have a main menu screen which goes into the main game fine. A keystate allows you to create a character name from this. However, when the character name reaches 8 letters, it reverts back to the main menu screen. It use to work fine and I had it up to 9 without issues. Obviously something changed in my code and I cannot see the error now. Would someone be able to check this and tell me why it occurs?

Just a warning, it is a length of code, mostly repetitive though...

The Naming Function and long part


This is the GameTitle:


My Main Game Code.

I Have removed all non-relevant parts of the code and left the title into the main game and only the naming function.

Any help on this matter would be great. The array for the names is also set large enough: ChtrName[6][8];

Sorry about the lengthy code, if anyone knows how to achieve the same by shortening it, by all means, please let me know! Thanks all!
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 17th Feb 2012 11:19
Umm your control variable for the length of a name is nx as far as i can tell from a quick review. It is set to never go above eight. Also, in your default naming function you are calling strcpy to put the default characters into the array but that is not correct. You should define it as so:



That will avoid the additional overhead of the function call to strcpy, though this is negligible on a fast machine and in menu code. But, if you did this with a name that was say eight characters you could end up overwriting the first character of the next name, or worse, some random location in memory, with a '\0'. Lastly, you say at the end of the function that ChrName is defined as a char[][] but you never refer to it as such in the code. Actually your code is kinda hard to follow because it is so lengthy. Consider adding comments at the head of each of the major blocks that tell us what they are supposed to do so that we know what we are looking for. The answer will most likely become clearer if we understand exactly what is going on. Comments don't write themselves and it's good coding practice! If you are a relatively new coder, learn good habits early. If you are a veteran coder, I apologize for talking down to you but shame, shame for lack of comments!! To me it looks like a control variable issue from just looking at it. I will plug it in and let you know what I come up with. If you want the names to be longer than a certain length you are going to have to come up with dynamic name lengths of some sort which can get pretty complicated in c++. Best idea for that is to use a vector of strings which is standard and safer I will get back to you asap, let me know what you come up with!
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 18th Feb 2012 00:34
Hey, thanks for the response. As for the ChrLetters[][], it doesn't seem to work that way, as I always get an error message in compiling it stating that it cannot convert from char to char[1] or something of the sort.

I'm not overly new to programming, though I am very new to programming in c++ and using DarkGDK. So, a lot of this stuff is new to me. Sorry about the lack of comments. I'll add some more and resend it.

I am not familiar with how to use vectors unfortunately. Also, almost all variables are externs, I don't know if that will slow down things, or not.

Anyways, back to the issue at hand. It use to work fine, and I would be able to get the 8 characters per name. However, now as soon as I put in the 8th character, that is when it goes back to the main menu. If you wish, I can send the FULL code, which has LOTS of irrelevant parts in it, and is quite lengthy due to other things being made already.

I have also expanded the array to hold 13 and even 40 characters, but no matter what as soon as it hits 8, it goes back to the first opening screen I created. Let me know if you want it all for a more full understanding, and I'll try to comment more on it. THANKS A LOT FOR ANY HELP, this is driving me insane.
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 18th Feb 2012 21:53 Edited at: 18th Feb 2012 22:03
I cannot be sure of anything in there because of some syntax, but I think on the one variable check you are skipping the next read if it is above eight:



The vector and string classes are very easy to use. To declare a vector, just #include <vector> and then:



They are much easier to use and all memory allocation is handled directly by the standard classes. The only issue that I see is that it adds a bit of code to your executable since std::string and std::vector take up alot of room in the runtime library. For a game though, this issue is, imho, negligible. Go ahead and get your comments in place and I will take another look. Also, if you could, try to indent your braces in some kind of standard way Lol for someone who is used to being obsessive about braces I have to copy and paste your code then spend quite a bit of time formatting. I don't mind this I am just trying to encourage good formatting practices Sorry if I sound like a jerk it just makes it easier for us.

Edit: you said that you wanted a shorter way to write those lines that assign your letters based on keypress. One thing you can do is eliminate block statements where you can (blocks are { }) by eliminating braces for single-line if-else statements, for loops, and while loops. For instance, these two are the same:



Also, try making a lookup-table for your character keys. For instance, if scancode 63 = 'a', 64 = 'b', and 68 = 'c':



Eventually you will do anything you can to shorten code as C++ tends to be very long-winded
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 20th Feb 2012 20:36
Hey again, thanks for the reply. I am unsure what is truly wrong with my indentation, I tried to fix up some of the few errors where I found them. I have looked up some of it online to see and they don't really say much other than what I have. Anyways, no you don't sound like a jerk, and I would truly like to do it to a standard format for ease of use for you guys to be able to help so I don't need to hear this again haha.

Anyways, if you have gotten a syntax error, I am assuming you do not have Cloggy's D3D plugin. It's a text plugin which I found speeds up text a lot. So, I've been using that. Anyways, here is the complete code, sorry about length again, but I thought it would be best to get a full working code ( may cut out the parts not relevant though, ie: inventory and such ), just anything with d3d would have to be made into dbText(). If you would prefer I do that, I can work on that a bit more. I commented as much as I could to give you a full understanding of it.

I think there is a slight misunderstanding on how the name is put in. It is not done by keypress so much as using W,A,S,D to move a cursor, and K to select the letter, and L to go back, if that makes sense. I only did this because I plan to implement the use of PC controllers, as I did for another game I worked on a bit using DBP. Anyways.... here is what I have thus far, and again.... very very sorry for the length. It's an RPG, so it has a lot of stuff in there.

THANKS SO MUCH FOR ANY HELP YOU CAN GIVE, I still try to work on this EVERY day, but fail miserably at getting it past 7 letters without crashing. I have set the array and acceptance criteria higher to see, and yet again it goes back to the title screen.



On the title screen press enter, then after that, 'j', and 'k' to enter the selected letter. 'w','a',s','d' to move cursor around. when nx = 8 it crashes.

Here is the globals header:


again, thanks for any help.
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 20th Feb 2012 21:42 Edited at: 20th Feb 2012 22:00
Wait wait wait:

Quote: "
when the character name reaches 8 letters, it reverts back to the main menu screen.
"


VS.

Quote: "
when nx = 8 it crashes.
"


I'm sorry lol does it crash or does it go back to the menu screen? If you are in the debugger do you get a breakpoint? Now that I have all the code I will copy it and take a look at the behavior and let you know

Edit: can you hand me the link to that text library so I can test? Thanx!


Edit Edit: Hey i found that library but I'm not going to mess with it. I guess you have to compile it? I don't feel like messing with adding new libraries so I will just look over what you have. I don't think that the d3d functions are the problem anyways, so I will try to parse through it without them.
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 20th Feb 2012 22:56
as soon as the 8th letter is added it reverts back to the main menu screen, which is the oddest thing to me because there is only 1 place where i have anything to bring it back there. anyways, sorry for the confusion and my shabby explanation. From what I have found, it adds the 8th letter, then it does not seem to reach to the main game loop where it call for the name character function again, it goes back to the main menu in there, which to me sounds like the problem lies directly in that name character function. But, I could be very well wrong. I have this feeling because I have 2 places where it prints the character name, once in the character name function and the other in the main loop. When you put the 8th letter in, it appears in the character name function text, though, the other remains with only 7 letters, then it quickly goes back to the main menu.

ALSO, I forgot to mention.... the screen goes from full screen to about 3/4 of a screen. However, it does not show that it was resised down. I found this only added to the confusion, and will probably confuse you more.

Sorry if everything sounds a little off. I am trying to explain it as best I can.. Thanks, again
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 20th Feb 2012 23:26 Edited at: 20th Feb 2012 23:28
Again in the loop (AcceptName < 2 && nx < 9) you use the code:



You do not want to do this! I am assuming that ChrLetters is the name while you are still adding characters to it? If so, here is how to declare it and use it:



Otherwise, the way that you have it written here is what is happening:



What is happening is that each position within ChrLetters is one byte long (or one char long if you are using unicode they are words) and when you use strcpy you are actually doing the equivalent of the following:



Since that is an invalid index it can point anywhere in memory. During the first few copies since ChrLetters is most likely contigious block of memory you are just overwriting the next character in the name with a '\0'. When you get to the end, you are overwriting the next location in memory!!!! Add a variable watch on InventoryItems and check inventoryitems[0][0][0]. You will most likely find a null character '\0' there. To test this, initialize the location to a known value at the start of your code. If the value is changed after your name function, you know this is the case. If not, then the character is going to some other location in memory, which is bad :/ I am not sure why you are using strcpy like this other than the fact that someone may have said you have to to work with arrays??? BTW I dont think this is causing your problem but it will be a headache later! Remember, C++ will NOT tell you when you are outside of the bounds of an array. The only thing that will happen is Windows will complain if you write to a protected location. If not, you can write to wherever you want, especially if your program owns the memory you are writing to. This is why I recommended a vector of strings. They are much more simple and safe!!! Look into this approach please!!!!
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 20th Feb 2012 23:56
Hey, I have originally tried the ChrLetter[nx] = 'A';, however, I continue to get an error stating it cannot convert from char to char[1] or something. Is there something I am doing wrong with this? I will look into that more and see what I can do... however, this code use to work perfectly, until recently when I think I may have deleted something within it, or something else is conflicting, but I cannot see where. I will try what you mention and see what happens
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 21st Feb 2012 16:53
Yes, what you are doing wrong is declaring the name as multidimensional. Also, when you reference the insertion of the character, you have to use single quotes, not double quotes. Single quotes denote characters, double quotes denote strings. Make sure you declare the ChrLetters array as one dimensional (ChrLetters[sz]) and use single quotes when inserting characters. Is it possible for you to send me a copy of the executable so that I don't have to build my own? It would help me greatly to see exactly what your code is doing but I can't get that library you told me about to build for some reason :/
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 22nd Feb 2012 04:28
Hey, thanks for the info, I will try hard to fix up that array issue. As for the reason I am using arrays, is so that first I may store them into a save file, and secondly so that Character[0] is the first, and Character[1] would hold the second characters name, this way I found it easier to cycle through the names with less code... but, I'm still new to C++ and a lot of coding in general, so, I can be wrong (most likely am) haha. Anyways, here is the .exe for the game. I changed the fonts so that you should be able to use it without having the proper game fonts yet.

I will get on fixing the arrays right away, thanks again, and I hope this helps you to understand what I mean. At the title screen you need to press 'enter' then after that its 'j' to enter character naming, and 'k' to accept a letter... well, i think I explain it in the game. Let me know if you can't open it.

Attachments

Login to view attachments
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 22nd Feb 2012 04:57 Edited at: 22nd Feb 2012 04:58
Again, strings will make storing the names much easier in that regard as well, because when using the c++ stream classes the string data will be written directly into the file. Faster and safer than the DGDK file functions! Now that I know what you are doing I will try to come up with an example routine for you that you can work from, ok?

Edit: Just so you know it may take me a few days. I haven't had much game development time lately because of school sorry.
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 22nd Feb 2012 05:03
that's quite ok! you are helping more than I could hope for! I appreciate it all.
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 22nd Feb 2012 06:52 Edited at: 22nd Feb 2012 18:00
LOL i have most of the code done. I will try to finish it tomorrow and then send you what I have. Feel free to use it however you want; I'm not picky

Edit: Ok, I am going to upload the executable as it stands thus far. Tell me if this is what you are looking for. Right now, I have the maximum name length set to 256 but you can modify this quite easily through a preprocessor definition.

Attachments

Login to view attachments
TheViking
12
Years of Service
User Offline
Joined: 16th Feb 2012
Location:
Posted: 23rd Feb 2012 01:20
Yes, that is exactly what I was looking for.
xix psycho xix
16
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 23rd Feb 2012 11:56
Ok, here is the source for the executable. I added a menu interface as well so you could see how I usually accomplish this. I would assume that you know how to download and unzip projects. Run it, study it, debug it, whatever helps. It isn't perfect, you need to add some fancification code lol to make it look nicer, but it should be fine. After you run the code, check out the running directory. You should see the file "test.txt" and this will have your usernames inside. Check out that file too. Good luck and let me know if you need help with any of the code.

Attachments

Login to view attachments

Login to post a reply

Server time is: 2024-11-23 00:17:02
Your offset time is: 2024-11-23 00:17:02