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 / New to DGDK (and some parts of C++), having trouble with variables

Author
Message
Yodaman Jer
User Banned
Posted: 4th Feb 2011 05:49
Hey guys,

I'm fairly new to C++ (just started seriously looking into it a couple of months ago), and I thought that using DGDK would be a good way to get even more serious with it. I seem to be having trouble with variables. Variables! Something as simple as that.

Here's my code...


Here's the part I'm having trouble with:


What I want to do is make it so that if the user right-clicks anywhere in the program, a "menu" appears at the old mouse positions, but I can only achieve this if the user holds the mouse button down. I thought that by adding the variable "bActive" would fix this, but I encountered an error that said "bActive is not declared in this scope", and I found out the only way I could use it was if I included it in the dbMouseClick==2 code block, but that totally defeats the purpose of even creating it and it doesn't even work!

I have no idea how to make variables global. I tried what's outlined in this page but it didn't work. What am I doing wrong? What do I need to do?

Thanks for any and all help,
-CoffeeCoder

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Feb 2011 06:29
If you declare a variable within a scope { }, then it will only exist within it, so you'll probably want to put the declaration for 'bActive' in either the global scope, or the first non-global scope, which is 'void DarkGDK ( void ){ -> here <- }'. Can you explain why the tutorial page's code didn't work? And make sure you weren't declaring the variable multiple times, i.e. 'int xyz;' and 'int xyz;' in a different scope, because they will be treated as different variables.

Yodaman Jer
User Banned
Posted: 4th Feb 2011 06:52 Edited at: 4th Feb 2011 06:56
Thanks dark coder, that fixed it!

Quote: "Can you explain why the tutorial page's code didn't work?"


I'm not sure. I didn't save the code since it didn't work (I should've just to study it over and learn from it, but I was frustrated so didn't). It just kept telling me that the variables hadn't been declared in that scope (even though I typed them up in a global.h file) and it just wouldn't compile. I think I did declare a few of the same variables in different scopes, so that may have been the cause.

Thanks for the quick reply. I'll get the hang of C++, it's just such a drastic switch from DBPro (as I'm sure you can imagine!). I already am in love with DGDK though, because it isn't giving me a crapton of errors like DBP does on Windows 7. No "Failed to AddCPicker::File" error yet.

C++ is very exciting too. I understand the basic principles of OOP and am looking forward to utilizing it for real in my future programs.

Here's my fixed code. I'm drawing things very sloppily at the moment, but I'm going to ix that with UDTs in the future as that will be easier and cleaner. This was just a quick n' dirty test.



EDIT: Forgot to make sure the menu text was in the center of the menu. Quickly fixed that.

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 4th Feb 2011 08:25
Looks better, just make sure you give the variables default values, else they'll be random.

Yodaman Jer
User Banned
Posted: 4th Feb 2011 20:32
Thanks dark coder, I will heed that advice.

Now I seem to be having trouble with types and structs. Anytime I try to declare a type to hold information for gadget positions like this...



...the program doesn't finish compiling. I also can't seem to reference it like this:



Google's not helping much either. Here's the error report I get within VC++ 2008:


All my code:



Can anybody help me out please? I think BASIC really screwed with mt here. C++ is so vastly different!

Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 4th Feb 2011 20:43 Edited at: 4th Feb 2011 20:44
you cannot set the default values of struct/class members like this, i believe the common way to do it is to set default values in the constructor, for example:


the constructor is called every time a new object was created of this type, for example, your button1 = new Gadget;

you can read more about constructors, just search google

Bran flakes91093
15
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 4th Feb 2011 21:03
Quote: "struct Gadget {
int x=0;
int y=0;
int w=0;
int h=0;
bool Visible=0;
}"


Errors 1-5: You can't define variables inside of a class or struct unless you make them static const. Use a constructor to initialize values.


Error 6: Structs and classes need semicolons after the last brace. (Fixed in the above code block).

Quote: "int button1=new Gadget;
button1.x=5;"


Error 7: new and delete are used to allocated and free memory on the heap, which does not go out of scope. These operators are to be used on pointers (declared with '*' symbol and dereferenced with "->"). However, what you want is to just create the variable on the stack, like any other variable. So remove everything after "button1" (unless you want to explicitly call a constructor other than the default: the default will be called automatically on creation).

Dynamic Memory

Structures

Fixed code:


“C++ : Where friends have access to your private members.”
-Gavin Russell Baker
Yodaman Jer
User Banned
Posted: 4th Feb 2011 21:29
@Bran:

I still get errors after compiling your code. Log:


I added a semicolon after the two braces statements and it fixed the error with the struct (I like that style btw!), but now it says this:




What does this mean?


Thanks for the help so far guys! I really appreciate it.

Bran flakes91093
15
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 4th Feb 2011 23:10
Oh I forgot to add the semicolon the second time.

The other error is because you need to declare button1 as type Gadget, not int.



Sorry, I didn't have time to test what I gave you.

“C++ : Where friends have access to your private members.”
-Gavin Russell Baker
Yodaman Jer
User Banned
Posted: 6th Feb 2011 00:07
Thanks Bran, that now works!

I have another question.

Can I not use the std namespace with DarkGDK? I tried using it with the <string> header in order to make my buttons have text on them (as, you know, that would be useful! ), but I keep getting compiling errors. For some reason it doesn't seem to like the std namespace, but I can't use strings without it, and as far as I know there's no other way to to hold text information in DGDK, is there?

Bran flakes91093
15
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 6th Feb 2011 03:53
http://forum.thegamecreators.com/?m=forum_view&t=155298&b=22

“C++ : Where friends have access to your private members.”
-Gavin Russell Baker

Login to post a reply

Server time is: 2024-06-23 03:17:39
Your offset time is: 2024-06-23 03:17:39