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.

DarkBASIC Professional Discussion / some help with Functions

Author
Message
Game designer extraordinaire
15
Years of Service
User Offline
Joined: 11th Oct 2010
Location: Mars
Posted: 10th Nov 2010 01:15
Hello. This is my third post and this time, i am yet again in need of help. You see im having some trouble with the function command set and cant seem to get it to work I have looked all over the internet for a solution. Thanks in advance

Yes...it is I
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 10th Nov 2010 12:36 Edited at: 10th Nov 2010 12:41
Functions can be used to perform a task or return an 'answer':




EDIT: One more thing, make sure you put your functions after your game code or in their own included dba file.

Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 10th Nov 2010 17:05 Edited at: 10th Nov 2010 17:12
@ Game designer extraordinaire:

Adding to what Baxslash said:
It's also best to put an END before the first function because if the function is actually seen it'll end the program with an error. Run the following code as is once then REM off the END and run it again to see the error.

Baxslashes great examples in one snip:


Quote: "I have looked all over the internet for a solution."


If you check out TDKs Tutorials you'll get a better understanding of the basics of Darkbasic.

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

baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 10th Nov 2010 17:22 Edited at: 10th Nov 2010 17:22
It didn't occur to me that I was showing it in a completely un-useable way, thanks Grog!

Further point, I tend to use functions for repetative tasks which, by putting it in a little capsule, does not need to affect the rest of my program.

By that I mean I can use certain variables knowing full well that (as long as they are not "global") do not need to be reset each time. Quick example using a gosub against using a function:



Game designer extraordinaire
15
Years of Service
User Offline
Joined: 11th Oct 2010
Location: Mars
Posted: 11th Nov 2010 16:20
Thanks everyone! I think I understand it now... functions have to be at the end of your code.... I get it! that's what I was doing wrong.

Yes...it is I
Game designer extraordinaire
15
Years of Service
User Offline
Joined: 11th Oct 2010
Location: Mars
Posted: 14th Nov 2010 20:47
dang it! i thaught i understood functions oh well, i am yet again having problems and am starting to wonder if using functions in the first place was a good idea...
anyway for some reason my functions dont do anything!
when i input this code nomatter what i press it prints zero. what am i doing wrong?

// code
rightpressed=0
do
get_right_pressed()
print rightpressed
loop
end

function get_right_pressed()
if keystate (205)=1
rightpressed=1
else
if keystate (205)=0
rightpressed=0
endif
endif
endfunction rightpressed

Yes...it is I
Brendy boy
21
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 14th Nov 2010 20:53
variables that are filled with values inside functions and used outside the functions need to be declared as global

Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 14th Nov 2010 22:38
endfunction rightpressed

The above line returns the value of that variable. For example-



The function returns the value of c, which in this case is the sum of a and b. The function returns 3 in my example.

Your code would be fine if you changed it to -


or-


DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 14th Nov 2010 22:57 Edited at: 14th Nov 2010 23:01
Here you go, had a quick look, don't use functions much myself but this seems to work.


I changed your keystate to rightkey for now, as I had no idea what 205 was. Impressive 200+ key keyboard you have there!

http://s6.bitefight.org/c.php?uid=103081
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 14th Nov 2010 23:31
Quote: "I changed your keystate to rightkey for now, as I had no idea what 205 was. Impressive 200+ key keyboard you have there!"


It's just a standard keyboard... the KEYSTATE() numbers are just up there on some of them.

Shameless plug for my KEYSTATE() Map Graphic:


DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 15th Nov 2010 00:29
Oh, odd when I use the keystate checker the right arrow key comes up 77.

http://s6.bitefight.org/c.php?uid=103081
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 15th Nov 2010 01:25
You're the first person I know of that has different numbers. What's the brand of your keyboard?

DVader
22
Years of Service
User Offline
Joined: 28th Jan 2004
Location:
Posted: 15th Nov 2010 02:57
Chicony, been around a fair while to my knowledge.

http://s6.bitefight.org/c.php?uid=103081
baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Nov 2010 10:12
You could just use "rightkey()"...

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 15th Nov 2010 13:04
Quote: "You're the first person I know of that has different numbers. What's the brand of your keyboard?"


What about keyboards that don't have the numeric keypad (like my laptop)? I see code 77 is one of the numeric keypad keys in that diagram.
Frank C
16
Years of Service
User Offline
Joined: 3rd Jul 2010
Location: Houston TX
Posted: 15th Nov 2010 14:44
77 is the Keypad Rightkey code value, for keyboards that don't have seperate directional keys it would use the 77
Grog Grueslayer
Valued Member
21
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 15th Nov 2010 16:21 Edited at: 15th Nov 2010 16:23
I always thought the numbers would stay the same no matter what keyboard we use. If DVader has a keyboard like the picture below and he hits the non-number keypad right arrow key (what should be KEYSTATE(205))and gets a 77 instead what makes that keyboard different?



baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Nov 2010 16:41 Edited at: 15th Nov 2010 16:44

Login to post a reply

Server time is: 2026-07-21 18:58:25
Your offset time is: 2026-07-21 18:58:25