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 / undeclared variable + InKey$()

Author
Message
CocaCola
14
Years of Service
User Offline
Joined: 23rd Mar 2010
Location: CocaCola.x CocaCola.y CocaCola.z
Posted: 23rd Sep 2010 05:37 Edited at: 23rd Sep 2010 05:41
Pretty simple question, why doesn't my code work? It shows 7 errors, the inkeys and the x,y,z variable.


Instead of dbInKey$ does dbKeyState work?? if so how does it, like

???

I want coke, not Pepsi!
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 23rd Sep 2010 06:10
You didn't declare x,y or z, and those aren't valid if statements. Follow some tutorials on variables and control structures to see where you went wrong.

CocaCola
14
Years of Service
User Offline
Joined: 23rd Mar 2010
Location: CocaCola.x CocaCola.y CocaCola.z
Posted: 23rd Sep 2010 06:28
Quote: "You didn't declare x,y or z, and those aren't valid if statements. Follow some tutorials on variables and control structures to see where you went wrong."

Thats kinda why I posted here to get help from you guys, anyway where could I find these tutorials? link?

I want coke, not Pepsi!
Dodga
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 23rd Sep 2010 06:44
It's dbKeyState, and you dont use a single =, you use two == to check if it equals, = is only to assign a value to a variable. Also your if statements should be in this format:

if (variable==6)
{
code to do

}

Also in c++ you cant just declare a variable like d=9;
You have to declare the variable type as like

int i=9;

or char b="b";

I would google "c++ tutorials" and go through a set of tutorials, you have the syntax all wrong, you can tell you've got darkbasic experience, but c++ is completely different.
CocaCola
14
Years of Service
User Offline
Joined: 23rd Mar 2010
Location: CocaCola.x CocaCola.y CocaCola.z
Posted: 23rd Sep 2010 07:10
so it would be something like
float dbObjectPositionX(1)=x;
??
does float work??
also I was never using anything like if (variable==6) I was simply going to use that to position the camera. so like dbPositionCamera(0,x,y,z)
also

thats correct?

I want coke, not Pepsi!
Dodga
14
Years of Service
User Offline
Joined: 12th Dec 2009
Location:
Posted: 23rd Sep 2010 07:40
nope, it would still be if(dbKeyState()==37) { code here }

and yes doing float would work. There is no ; after the if statement.
CocaCola
14
Years of Service
User Offline
Joined: 23rd Mar 2010
Location: CocaCola.x CocaCola.y CocaCola.z
Posted: 23rd Sep 2010 08:07 Edited at: 23rd Sep 2010 08:07
so

would that work??

and this is fine how it is?


I want coke, not Pepsi!
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 23rd Sep 2010 08:40 Edited at: 23rd Sep 2010 09:04
No. For dbKeyState you have to give the scan code of the character as a parameter and it returns 0 or 1 as a result. For example:



For scan codes, check the dinput.h file of the DirectX SDK, that contains pre-defined constants for key values, so for example, for letter V you could use (if you include dinput.h or copy the constants from there):



or even:



Your second statement is wrong because it's trying to assign a value to a function, not to the variable. The correct way is:



And dbInKey does not have a dollar sign at the end. Some pages of the Dark GDK help were just plain copied from the Dark Basic Pro help where a dollar sign is used, but it is never used in C++.

For learning C++ syntax:

http://www.cplusplus.com/doc/tutorial/

http://www.learncpp.com

http://msdn.microsoft.com/en-us/beginner/cc305129.aspx

(At the present moment my browser can't access the learncpp.com but I still include the link, maybe it's just a temporary problem, I used to read that site. The other two are working.)

EDIT, P.S. If you want to check several keys, another way is to use the dbScanCode() function which returns the code of the key currently pressed, and then write a switch statement on the value. That would result in more elegant code, but dbKeyState works as well.
Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: <script> alert(1); </script>
Posted: 23rd Sep 2010 15:12
cplusplus is very good, i highly recommend it, you must study c++ before you start with creating games, it would really help you with alot of things, if you have very little knowledge of c++, you might not know how your program's structure should look like, but if you study it, you will have several structures in your mind so you can choose the best, this will make your code way cleaner and more functional, plus, if you start without c++ knowledge, you might do some mistakes that doesn't trigger an error when you compile, but it will do something unexpected, for example, the "if" statement, for beginners, it is a very common mistake to use "=" instead of "==", which makes alot of trouble, for example


this will set the value of x to 9, the if statement checks whether the value is true or false (false = 0), so this will check if x (which is 9, as you wrote) is true, and since it's not 0 it's true, thus this statement is always true, but if you use the "==" instead of "=", it will do what you expected, it will check whether x is 9 or not

CocaCola
14
Years of Service
User Offline
Joined: 23rd Mar 2010
Location: CocaCola.x CocaCola.y CocaCola.z
Posted: 23rd Sep 2010 17:23
Ok, thanks everyone im making notes of all this for later use.
And would this work?
if (dbInKey(w) == 1) { code }
???

I want coke, not Pepsi!
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 24th Sep 2010 08:53 Edited at: 24th Sep 2010 08:57
Again no, for several reasons.

1. If you check the help for dbInKey, you can see that the parameter is "void", meaning that it does not expect any parameters, so you cannot put "w" inside the brackets. Although the help is not always good but at least it tells you the parameters and return values of functions and explains what they are. Also, the Visual Studio "Intellisense" helps you in this regard, meaning that if you type a function name and the first bracket, a pop-up text will show you the expected parameters and also the type of the return value of the function. If a function has several parameters, the pop-up even indicates with bold text which of them you are typing at the moment.

2. In any case, writing just w without any quotation marks or apostrophes is not accepted. You must write either a string with double quotation marks "w" or "wwww" or if it is a single character, then apostrophes 'w'. Just w would be an (undeclared) variable name.

3. The return value of dbInKey is a char pointer, not an integer, so you cannot compare it with 1. (More exactly, you might be able to but the result doesn't have any sense at all.)

4. Before you ask, this won't work either:



This might be your next attempt but in this case, you would be comparing the pointers of two strings, not the strings themselves. For comparing strings, you could use strcmp, or an index to compare only the first character of the returned string with the single character 'w'. However, there is still one problem left if you do this: Dark GDK functions which return char pointers like to create memory leaks because they allocate a new string which never gets deleted, so you must take care of storing the pointer and using delete[] on the string when it's no longer needed.

I once wrote a character input function, using either dbInKey or dbEntry, I will look for it and post it again if you are interested. That is useful if you want to handle longer strings typed in by the user (for example, "type in your name"). But if you only want to get single keypresses to control the game, then stay with scan codes, they are much easier and faster to handle. (Besides, scan codes are usually used for game control because they indicate the position of the key on the keyboard, not the letter painted on it. That can present an additional problems of course, since "press Z" would not necessarily be Z on a keyboard of another language, but at least the position of the player's fingers on the keyboard will be the same.)

Login to post a reply

Server time is: 2024-07-02 08:21:35
Your offset time is: 2024-07-02 08:21:35