Post subject: I'm trying to make a simple Lua script
Joined: 2/15/2014
Posts: 2
I'm attempting to just make a simple script that will push random buttons while running a Gameboy ROM
local buttons = {"A","B","Start","Select","Up","Left","Down","Right"}
local state = {true, false}

while true do
	joypad.set({buttons[math.random(8)]},state[math.random(2)])
	emu.frameadvance()
end
When I run the script, no input is even sent to the emulator.
Editor, Expert player (2072)
Joined: 6/15/2005
Posts: 3282
void joypad.set(table buttons, int/string controller = null)
"controller" is which controller to set key input. Game Boy requires controller to be null or nil (but for, say, NES, a value of 1 means player 1 and a value of 2 means player 2). "buttons" is a Lua table specifying the key input which to send to the controller. The keys are "A", "B", etc, and their values (true/false) determine whether they are pressed or not. For example, to press start on the Game Boy controller, you would do
local keyinput={}
keyinput["Start"]=true
joypad.set(keyinput,nil)
or alternatively,
local keyinput={}
keyinput.Start=true
joypad.set(keyinput)
The code which does what I think you intend is below:
local buttons = {"A","B","Start","Select","Up","Left","Down","Right"}
local state = {true,false}
local keyinput={}

while true do
  for key,value in pairs(buttons) do
    keyinput[value]=state[math.random(2)]
  end
  joypad.set(keyinput)
  emu.frameadvance()
end
Edit: You also need to check "Allow UDLR" in controllers if you want it to be truly random (enables up+down and left+right).
Joined: 2/15/2014
Posts: 2
Thanks for the quick response. I guess my problem was I didn't set the buttons to true in the initial table, and I had been going off documentation that the second variable was the state of the button.
Chamale
He/Him
Player (182)
Joined: 10/20/2006
Posts: 1353
Location: Canada
I actually need this too, thanks! Now I'm trying to modify it to change the probability of each button press, but I'm not sure how. This is what I attempted to give it a 1/1000 chance of pressing Start:
local buttons = {"A","B","Up","Left","Down","Right"} 
local state = {true,false} 
local keyinput={} 

while true do 
  for key,value in pairs(buttons) do 
    keyinput[value]=state[math.random(2)] 
  end 
  joypad.set(keyinput) 
end

local buttons = {"Start"} 
local state = {true,false} 
local keyinput={} 

while true do 
  for key,value in pairs(buttons) do 
    keyinput[value]=state[math.random(1000)] 
  end 
  joypad.set(keyinput) 
  emu.frameadvance() 
end
That didn't work. Can you tell me what I did wrong, please? edit: Wait, I think I know. I need to put the frame advance after all the key inputs. edit 2: ":22: '=' expected near 'end'". I don't see where to put the = sign near either "end" in this code, so it won't run. When I put an = in front of end, it says "unexpected symbol near 'end'."
Editor, Expert player (2072)
Joined: 6/15/2005
Posts: 3282
Once execution reaches while true do ... end, it will never leave (unless you use break or something). So the first while true do ... end and everything before it should not be there. It should be more like this:
local buttons = {"Start"} 
local keyinput={} 

while true do 
  r=math.random(1000)
  for key,value in pairs(buttons) do
   if r==1 then
    keyinput[value]=true
   else
    keyinput[value]=false    
   end
  end 
  joypad.set(keyinput) 
  emu.frameadvance() 
end
Note: math.random(n), where n is an integer, returns a random integer from 1 to n.
Chamale
He/Him
Player (182)
Joined: 10/20/2006
Posts: 1353
Location: Canada
FractalFusion wrote:
Once execution reaches while true do ... end, it will never leave (unless you use break or something). So the first while true do ... end and everything before it should not be there. It should be more like this:
local buttons = {"Start"} 
local keyinput={} 

while true do 
  r=math.random(1000)
  for key,value in pairs(buttons) do
   if r==1 then
    keyinput[value]=true
   else
    keyinput[value]=false    
   end
  end 
  joypad.set(keyinput) 
  emu.frameadvance() 
end
Note: math.random(n), where n is an integer, returns a random integer from 1 to n.
Thanks, but that doesn't seem to work. It runs but doesn't press Start at all. I got the following scripts to work, and I got the result I wanted by running them concurrently. To press A, B, up, left, down, and right 50% of frames:
local buttons = {"A","B","Up","Left","Down","Right"} 
local state = {true,false} 
local keyinput={} 

while true do 
  for key,value in pairs(buttons) do 
    keyinput[value]=state[math.random(2)] 
  end 
  joypad.set(keyinput) 
end
And to press Start 0.1% of frames:
local buttons = {"Start"} 
local state = {true,false} 
local keyinput={} 

while true do 
  for key,value in pairs(buttons) do 
    keyinput[value]=state[math.random(1000)] 
  end 
  joypad.set(keyinput) 
  emu.frameadvance() 
end
I don't precisely understand it, but it works. Now I'll set it on turbospeed and make it play Pokemon Red for twelve hours.