1 2 3
8 9
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
In a newer version of bizhawk, my script has started to malfunction. It is supposed to display the currently pressed gamepad buttons but now it displays Tilt Z and stuff like that. How can I turn that off? Relevant code:
t = ""
for key, value in pairs(joypad.get()) do
--  if value ~= false then    t = t .." ".. tostring(key) end
    if value ~= false then
         if tostring(key)=="Up" then t = t .." ^"
         elseif tostring(key)=="Down" then t = t .." v"
         elseif tostring(key)=="Left" then t = t .." <"
         elseif tostring(key)=="Right" then t = t .." >"
         elseif tostring(key)=="Select" then t = t .." s"
         elseif tostring(key)=="Start" then t = t .." S"
         else t = t .." ".. tostring(key) end
    end
end
gui.text(76,60,tostring(t), 0xff000000, 0xffffffff)
end
--- Does bizhawk support tilting? How? --- This code doesn't seem to work, why?
gui.drawText( 82, 7, "Hello world", "#ffffff")
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
Seriously? You loop over all the controls and print them out. At some point "tilt" gets printed. Now you wonder if bizhawk supports tilting? Of course it does. You print everthing it supports, and it started printing tilt. Stop looping over all the controls and just print the ones you want. Finally, i dont know why, but in the meantime you can use 0xFFFFFFFF. You know, like the example lua scripts provided with the emulator do.
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
zeromus wrote:
Seriously? You loop over all the controls and print them out. At some point "tilt" gets printed. Now you wonder if bizhawk supports tilting? Of course it does. You print everthing it supports, and it started printing tilt. Stop looping over all the controls and just print the ones you want. Finally, i dont know why, but in the meantime you can use 0xFFFFFFFF. You know, like the example lua scripts provided with the emulator do.
I have tried a lot back when I wrote that and couldn't find any other way to make it work. And "hexcode" usage is from the script FatRatKnight provided in the 2nd post. Lastly, I was asking how to use tilting in bizhawk.
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
TILT came out of the joypad controls table. To tilt bizhawk, kick over your case, or bind it the same as any other control.
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
I tried kicking my computer but it didn't seem to work. Are you sure this is supposed to tilt bizhawk? I have also found this place: But I can't seem to bind a key. When clicking on the greyed out text fields and pressing a key, it isn't recognized. Clicking on nearby buttons didn't help.
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
Why are you trying to press keys to bind analog controls
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
zeromus wrote:
Why are you trying to press keys to bind analog controls
How else?
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
wiggling sticks?
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
I'm writing a new script in Bizhawk and I'm still wondering how to improve the code I posted earlier. Any help is welcome. The goal is to print all currently pressed buttons except Tilt / Light Sensor / etc..
for key, value in pairs(joypad.get()) do
    if value ~= false then
         if tostring(key)=="Up" then t = t .." ^"
         elseif tostring(key)=="Down" then t = t .." v"
         elseif tostring(key)=="Left" then t = t .." <"
         elseif tostring(key)=="Right" then t = t .." >"
         elseif tostring(key)=="Select" then t = t .." s"
         elseif tostring(key)=="Start" then t = t .." S"
         else t = t .." ".. tostring(key) end
    end
end
Edit: I'm using this now, but if you can improve it please feel free.
for key, value in pairs(joypad.get()) do
   if value ~= false then
         if tostring(key)=="Up" then t = t .." ^"
         elseif tostring(key)=="Down" then t = t .." v"
         elseif tostring(key)=="Left" then t = t .." <"
         elseif tostring(key)=="Right" then t = t .." >"
         elseif tostring(key)=="Select" then t = t .." s"
         elseif tostring(key)=="Start" then t = t .." S"
         elseif tostring(key)=="A" then t = t .." A"
         elseif tostring(key)=="B" then t = t .." B"
         elseif tostring(key)=="L" then t = t .." L"
         elseif tostring(key)=="R" then t = t .." R"
         else end
    end
end
Masterjun
He/Him
Site Developer, Skilled player (1988)
Joined: 10/12/2010
Posts: 1185
Location: Germany
Language: lua

local buttons = {["Up"]="^", ["Down"]="v", ["Left"]="<", ["Right"]=">", ["Select"]="s", ["Start"]="S"} local s = "" for k,v in pairs(joypad.get()) do if v==true then -- the Tilt things are never true... s=s..buttons[k] elseif v==false then -- ...nor are they false, so they skip all of this s=s.." " end end gui.text(50,50,s)
Add the L and R things if you want.
Warning: Might glitch to credits I will finish this ACE soon as possible (or will I?)
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
How to use emu.yield()? (which supposedly makes scripts run even when the emu is paused) And is there a way to change Bizhawk's window width and height with lua?
adelikat
He/Him
Emulator Coder, Site Developer, Site Owner, Expert player (3573)
Joined: 11/3/2004
Posts: 4754
Location: Tennessee
You use emu.yield() in place of emu.frameadvance(), it will then run your loop for every screen draw instead of ever frame. Be careful with it.
It's hard to look this good. My TAS projects
Amaraticando
It/Its
Editor, Player (159)
Joined: 1/10/2012
Posts: 673
Location: Brazil
I make like this:
Language: Lua

while true do --- code -- Frame advance: hack for performance if client.ispaused() then emu.yield() gui.clearGraphics() gui.cleartext() else emu.frameadvance() end end
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
	if X > PositionX and X <PositionX> PositionY and Y <PositionY> 0 then 
			
			menuscreen=0
			PositionY = PositionY + (Y-YBefore)
			PositionX = PositionX + (X-XBefore)

			if PositionY <0> 159-height then PositionY=159-height end
			if PositionX <0> 239-width then PositionX=239-width end
			
		end
		
		boxSelected(PositionX,PositionY,PositionX+width,PositionY+height)
	else
		boxNormal(PositionX,PositionY,PositionX+width,PositionY+height)
	end
This code is used in a lot of functions I'm using. The only thing that's different is the PositionY = PositionY + (Y-YBefore) PositionX = PositionX + (X-XBefore) part. Can I make the above code into a function and pass a custom function, like so?:
drawDisplayBox(posx,posy,width,height,a()
         PositionY = PositionY + (Y-YBefore)
         PositionX = PositionX + (X-XBefore) 
end)
I have not gotten this to work.
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
drawDisplayBox(posx,posy,width,height,
 function(X,Y,XBefore,YBefore)
  local PositionY = PositionY + (Y-YBefore);
  local PositionX = PositionX + (X-XBefore);
  return PositionX, PositionY
 end
);
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
Ok, I try:
drawDisplayBox(posx,posy,width,height,a())

	if X > posx and X < posx+width and Y > posy and Y < posy+height then
	
		if clicked and clickedFrames > 0 then 
			
			a()
			
		end
		
		boxSelected(posx,posy,posx+width,posy+height)
	else
		boxNormal(posx,posy,posx+width,posy+height)
	end
	
end

----------------------------------------------------------------------
local drawPosition = function()

	width=106
	height=35

	drawDisplayBox(PositionX,PositionY,width,height,a()

        -- this happens when clicking on the display:
			menuscreen=0 -- menu gets closed
			PositionY = PositionY + (Y-YBefore) -- enables dragging the display around
			PositionX = PositionX + (X-XBefore) -- "

	end
	)
	

	(...)

end
drawDisplayBox() is the new function I'm using. PositionX, PositionY, X, Y, XBefore, YBefore, clicked and clickedFrames are all global variables so I'm not passing such as parameters. It doesn't matter if using function() or a(), I'm still getting an error: LuaInterface.LuaScriptException: [string "main"]:564: '<eof>' expected near 'end' 564 is the line where the very last "end" of drawDisplayBox() is. Thanks everyone for helping me so far.
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
You know how to make a function, because you wrote the drawPosition function, so why is the very first line of your script attempting to create a function some other garbledy way? why are you trying to name the parameter a()? parameters dont have names with ().
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
I need to call that custom function inside drawDisplayBox, at line 5. If I just name it function, I cannot call it at line 5. I have also tried naming it just a and writing just a at line 5 which didn't work. If you can, please give me an example code what I'm supposed to write.
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
so don't name it function, name it a. you can't just write a at line 5. since when do you call a function by just writing the name of the function? you call a function by writing the name of the function and then (). if a is a function, then a() is calling function a.
Editor, Expert player (2073)
Joined: 6/15/2005
Posts: 3282
The function a() should be defined separately in its own block. Even if for nothing else other than to make the code easier to understand. Also, all functions must be defined with the "function" keyword. For example:
function a()

        -- this happens when clicking on the display:
         menuscreen=0 -- menu gets closed
         PositionY = PositionY + (Y-YBefore) -- enables dragging the display around
         PositionX = PositionX + (X-XBefore) -- " 

end
Furthermore, when passing a function in a parameter list, just pass the name, with no parentheses.
Player (75)
Joined: 8/26/2015
Posts: 70
local drawPosition = function() 

   width=106;
   height=35;

   local a = function()
      menuscreen = 0
      PositionY = PoisitionY + (Y-YBefore)
      PositionX = PositionX + (X-XBefore)
   end;

   drawDisplayBox(PositionX,PositionY,width,height,a);

   -- (...)

   a();

end
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
Thanks I got it to work. You can see here http://pastebin.com/TxhgSXBC (expires in 30 days) the usage of drawButton() and drawDisplayBox(). (Let's use this opportunity to ask another question: How to best give a value (a certain amount of) leading zeros?)
Player (75)
Joined: 8/26/2015
Posts: 70
MUGG wrote:
Thanks I got it to work. You can see here http://pastebin.com/TxhgSXBC (expires in 30 days) the usage of drawButton() and drawDisplayBox(). (Let's use this opportunity to ask another question: How to best give a value (a certain amount of) leading zeros?)
I'd probably use the lua function string.format, provided it's exposed in BizHawk's lua; I haven't tested it so can't say for certain. Say you want to pad it to 8 digits wide with leading zeros, it would be something like:
string.format("%08d", 1234);
-- gives you "00001234"
Some people would disagree with this (lots of programmers I know don't like format functions for some reason), but I think it's the solution that expresses your intent the most clearly.
Editor, Expert player (2330)
Joined: 5/15/2007
Posts: 3933
Location: Germany
Belated thanks :) And onto the next adventure:
if a="rex" then

  rex=true

elseif a="pig" then
 
  pig=true

elseif a="cat" then

  cat=true

end
I would like to write something like tovar(a)=true How can I do this?
Player (75)
Joined: 8/26/2015
Posts: 70
Put it as a table element.
Language: lua

local mytable={[a]=true};
You have to access it as mytable[a], but it's the closest I can think of. For an example of how this works, that was what I did for controller input in my Denki Blocks lua script (L103)
1 2 3
8 9