Coding Help with buttons

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

Moderators: Shine, Insert_witty_name

Post Reply
Barcode
Posts: 5
Joined: Tue Aug 23, 2005 2:06 am
Contact:

Coding Help with buttons

Post by Barcode »

I'm trying to run a simple program that is mostly text based. I started off by loading a simple image in the background and checking to see if I could get text up (with the simple Hello World). I got that to work, but then I wanted to add a button function to let the user advance to more text.

I got that to work as well, however, you had to hold the button down to view the text or else it goes back to the old text.

I reworked things around and tried something else by calling a function.

Now I get this error message:

error: script.lua:39: <eof> expected near 'end'

Here's my code:

Code: Select all

-- load images
background = Image.load &#40;"image.png"&#41;

-- create a new Color object
green = Color.new&#40;0, 255, 0&#41;
while true do
	screen&#58;blit&#40;0, 0, background, 0, 0, background&#58;width&#40;&#41;, background&#58;height&#40;&#41;, false&#41;

-- show some text on offscreen
screen&#58;print&#40;200, 100, "Hello World!", green&#41;
end
	
-- function
function option2&#40;&#41;
while true do
	screen&#58;blit&#40;0, 0, background, 0, 0, background&#58;width&#40;&#41;, background&#58;height&#40;&#41;, false&#41;
-- show some text on offscreen
screen&#58;print&#40;200, 100, "Hello World!oneone!", green&#41;
end

	
	
-- controls
	Pad = Controls.read&#40;&#41;
	if Pad&#58;cross&#40;&#41; or Pad&#58;start&#40;&#41; then
		screen.waitVblankStart&#40;1&#41;
		return "option2"
		end
	end
-- flip visible and offscreen
screen.flip&#40;&#41;

		

-- wait forevever
	screen.waitVblankStart&#40;&#41;


end

Benihana
Posts: 12
Joined: Sun Jul 31, 2005 4:54 am

Post by Benihana »

Lua doesn't mean what it says half the time - and that's not really Lua's fault. It would be near impossible for it to know what you mean when you make a mistake.

If this is your only code, you have an extra end... therefore my guess is that is what is causing your issues. Since I am not at home I can't check for you. Another easy mistake is misspellings, but I couldn't find any of those. A Lua IDE would likely catch such a mistake, as it would highlight the extra end, or throw off indenting. Also it would be useful to mark line 39 so that regardless of formatting everyone else knows what line is causing the error.

As far as buttons, although many people preach against global variables, I recommend it in this case. You can use a variable set to true or false, like buttonDown == true when the button is down. Sometimes I get lost trying to pass variables back and forth between functions.
MikeHaggar
Posts: 116
Joined: Mon Jul 18, 2005 2:20 am

Post by MikeHaggar »

Dunno if this is what you want:

Code: Select all

-- load images 
background = Image.load &#40;"image.png"&#41; 

-- function 
function option2&#40;&#41; 
  while true do 
    screen&#58;blit&#40;0, 0, background, 0, 0, background&#58;width&#40;&#41;, background&#58;height&#40;&#41;, false&#41; 
    -- show some text on offscreen 
    screen&#58;print&#40;200, 100, "Hello World!oneone!", green&#41;
  end
end 


-- create a new Color object 
green = Color.new&#40;0, 255, 0&#41; 

while true do 
  screen&#58;blit&#40;0, 0, background, 0, 0, background&#58;width&#40;&#41;, background&#58;height&#40;&#41;, false&#41; 

  -- show some text on offscreen 
  screen&#58;print&#40;200, 100, "Hello World!", green&#41; 

  -- controls 
  Pad = Controls.read&#40;&#41; 
  if Pad&#58;cross&#40;&#41; or Pad&#58;start&#40;&#41; then 
    screen.waitVblankStart&#40;1&#41; 
    return "option2" 
  end 

  -- flip visible and offscreen 
  screen.flip&#40;&#41; 

  -- wait forevever 
  screen.waitVblankStart&#40;&#41; 

end
Barcode
Posts: 5
Joined: Tue Aug 23, 2005 2:06 am
Contact:

Post by Barcode »

I tried that, but when you press X, the screen goes blank and says "press start to restart" with nothing to show my error.

I'm wanting to make a game that is pretty much text based.

Typically, i want to show a background image and have some kind of text saying "Hit X to continue". When you press X, it will apply a different background and then new text. Then hit X again to continue. Eventually the controls will become more complex to where you have options as to what you want happen. Typically, I'm working on a simple simdate game.
mikeyleo
Posts: 6
Joined: Thu Jul 21, 2005 4:41 am

Post by mikeyleo »

Should the line return "Option2" be there? Is in the main loop. I think when you execute that line you would exit.
Arwin
Posts: 426
Joined: Tue Jul 12, 2005 7:00 pm

Post by Arwin »

reading the controller is non-blocking, in Lua too. If I understood it correctly anyway. So that means you read the buttons, and then the program just goes on, no matter what is being read.

I'm not at all versed in Lua yet, but I assume that if you want to wait until X is pressed, you do something like

while not Pad:cross() do
Pad = Controls.read()
end

This should wait for a key to be pressed. Put this in your main loop, and move on to the next screen as soon as you've exited that loop.
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

Code: Select all

	
continue = false
repeat	--wait till x is pressed!
	pad = Controls.read&#40;&#41;
	if pad&#58;cross&#40;&#41; then
		continue=true
		screen.waitVblankStart&#40;30&#41; -- 1/2 sec
	end
until continue==true
set the wait time to whatever you want, i set it to half a sec
greets
Post Reply