Advertising (This ad goes away for registered users. You can Login or Register)

[LUA] Need help with code plz

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Locked
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

[LUA] Need help with code plz

Post by dragonxtamer596 »

I'm making a random number generator
error index lua 24 method must be called with a colon
code, what do i do?

-- My Second Lua Program
-- Author: Dragonxtamer596

red = Color.new(255, 0, 0)

function checkcontrols()
pad = Controls.read()
if pad:start() then
end
exitgame = 1
end

local choice = math.random(1, 3) --Chooses a random number between 1 and 3.

if choice == 1 then
screen.print(200, 100, "1", red)
end

if choice == 2 then
screen.print(200, 100, "2", red)
end

if choice == 3 then
screen.print(200, 100, "3", red)
end

screen.flip()
while true do
screen.waitVblankStart()
end

checkcontrols()
drawall()
if pad:start() == 1 then
end
while true do
break
end
Advertising
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [LUA] Need help with code plz

Post by qwikrazor87 »

For the interpreter you are using you must use a colon with the print function.
Example
screen:print(10,10,"Hello World!",red)
Also the controls function does not return a number unless you make a control function that does so.
pad:start() returns a boolean value, either true or false.
Example
if pad:start() == true then
do something here
end
or
if pad:start() then
do something here
end
There is a lot wrong with your code so feel free to post your errors here when they show up. :)
Advertising
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

qwikrazor87 wrote:For the interpreter you are using you must use a colon with the print function.
Example
screen:print(10,10,"Hello World!",red)
Also the controls function does not return a number unless you make a control function that does so.
pad:start() returns a boolean value, either true or false.
Example
if pad:start() == true then
do something here
end
or
if pad:start() then
do something here
end
There is a lot wrong with your code so feel free to post your errors here when they show up. :)
where would I put
if pad:start() then
do something here
end
if i want start to generate a new random number or exit game?
EDIT:It works, 3 is always the random number, and start won't exit game.......how do i make start generate a new random number?
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [LUA] Need help with code plz

Post by qwikrazor87 »

You can just do something like
if pad:start() then
choice = math.random(1,3)
end
You have to make sure your program is running in a loop and not only once.
while true do
code here
end
Depending on the interpreter you are using you may want to check if the math.random function is not returning a float number. If it is then just use math.floor or math.ceil.
choice = math.ceil(math.random(1,3))
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

New code, will this work?
Can you explain a few code lines to me?
screen.flip()
while true do......i know its a loop but does it do anything else?
screen.waitVblankStart()
drawall()
and break.............it goes to the bottom of code but does it do anything else?
Is the

Code: Select all

function checkcontrols()
pad = Controls.read()
if pad:start() then
choice = math.ceil(math.random(1, 3))
end 
in the right place?

Code: Select all

-- My Second Lua Program
-- Author: Dragonxtamer596

red = Color.new(255, 0, 0) 

function checkcontrols()
pad = Controls.read()
if pad:start() then
choice = math.ceil(math.random(1, 3))
end

if choice == 1 then 
screen.print(200, 100, "1", red) 
end

if choice == 2 then 
screen.print(200, 100, "2", red) 
end

if choice == 3 then 
screen.print(200, 100, "3", red) 
end

screen.flip() 
while true do
screen.waitVblankStart()
end
Different55
Posts: 130
Joined: Thu Jun 07, 2012 9:28 pm

Re: [LUA] Need help with code plz

Post by Different55 »

Yeah, that looks right. The reason for the loop is to keep the program from ending as soon as it's done. You might want to wrap the whole code in the loop, otherwise you'd have to hold start when you run the program, because the program would enter the loop before you could press start. It works as it is right now but there are a lot of improvements that could be made, like taking out the ifs and replacing them with one print statement (you can print integers/floats as well as strings), or adding the loop around the whole script, or adding a oldpad variable to prevent the script from picking and printing multiple numbers on top of each other, or adding a screen:clear() to prevent the numbers from stacking up on each other if you do the loop around everything.
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

Code: Select all

while true do
-- My Second Lua Program
-- Author: Dragonxtamer596

red = Color.new(255, 0, 0) 

function checkcontrols()
pad = Controls.read()
if pad:start() then
choice = math.ceil(math.random(1, 3))
end

if choice == 1 then 
screen.print(200, 100, "1", red) 
end

if choice == 2 then 
screen.print(200, 100, "2", red) 
end

if choice == 3 then 
screen.print(200, 100, "3", red) 
end

screen:clear()
screen.flip() 
while true do
screen.waitVblankStart()
end
end
Like this?
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [LUA] Need help with code plz

Post by qwikrazor87 »

"screen.flip()" updates the screen with any changes made to the drawing buffer.
"screen.waitVblankStart()" is a function you can use to delay your program. Place a number inside the parentheses and that is the amount of milliseconds your program will pause before starting a new loop.
"drawall()" is a function and I don't know exactly what it does since you have not posted the function.
"break" will stop a loop from running when called.
Example

Code: Select all

function foo()
 while true do
  if this == that then
   break
  end
 end
end
That will stop the loop inside the function from running. break can be used inside any loops you use, while true do, for loops, repeat until loops.

Check out these tutorials if you haven't done so already.
http://www.evilmana.com/tutorial/psp-lua-programming/
it may be outdated but it will teach you the basics.
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

I give up on this project heres the download http://www.sendspace.com/file/8rc54i I give anyone who wants it permission to edit and call it theirs.
Can be used in replace of dice for bored games if you lose the dice, be used to give random weapon/item drops in game, and what ever else you can think of.
qwikrazor87
Guru
Posts: 2874
Joined: Sat Apr 21, 2012 1:23 pm
Location: The North Pole

Re: [LUA] Need help with code plz

Post by qwikrazor87 »

Don't give up so soon. I was just where you are. You will go through a lot of trial and error when programming. Don't overwhelm yourself by trying to make a big project at first, take some time to learn what each function does and experiment with it.
You won't learn everything in a short amount of time, it requires a lot of patience. Consider taking a look at the tutorials I linked.
PSP 2001 - TA-085 - 6.61 PRO-C2
PS Vita 3G - PCH-1101 - 3.65 HENkaku Ensō
Alcatel phone - Android 8.1.0
Laptop - Toshiba Satellite L305D-S5974 - Ubuntu 16.04 LTS
Locked

Return to “Programming and Security”