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 / How to change the value of a variable

Author
Message
DarkBasic professional
9
Years of Service
User Offline
Joined: 4th Jun 2014
Location:
Posted: 4th Jun 2014 09:35
Hi all! How to change the value of a variable? For example:
_start=1
...........
...........
do
..........
..........
if mouseclick()=1
........
_start=_start+1
........
endif
loop

its wrong
how to make?
Thanks
VisualProg
15
Years of Service
User Offline
Joined: 31st Mar 2009
Location: RussianFederation, Smolensk
Posted: 4th Jun 2014 14:49 Edited at: 4th Jun 2014 14:50
use tag "code"

public variable:


or

(is welcome write type)


next, you do use your code:


MZh[NUL] <---

Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Jun 2014 16:37
I didn't understand that answer.

The problem is that the loop is scanned several times while the mouse click still records 1 so your variable gets incremented several times for each mouse click.

There are at least two ways of dealing with that. The "quick and dirty" solution is to add a short loop which waits for the last mouse click to clear. For example





Powered by Free Banners
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Jun 2014 16:51 Edited at: 4th Jun 2014 17:36
The not so dirty way is to process the mouse click to trigger on positive edges only. That way, program flow continues uninterrupted.



This also works when checking for right clicks and middle clicks, just test if mouse_positive_edge = 2. or if mouse_positive_edge = 4 respectively.

Your dungeon has been arrested by a signature image because it tried to be a mod
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Jun 2014 17:03 Edited at: 4th Jun 2014 17:09
Doesn't compile.

Edit Neat - with typo fixed.





Powered by Free Banners
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Jun 2014 17:35 Edited at: 4th Jun 2014 17:35
Fixed. Thanks GG! I'm too used to C++

Your dungeon has been arrested by a signature image because it tried to be a mod
Zero G Scott
16
Years of Service
User Offline
Joined: 14th Dec 2007
Location: California, USA, Earth, Sol Sys
Posted: 4th Jun 2014 20:10
What's a mouse positive edge? This sounds suspiciously handy if it means what I think it means.
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 4th Jun 2014 21:45
I wondered about that too - but the code seems clear enough.



Powered by Free Banners
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Jun 2014 22:14 Edited at: 4th Jun 2014 22:17
Quote: "What's a mouse positive edge? This sounds suspiciously handy if it means what I think it means."


The term "positive edge" is derived from electronics in the field of digital signal processing.

Basically, if you were to plot the value of mouseclick() over time when a user clicks, you'd get something like the following, where each vertical line represents one execution of the DBP main loop.



The positive edge is on the left side where the signal rises to become a binary "1". There is also a negative edge, which is on the right side where the signal falls to being a become "0".

In order to detect a positive edge, you require at least two sample points. I did this by saving an old state, delaying the signal by one loop:



Let c be the current mouse state (green) and o be the old mouse state (blue), a positive edge p exists when c&~o, and a negative edge n exists when ~c&o, where ~ represents an inversion of all bits and & represents a bitwise AND operation.

Plotting p and n over time results in the following:



mouseclick() encodes button presses as "1" for left-click, "2" for right-click and "4" for middle click. Those familiar with bitwise operators will already know that these are single bits which when OR-ed together slot perfectly next to each other, i.e. 1||2||4 = 7, or %001||%010||%100 = %111.

My example takes advantage of these facts, and processes the positive edge for all 3 buttons at the same time.

I neatened it up a little and posted it on the code snippets board:
http://forum.thegamecreators.com/?m=forum_view&t=211243&b=6&p=0

Your dungeon has been arrested by a signature image because it tried to be a mod
Zero G Scott
16
Years of Service
User Offline
Joined: 14th Dec 2007
Location: California, USA, Earth, Sol Sys
Posted: 5th Jun 2014 07:13
This appears to make clicking and dragging a lot easier to manage, at least that's what my small mind gleams from this gem. I'm curious to know what other uses might this be helpful for? Thank you for posting this info TheComet!
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 5th Jun 2014 13:38 Edited at: 5th Jun 2014 13:39
Quote: "This appears to make clicking and dragging a lot easier to manage"


That's a great example! Dragging something becomes as simple as:



It generally makes more sense to trigger everything on positive edges. In FPS games for example, shooting is as simple as:



GUI elements (buttons, checkboxes, etc.) generally are better when triggered on negative edges. Windows does this too: If you click and hold the mouse on a button, you can still drag your mouse away before releasing again.

gamerboots
15
Years of Service
User Offline
Joined: 8th Dec 2008
Location: USA
Posted: 7th Jun 2014 14:34
You can also use the inc and dec commands.
a=a+1
is the same as
inc a

they will never know what bit them until its too late...
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 17th Sep 2014 10:00
I tie all my DBPro mouse needs in with the Windows User32.DLL, as I find DBPro bounds the mouse in a couple of limiting ways. Once you've geared your DBPro projects to use User32.DLL for mouse tracking, the world is your oyster!

Here are some of the variables that I capture;



Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Sep 2014 02:25
What exactly does the ".." operator do?

Quote: "
mouse_positive_edge = (mouseclick()..7)&&mouse_last_state
"


I've tried it with a bunch of numbers, the second operand doesn't seem to have any effect, it just inverts all bits. I was aware of &&, || and ~~, but haven't seen .. yet?

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 21st Sep 2014 11:49
From the documentation:


(mouseclick()..0x07) inverts the first 3 bits (bit0=left button, bit1=right button, bit2=middle button)

I like offending people. People who get offended should be offended. -- Linux Torvalds
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 21st Sep 2014 18:23
I didn't understand that - nor do I see the point of that operator. What is the purpose of the second operand?

I think the Help file you quote is wrong. Try this:





Powered by Free Banners

Login to post a reply

Server time is: 2024-05-02 15:10:27
Your offset time is: 2024-05-02 15:10:27