forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pressing buttons?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
Leo28C



Joined: 18 Jan 2007
Posts: 19

PostPosted: Sun Feb 04, 2007 3:58 pm    Post subject: Pressing buttons? Reply with quote

Hello everyone! :-D

I'm amazed by this Lua language... It does in 25 lines what C++ would do in like 60. O_O

Now, I might not be that new to coding but I am to games... :-P

My game does a change whenever the user presses the triangle button.

However, when that happens, that change occurs many times due to the fact that the button-press checking occurs every time the game loop goes by... Is there any way to determine how long it should be pressed for that change to take place?

I'm sorry about the bad wording and stuff, I'm really new. Help. Thanks! ;-)
Back to top
View user's profile Send private message MSN Messenger
PsyOps



Joined: 23 Jan 2007
Posts: 5

PostPosted: Sun Feb 04, 2007 8:21 pm    Post subject: Reply with quote

Im pretty new so i dunno if you would use my advice. New to programing.
Anywho for lua i use a buttonpress timer so that it doesnt repeat all actions over and over again on press.

initiate timer
checktimer if time >= .05 seconds then

put keypress, code all the pad stuff here
make sure you reset timer afer every keypress

end


Dunno if you can use that. And if there is a better way i would like to know myself.
_________________
Snoochie Boochies
Back to top
View user's profile Send private message
Leo28C



Joined: 18 Jan 2007
Posts: 19

PostPosted: Mon Feb 05, 2007 4:04 am    Post subject: Reply with quote

OK, this kind of works:

Code:

-- At beg. of file:
counter = Timer.new()
counter:start()

trianglePressed = false
trianglePressedAt = 0


    -- Later on:

    if pad:left() then x = x - a end
    if pad:right() then x = x + a end
   
    if pad:triangle() then
        if trianglePressed == false then
         trianglePressed = true
         trianglePressedAt = counter:time()
        else
            if counter:time() - trianglePressedAt >= 250 then
                -- Handle the press here --
                trianglePressed = false
                trianglePressedAt = 0
            end
        end
    end


It kind of works. 'Kind of' because it doesn't do it WHEN the triangle is pressed, but .25 second between the start and end of the press... :-(

Any ideas? Thanks! ;-)
Back to top
View user's profile Send private message MSN Messenger
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Mon Feb 05, 2007 6:30 am    Post subject: Reply with quote

Reset the timer each time you press a button..
Back to top
View user's profile Send private message
Leo28C



Joined: 18 Jan 2007
Posts: 19

PostPosted: Mon Feb 05, 2007 8:56 am    Post subject: Reply with quote

Mhmm... I don't get it... :-/

I mean, how can I check for a button press? Like;

* Push button
* Perform action
* Release button
* Push button
* Perform action
* Release button

Right now, it doesn't wait for the release to perform the action again... What's the way to check for that? Thanks! ;-)
Back to top
View user's profile Send private message MSN Messenger
cheriff
Regular


Joined: 23 Jun 2004
Posts: 262
Location: Sydney.au

PostPosted: Mon Feb 05, 2007 9:14 am    Post subject: Reply with quote

Have a boolean variable to act as a flag to keep track of current state of the button. Each time you read the pad in the game loop:

If button is not down, clear flag.
If button is down, and the flag is not set, then set the flag and perform your action as this is the first time the button was just pressed.
If button is down, and flag is set, then this is a repeated press/held key which you may choose to ignore, or repeat after a time delay or whatever you want, depending on the use
_________________
Damn, I need a decent signature!
Back to top
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Mon Feb 05, 2007 10:13 am    Post subject: Reply with quote

http://wiki.ps2dev.org/psp:lua_player:functions#controls


while true do
if not key = Controls.read() then
key = Controls.read()
if key:triangle() then
-- do stuff here
end
end
end
Back to top
View user's profile Send private message
Leo28C



Joined: 18 Jan 2007
Posts: 19

PostPosted: Mon Feb 05, 2007 11:24 am    Post subject: Reply with quote

Cheriff's solution worked, many thanks to all of you! ;-)
Back to top
View user's profile Send private message MSN Messenger
Altair



Joined: 20 May 2006
Posts: 76
Location: The Netherlands

PostPosted: Tue Feb 06, 2007 7:18 am    Post subject: Reply with quote

Can't believe nobody posted this solution. Everybody use's it as far as I knew:


Code:
pad = Controls.read()

if pad~=oldpad then
-- put the button stuff here.
end

oldpad = pad


What this does is, it stores the old button state and checks if it differs from what it is know. Only then it will execute the code again. Most easy way ever. No seperate button checks or booleans or whatever.
Back to top
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Tue Feb 06, 2007 7:49 am    Post subject: Reply with quote

I posted that on the previous post. It just looks a lil diff.
Back to top
View user's profile Send private message
Altair



Joined: 20 May 2006
Posts: 76
Location: The Netherlands

PostPosted: Tue Feb 06, 2007 9:04 am    Post subject: Reply with quote

Ah yeah I see now. However yours reads the controls twice, which isn't necessairy. However for the rest it's the same, you're right.
Back to top
View user's profile Send private message
the underminer



Joined: 03 Oct 2005
Posts: 124
Location: Netherlands

PostPosted: Tue Feb 06, 2007 6:44 pm    Post subject: Reply with quote

romero126 wrote:



while true do
if not key = Controls.read() then
key = Controls.read()
if key:triangle() then
-- do stuff here
end
end
end

shouldn't it be
if not key == Controls.read() then

Because it seams you are checking wether they are equal (== instead of =)
_________________
Behold! The Underminer got hold of a PSP
Back to top
View user's profile Send private message
Altair



Joined: 20 May 2006
Posts: 76
Location: The Netherlands

PostPosted: Wed Feb 07, 2007 1:09 am    Post subject: Reply with quote

yeps it should, but that wasn't really what it was about
Back to top
View user's profile Send private message
romero126



Joined: 24 Dec 2005
Posts: 200

PostPosted: Wed Feb 07, 2007 7:21 am    Post subject: Reply with quote

I was drunk as a skunk when i wrote it leave me alone.
Back to top
View user's profile Send private message
Leo28C



Joined: 18 Jan 2007
Posts: 19

PostPosted: Wed Feb 07, 2007 12:35 pm    Post subject: Reply with quote

romero126 wrote:
I was drunk as a skunk when i wrote it leave me alone.


LMAO! xD
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group