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.

Newcomers DBPro Corner / Number only for Input command?

Author
Message
Anti Human
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Good ol\' West Virginia
Posted: 15th Apr 2010 09:25 Edited at: 15th Apr 2010 09:46
I searched 'Input' but only found one post about limiting the command to numbers. It sounded like the problem was figured out but I couldn't see where.

[href]null[/href]http://forum.thegamecreators.com/?m=forum_view&t=113617&b=7

all I want to is limit user input after the command to a number, or at the least toss out an error message if a letter or puncutation is typed and reset the question.



I have made it so the user can't add more points to P_Str than they have points (Skpt). but if the user enters a letter into the input it adds nothing and moves on (i assume because the varible is "P_SI" and not "P_SI$")

(edit) I'm going to play around with the "Repeat/Unti" command. might find an answer in that.

Today is a good day, at least compared to tomorrow.
And every day is a good day to die, because then, there is no tomorrow
luskos
17
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 16th Apr 2010 22:17 Edited at: 16th Apr 2010 22:30
Try this if it`s helpful.With my current skills i managed to make it work with positive numbers only, no zero, no negative, no letters or special symbols.



EDIT:I`ve noticed that it actually worked with floats and altered the code, now it works with float inputs but they do not take effect on calculated result as before.

Where there is a will, there is a way.
I often edit my posts, that`s who i am
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 17th Apr 2010 00:54 Edited at: 18th Apr 2010 14:43
To do it properly, you can't use Input as whatever the user types is passed as a whole entity only when the Enter key is pressed.

Using Inkey$(), Entry$() or ScanCode() on the other hand, you can read in characters one at a time and filter out the non-numeric characters you don't want.

But, you also have to process manually all the other keypresses you need - like the Enter, backspace and cursor keys. But, on the plus side, you can make yourself a cursor which you don't normally get.

Here's the basic version of a number input function I wrote a while back and then modified for Dark Sprites:



Notes On Using The Function:

1. The function returns a string. Use VAL() to convert it if you need it in a proper numeric variable.

2. The first parameter allows you to pass a 'current' or 'default' value to the function. The above example uses "30" so that is what appears by default and the user can edit it or just press Enter to use it.

3. Parameters 2 and 3 are the screen X and Y position you want the input box to appear. (The function was designed to work on a non-black screen, so if required the RGB values can be altered in the function).

4. Parameters 4 and 5 are the width and height of the text box. If you look at Dark Sprites, you will see how they are used, but you may not have a use for these parameters.

5. The last parameter is a maxvalue value where you can stipulate the maximum number the user can type in. If the number they type in exceeds this value, what they typed will be replaced with the value you enter for this parameter.

Hope it's of some use for you. If it is, by way of a thank you, please promise to never use Goto again!!

TDK

Anti Human
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Good ol\' West Virginia
Posted: 18th Apr 2010 04:02
firstly, thank you both for the help. sorry for the late reply, at parents house with limited bandwith. my systems not on net and only medium is to burn a CD so it will be longer still before i can try either of these.

and i am trying to break away from the 'goto' command, but it's just so darn easy (and i'm still learning alternetives).

both idea's seem over-whelming to me ATM. it will take some time but i'l try to get through both code snips.

that said, i should have explained earlier. it's just a text based 'almost-RPG' thing for now.

The enter and backspace keys would be needed; and so would zero. P_Str is a base stat, i'm trying to allow the user to add free skill points to it.

Today is a good day, at least compared to tomorrow.
And every day is a good day to die, because then, there is no tomorrow
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 18th Apr 2010 14:43
Quote: "both idea's seem over-whelming to me ATM"


Mine is just a function. Simply copy and paste it in at the end of your program and whenever you need to enter a number in your program just use the line:

Entered$ = InputNum("30",100,100,100,16,999)

...using whatever 6 values you need in the brackets. (What each one does is covered in my post).

After the user has pressed the enter key, whatever number they typed in is sitting in the string variable Entered$ (or whatever variable you chose to use).

TDK

Anti Human
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Good ol\' West Virginia
Posted: 18th Apr 2010 23:06
does sound interesting, even if the length the of code is daunting. once i'm back at a friends house and my system is on net i'l check that out.

Today is a good day, at least compared to tomorrow.
And every day is a good day to die, because then, there is no tomorrow
Anti Human
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Good ol\' West Virginia
Posted: 22nd Apr 2010 10:50
well, i finally made it here and now have my system on the net. acouple days ago i woke up and started hitting myself in the head for being a moron; realized my own solution, i'd been looking at the problem from the wrong end, took about 3 seconds to rewrite. i was trying to exclude all but numbers, i should have been trying to include only numbers.

i will look over both idea's, not only might they help in current code but also for future knowlegde; but i'm so excited about having my largest bug squished that right now i'm trying to focus on coding.

I do have a question though, directed at TDK, but any one with ideas, by all means please inform.

what are the alternatives to 'GoTo'? currently i have none in my code, but since how 'GoSub' seems to amount to the same thing (and I do have quite a few of those) i'm thinking my current code could give some one else a head ache

Today is a good day, at least compared to tomorrow.
And every day is a good day to die, because then, there is no tomorrow
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 22nd Apr 2010 16:39
Quote: "what are the alternatives to 'GoTo'? currently i have none in my code, but since how 'GoSub' seems to amount to the same thing."


Actually, you couldn't be more wrong!

Gosub is a conditional jump, whereas Goto is unconditional. All is explained here (See number 18):

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

TDK

veltro
User Banned
Posted: 23rd Apr 2010 00:18
possibly avoid also Do loop cause exit statement really resemble a goto. use if possible while or repeat loop

Try also not using gosub and use function instead. Unfortunatly DB lacks procedure declarations, but you can use function with no result. A clear advantage of functions over gosub is the use of local variables
Anti Human
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Good ol\' West Virginia
Posted: 23rd Apr 2010 09:09
@TDK: so using 'GoSub' is alright because it returns to the line that called it where using 'GoTo' stands a good chance turns the code into jumbled mess of head scraching. K, that I understood.

btw, thanks for the link! the reason i logged on was to find a tut on making buttons and i was gonna check your tut's first. reading the posts here, your tut on how/why to not use 'GoTo', and replying did actually take me longer; but that's ok by me, i learned something.

umm, there is something i noticed in my code. I have a Sub-Routine for creating the players charactor, it ends with a 'Return' but i don't know if it's ever actually read. before the return is a yes/no question, yes leads to another GoSub and no leads back to the GoSub that ends in the Y/N question.

is that/could that be a problem?

@veltro: given my near epic-fail coding abilities imma stick with the 'GoSub's i have in my current program. they may actually be what i need for where i'm stuck ATM though so i'll try to remember to figure them out after i get rid of all the numbered option menu's and get replace them with buttons.

@all: sorry for being so long winded. if winded is the right term.

Today is a good day, at least compared to tomorrow.
And every day is a good day to die, because then, there is no tomorrow
TDK
Retired Moderator
22
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 23rd Apr 2010 16:43 Edited at: 23rd Apr 2010 16:47
Quote: "but i don't know if it's ever actually read. before the return is a yes/no question, yes leads to another GoSub and no leads back to the GoSub that ends in the Y/N question"


The beauty of Gosub is that it doesn't matter how many other Gosubs you use before hitting the Return from the last one, the stack always guarantees that you will eventually be returned to where you started.

It's literally impossible for it to go wrong... unless you use Goto to jump somewhere else - and in the process skip over lines like Return - which keep the stack working correctly.

So what you are talking about simply can't happen.

TDK

Login to post a reply

Server time is: 2024-11-23 17:31:48
Your offset time is: 2024-11-23 17:31:48