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
Different55
Posts: 130
Joined: Thu Jun 07, 2012 9:28 pm

Re: [LUA] Need help with code plz

Post by Different55 »

Code: Select all

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
This should be moved inside the loop, after the clear and before the flip.

Code: Select all

checkcontrols()
This should be added into the loop. It works wherever, really, but it would work best at the beginning of the loop, right after oldpad.
Advertising
dragonxtamer596
Posts: 188
Joined: Wed Oct 24, 2012 11:49 pm
Contact:

Re: [LUA] Need help with code plz

Post by dragonxtamer596 »

Different55 wrote:

Code: Select all

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
This should be moved inside the loop, after the clear and before the flip.

Code: Select all

checkcontrols()
This should be added into the loop. It works wherever, really, but it would work best at the beginning of the loop, right after oldpad.
Ok,ty,but now it has an error script.lua:6: attempt to index global 'pad' <a nil value>

Code: Select all

-- My Second Lua Program
-- Author: Dragonxtamer596

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

if pad:start() and oldpad:start() ~= pad:start() then
choice = math.ceil(math.random(1, 3))
end

while true do
pad = Controls.read()
oldpad = Controls.read()
checkcontrols()
screen:clear()
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() 
screen.waitVblankStart()
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 »

That is because you are calling pad before it is declared, it is also not inside the loop or inside a function that is inside the loop.
So put the if pad:start() statement inside the loop or place it inside a function and place that function in the loop.
It is better to write oldpad = pad so you will only call Controls.read() once.

My bad about the math.ceil(math.random()), it should be

Code: Select all

math.ceil(math.random(0, 3))
or you can do

Code: Select all

math.floor(math.random(1, 4))
with your current random statement it will only output 2 or 3 so you may want to change it to one of the above stated options.
You may also want to place

Code: Select all

math.randomseed(os.date("%S"))
somewhere at the top of your script, do NOT place it inside a loop. That will make the random generator output different options each time you restart the app. Using os.time() as an option for randomseed doesn't work as tutorials suggest.
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 »

Where do i put pad=oldpad?
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 »

In your code you posted

Code: Select all

pad = Controls.read()
oldpad = Controls.read()
you can change it to

Code: Select all

oldpad = pad
pad = Controls.read()
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 »

Error: index. lua: 6 bad argument to random seed number expected got string
what do i do?
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 »

Post what you wrote for math.randomseed.
Make sure your wrote the correct os.date argument.

Code: Select all

math.randomseed(os.date("%S"))
If that doesn't work then do this,

Code: Select all

math.randomseed(tonumber(os.date("%S")))
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:Post what you wrote for math.randomseed.
Make sure your wrote the correct os.date argument.

Code: Select all

math.randomseed(os.date("%S"))
If that doesn't work then do this,

Code: Select all

math.randomseed(tonumber(os.date("%S")))
Opps forgot the s, print.smily("100,50,facepalm,red")
script. lua: 11 attempt to call global 'checkcontrols' <a nil value>

Code: Select all

-- My Second Lua Program
-- Author: Dragonxtamer596

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

math.randomseed(os.date("%S"))

while true do
oldpad = pad
pad = Controls.read()
checkcontrols()
screen:clear()
if pad:start() and oldpad:start() ~= 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() 
screen.waitVblankStart()
end 
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 didn't make the function checkcontrols()

you need to make it before you call it and I don't see it in your code.

Example:

Code: Select all

function checkcontrols()
--code here
end

while true do
checkcontrols()
--code here
end
Also for choice you should change it to

Code: Select all

choice = math.ceil(math.random(0, 3))
because your current code will only output 2 and 3.
or you can do

Code: Select all

math.floor(math.random(1, 4))
To make your code less redundant you can do control arguments like this

Code: Select all

if pad:start() and not oldpad:start()then
--code here
end
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:you didn't make the function checkcontrols()

you need to make it before you call it and I don't see it in your code.

Example:

Code: Select all

function checkcontrols()
--code here
end

while true do
checkcontrols()
--code here
end
Also for choice you should change it to

Code: Select all

choice = math.ceil(math.random(0, 3))
because your current code will only output 2 and 3.
or you can do

Code: Select all

math.floor(math.random(1, 4))
To make your code less redundant you can do control arguments like this

Code: Select all

if pad:start() and not oldpad:start()then
--code here
end
IT WORKS!!!! YES, awsome!!!
Locked

Return to “Programming and Security”