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 / Single Mouse Click

Author
Message
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 3rd Feb 2013 19:30
Im having trouble making the mouse register just one click. As it stands as long as the mouse button is down no matter for how long this registers as multiple clicks. I know this. The question is, is there a way of making it click just once per "normal" click without using some sort of timer to make it register only one click per half second or something. Using flags and a timer i think i can make it work but there has to be a better way.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Feb 2013 20:24
What you're looking for is positive edge detection. That's usually done by looking at the difference between an old and new state.



TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 3rd Feb 2013 23:00
Quote: "What you're looking for is positive edge detection"

Ummmm, what? All he needs is a simple flag.



No clue what you'd need a timer for.

Somarl, have you looked at code snippets for my mouse management code? Does a lot more than what you're asking, but might be worth looking at.
http://forum.thegamecreators.com/?m=forum_view&t=201158&b=6

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 4th Feb 2013 09:08 Edited at: 4th Feb 2013 09:11
Yeah i tried using a flag but i cant seem to figure out where to put it to get the desired result. I want to click a button and the mousecursor become a building, then when the mouse is clicked a second time it places the building in the position the mouse was at at the time of clicking but i just cant seem to get it right.

Here is the code i have so far but its without media so i do apologise.



The only reason i was thinking a timer was because i couldnt think how to stop the mouse registering hundreds of mouseclicks a second so something like a flag system that goes off ahhhh i cant even think and type what i was thinking now so thats out of the window. Anyway i think you get the gist of what i was trying to do. I think i just have the flag in the wrong place.

By the way i will be checking out that link of yours, sounds good if i can wrap my head round it. Thank you.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Feb 2013 10:20 Edited at: 4th Feb 2013 11:14
@ Somarl

Give this a go and see if it does what you want. I've copied some of the code I used in my RTS and cut it down to your needs. Here's how you use it:

1) Before you can use it, you need to call InitMouse() once.
2) You have to call ControlMouse() once every loop.
3) uMouse.click returns:
-- 1 if the mouse was just clicked
-- 2 if the mouse is being held down
-- 3 if the mouse was just released

In your case, you are looking for when the mouse was just clicked, so you have to check for a 1 (which you are doing here):



Then, instead of checking mouseclick(), I replaced it with this:



Here's the entire code:



Quote: "Ummmm, what? All he needs is a simple flag. "


That's exactly what my code does.

I'm not sure what it's called in the IT world, but with micro controllers and in electronics, button signals have 4 states:



And most things are triggered on a "positive edge" (just pressed), because the signal is rising. Hence the name "positive edge detection". There are also applications where things are triggered when a button is released, so you'd need to detect a negative edge.

In my code I posted, MouseClick_positive would need to be checked if you wanted to detect a positive edge, MouseClick_normal would return the normal "mouseclick()" value, and MouseClick_negative would need to be checked to detect a negative edge.


TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Feb 2013 14:32
Quote: "but with micro controllers and in electronics, button signals have 4 states:"

ah that explains where your method was coming from. Never seen anyone do it like that before.

Quote: " i was thinking a timer was because i couldnt think how to stop the mouse registering hundreds of mouseclicks a second "

That's what the flag does. Until you remove your finger off the button, it can't register the click more than once.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Feb 2013 14:49 Edited at: 4th Feb 2013 14:51
Just throwing this out there, if you ever have a case where you want to detect rising and falling edges in a variable where each bit represents 1 button (e.g. btns as dword --> "btns" could store the states of 32 buttons because it has 32 bits), this can be done with the following code:





TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Feb 2013 17:50
I did something similar in my mouse management code.




"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 4th Feb 2013 19:40
Some very interesting stuff there but i cant seem to get that code working Comet. It does register a click but wont paste the jpg nor place it.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 5th Feb 2013 16:37
Quote: "I did something similar in my mouse management code."


So THAT is what the % symbol is for! Wow, thank you for teaching me something new, it just never clicked.

Compressing things like that is great for network games, because it can make quite an impact on how much data you have to send.

TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th Feb 2013 17:46
Quote: "So THAT is what the % symbol is for! "

lol, yup its for binary. I think it might be mentioned in the help files.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 5th Feb 2013 20:41
Im still struggling to make the jpg "paste" onto the final click. All the code does so far is attach correctly when build is clicked. Then it follows the mouse around as desired but wont place down on the second click.
zeroSlave
14
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 5th Feb 2013 21:58
If you are still using the code from your above post, it doesn't look like you are setting the varible "Flag" for use in:


Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid.
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 5th Feb 2013 22:47 Edited at: 5th Feb 2013 22:56
Do you mean resetting it like this:

Because that seems to have the same effect. I just seem to keep adding flags and im just over complicating the problem now as it either disappears when mouseclick is 0 or only pastes inside the mousebox when the mouse is down when i add anything else to it.
I also made flag a global and set it to 1 after a click but that just stopped displaying the sprite altogether. Basically the more i do the further it gets from doing what i want. Its almost there but i dont know how to make the mousex and y stop once the second mouse is clicked. It just stays attached forever. Do i need another flag?

Edit: Ok i get now that the above bit of code isnt even doing anything because MouseClick() = 1 And Flag = 1 NEVER happens. So i have put a global flag in and on the first mouse click it should be 1. Here


Now this actually works worse than before but i think its just a case of moving round the flag till it fits somewhere that makes it recognise 1 click and 1 flag. Not sure where though as its been all over the code. That and i still think the mouse is registering more clicks past a certain point so already pastes down and picks up the image multiple times before it leaves the mousebox, then after that it cant register anything i think.
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 5th Feb 2013 23:13
I finally got it.
2 Things that i wasnt doing properly.

1) Changed the flag to go off when umouseclick = 0, then when the mouse was clicked again with the flag on and buildingflag on it worked as intended.

Then i had the problem that it would disappear when being placed down so..

2) Made the variables for HouseX and Y and HouseOwned global and that fixed it as they were not being carried over into the function. Something i should have done at the very beginning.

Could not have done it without you guys. Thanks.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Feb 2013 00:47
For good coding practices, perhaps you should take another look at your functions and see if could just add parameters to the function header rather than making everything global. Making everything a global variable can be unsafe and a nuisance to track down problems.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 6th Feb 2013 09:05
Do you mean like i did with the mousebox thing. That it returns something after the function is done with? If so i will hve a bash at it but i presume maybe some things must be global or is that not true?
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 6th Feb 2013 14:14
What I don't get is why you're checking for mouseclick underneath the buttonClick function. Doesn't that function already check for a mouse click? And if so, then you're setting the buildingFlag to 1 right there then checking immediately underneath that IF statement for that flag anyway.

If you have to assign values to several variables, then a function is probably not what you want, especially if there are no parameters you need to pass to that code block.

I'd probably rewrite it this way myself:


"You're not going crazy. You're going sane in a crazy world!" ~Tick
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 6th Feb 2013 19:25
I see what you mean. I admit it was starting to get messy and while it works i would need to redo that function for every other box that doesnt need to return a flag and so on. I think i just ended up with too many flags and while what i have works its not neat.
Doing it your way would be fine, therefore it would be easier.

Thanks.
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 7th Feb 2013 20:28
@TheComet I wonder if you can just elaborate a little on this bit



Like what each bit is doing. I know it works im just struggling to follow it and i want to understand it thoroughly before i use it in a project. Thanks
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 7th Feb 2013 22:26


This is a UDT. UDT stands for "User Defined Type". Much like integers or floats, a variable can be defined as a UDT as well:



This means that "z" now has all of the elements inside the UDT defined above, and I can use them just like you use variables:



It's just a neat way of organizing your variables.



This just defines the variable "uMouse" as type "MouseVT" so you can access it later on in your program.



This is the logic that determines the current mouse click value. In the very first post I made in this thread, I showed you this code:



It's exactly the same. The concept is to get an "old" state and a "current" state of the mouse.



old_MouseClick will always be one frame behind current_MouseClick, and that allows you to tell all kinds of things about it, because if current_MouseClick = 1, and old_MouseClick = 0, we know that the mouse has just been pressed. if current_MouseClick = 0, but old_MouseClick = 1, we know the mouse has just been released. If they are both 1, we know the mouse is being held down. And if they are both 0, we know the mouse isn't being pressed.

BTW, that code was extracted straight from my RTS game "PonyCraft" and was cropped down to suit your needs. If you are interested in seeing the original code, you can do that here : https://bitbucket.org/TheComet/ponycraft-prototype/src/5bfa9076e6ca74ee957915c244fb83798b1a9764/source/Mouse.dba?at=master

It's free to use.

TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 7th Feb 2013 23:41
Thanks for that. It was more what the mouse states were i.e click 0, click 1 etc but i think im ok now.

Yeah i have already downloaded that file the other day and am being heavily inspired by your project (even though i might do somethign much more simpler and 2d first). I saw the youtube video and was gobsmacked at how nice everything moved along, even though it was a previs concept i just loved it. I wish you would have carried it on in dbpro or written a tutorial or anything but i understand that dbpro was not good enough for the full requirements of the game, mainly due to compatibility. I hope to negate this factor if i ever build anything so complex by not really building it for anyone, as long as it works for me (perhaps a few close friends who all have the same rig and operating systems etc) then thats fine but i fully understand the compatibility being a major issue to most.

Please write up a tutorial with Phaelax as it would just be the most awesome thing as you two are probably the most passionate people on here in regards to RTS's

Im just kidding. Even if im not.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 8th Feb 2013 13:03
Quote: "Thanks for that. It was more what the mouse states were i.e click 0, click 1 etc but i think im ok now."


Ah right. Maybe it would be smoother to actually use constants instead of numbers, perhaps something like this:



And then use this to check:



DBP's precompiler will replace all constants with the numbers, so you don't lose performance by using constants, and it makes reading the code much more obvious.

Quote: "Please write up a tutorial with Phaelax as it would just be the most awesome thing as you two are probably the most passionate people on here in regards to RTS's "


Phaelax? Would you like to join forces?

Well, since I'm developing the game from scratch again, I could afford to write something like a "blog" alongside it, documenting my thoughts and decisions as I go. RTS's are highly complex though and there are so many different ways to do it, I don't even know if you can make a tutorial about it. And if you could, said tutorial would be hundreds of pages in length.

TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 8th Feb 2013 13:59
Quote: "Phaelax? Would you like to join forces?
"

Please do

Anyway, i found this and its great but it is just ever so slightly unfinished.

http://www.dbcodecorner.com/darkbasic/rts_tutorial11.pdf

I understand the complexity behind RTS's as like you said there are so many ways to go about making one and so many elements that go into making one that no matter what you chose some might say its not the best way.
I would just like to see a tutorial that covers some basics about each element and lets you take it further. For example no need to explain how to make hundreds of different units with different stats and costs and so on, just do one and we should be able to figure out how to do others (that are stronger or run at different speeds etc).
Im sure this principle can be carried over to other elements of making the game. Who cares if its the best way, as long as its 'a' way and the end result is a project people can expand further with their new knowledge.

But its a dream at the moment In the meantime im just going to keep trying to solve small problems but if anything tutorialish pops up ill be following it start to finish.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th Feb 2013 15:20
We can try. Its a big project. You can look at my old tutorial and see that. Roughly 20 pages and it's not even finished.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 8th Feb 2013 15:48
Yeah, I just flew over it.

It would make sense to break it down into different fields, write an in-depth tutorial on every field, and then assemble it all again into one huge document.

We'd also need to meet on the same level of understanding, because I doubt we share the same techniques for writing an RTS. When assembling the tutorials, they need to slot together and work, which means they all need to follow the same general concept.

I dunno... I know the scale of a project like this, and my schedule is already very full. I think I'll have to decline this one.

TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 8th Feb 2013 18:10
Quote: "It would make sense to break it down into different fields, write an in-depth tutorial on every field"
This would be awesome as it would allow people to not only understand it but then expand the concept to fit their own.

Quote: "I think I'll have to decline this one. "


Noooooooooooooooooooooooooooooooooooooooooooooo.

Oh well i tried
I am going to go through that tutorial of yours Phaelax and see how far i get. After that i guess im not on my own if i ask for help understanding what to do next. It would be good for me to at least try.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th Feb 2013 21:03
Quote: "It would make sense to break it down into different fields, write an in-depth tutorial on every field, and then assemble it all again into one huge document."

I had the same thought if I were to write it again.


I haven't looked at my tutorial in a long time. But as far as I remember, I had a viking chopping down trees to gather resources along with unit formations and A* pathfinding. If you understand enough to make it that far, you can probably finish it yourself. There's better options available now than what I had to work with back then as well, such as the terrain for instance.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 9th Feb 2013 02:39
Quote: "If you understand enough to make it that far, you can probably finish it yourself. There's better options available now than what I had to work with back then as well, such as the terrain for instance."


Yeah, that's a very good point. I think it would be far more beneficial for others if smaller, more focused tutorials on specific subjects are made rather than writing a huge thing that isn't all too specific. An RTS game in the end is just a collection of small mechanics, and by the time the programmer understands these mechanics in depth, he/she will have the ability to write an RTS.

TheComet

http://blankflankstudios.tumblr.com/
"ZIP files are such a retarded format!" - Phaelax
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 9th Feb 2013 11:29
Quote: " An RTS game in the end is just a collection of small mechanics"

Well this is what im aiming for if people dont mind me asking questions every five minutes (which i know they dont, this is why its a noobie forum ). So hopefully piece by piece i can get to where i want to be. But i have to admit, following a good tutorial and asking questions on the fly as it progresses is in my opinion the very best way to learn. So see how to start a project and what to add to it, when and why, and what direction to take it can be quite invaluable for people who learn like myself.
I am still grateful for the help so far. Slowly but surely over the years i will build up a decent enough knowledge to start a full project that i can plan out from start to finish. That is the long long term goal of all these little questions
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 14th Feb 2013 21:56 Edited at: 14th Feb 2013 22:12
I have a nice snippet for this: key = keystate(x) * (key+1)
This will count the iterations that key "x" is held and will reset upon release. You can use this in tandem with a flag to toggle:
You can also use the count aspect to time or delay actions:
Apply some bitwise logic and you can adjust delay increments:


^ That's what she said.

Login to post a reply

Server time is: 2024-04-24 10:46:01
Your offset time is: 2024-04-24 10:46:01