Pressing buttons?

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Leo28C
Posts: 19
Joined: Thu Jan 18, 2007 8:20 am

Pressing buttons?

Post by Leo28C »

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! ;-)
PsyOps
Posts: 5
Joined: Tue Jan 23, 2007 12:50 pm

Post by PsyOps »

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
Leo28C
Posts: 19
Joined: Thu Jan 18, 2007 8:20 am

Post by Leo28C »

OK, this kind of works:

Code: Select all

-- 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! ;-)
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Reset the timer each time you press a button..
Leo28C
Posts: 19
Joined: Thu Jan 18, 2007 8:20 am

Post by Leo28C »

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! ;-)
cheriff
Regular
Posts: 258
Joined: Wed Jun 23, 2004 5:35 pm
Location: Sydney.au

Post by cheriff »

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!
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

http://wiki.ps2dev.org/psp:lua_player:f ... s#controls


while true do
if not key = Controls.read() then
key = Controls.read()
if key:triangle() then
-- do stuff here
end
end
end
Leo28C
Posts: 19
Joined: Thu Jan 18, 2007 8:20 am

Post by Leo28C »

Cheriff's solution worked, many thanks to all of you! ;-)
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

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

Code: Select all

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.
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

I posted that on the previous post. It just looks a lil diff.
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

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.
the underminer
Posts: 123
Joined: Mon Oct 03, 2005 4:25 am
Location: Netherlands

Post by the underminer »

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
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

yeps it should, but that wasn't really what it was about
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

I was drunk as a skunk when i wrote it leave me alone.
Leo28C
Posts: 19
Joined: Thu Jan 18, 2007 8:20 am

Post by Leo28C »

romero126 wrote:I was drunk as a skunk when i wrote it leave me alone.
LMAO! xD
Post Reply