Reply
Logi Visitor
josick
Posts: 26
Registered: ‎03-20-2012
0

Re: like select case in lua

The difference with my coding and ken... Ken make treatment after ("I make a lot of use of Lua's type() function to determine what type a value is (a number, a string, a table or a function) and to handle it appropriately.")

 

I have a first table with functions of treatment (for example, only two very simple):

treatments = {
alpha =function(x) PressAndReleaseKey(x) end,
msg = function(x) OutputLogMessage(x.."\n") end,
}

And after :

Map[1] = {
    [1] = function(x)  treatments.alpha("a")  end,
    [2] = function(x)  treatments.alpha("b")  end,
    [3] = function(x)  treatments.alpha("c")  end,
    [4] = function(x)  treatments.alpha("d")  end,
    [5] = function(x)  treatments.msg("hello")  end,
    [6] = function(x)  treatments.msg("bonjour")  end,
}

 Like that, you can use directly, without treatment to detect...

 


 


Logi Guru
bystander
Posts: 1,121
Registered: ‎06-04-2010
0

Re: like select case in lua

[ Edited ]

Your ways works, I can't argue that, and when calling functions, assuming you get rid of the unneeded x parameter in your map table, it's easy to see you are calling a function.  However, it sure is nice and clean to just see keystrokes directly assigned to a G key.  It's easy to use and see, although it might not be bad to use something similar to yours when calling functions, as it does show that you are calling a function with the variables passed in the more traditional manner.

 

Perhaps a little mix and matching of both systems would be best.

Map[1] = {
    [1] = "a",
    [2] = "b",
    [3] = "c",
    [4] = function()  fn_specialAction("d")  end,
}

function fn_specialAction(c)
  PressKey(c)
  Sleep(100)
  ReleaseKey(c)
end

 

You get the simplicity on standard keystroke assignments, and the ability to call special made functions in a more traditional manner as well.

Logi Visitor
josick
Posts: 26
Registered: ‎03-20-2012
0

Re: like select case in lua

No, no good bystander !

 

You need have standard call...

 

And your way, no standard !

 

With your example ... for exemple call [1] --> you need reuse Map[1][1] without () in a other fonction.

     call[4]--> you can use it directely :  Map[1][4]()  in this case with ()

 

If I make like you, but without table for treatment function, I'll do that :

Map[1] = {
    [1] = function()  fn_1letter("a") end,
    [2] = function()  fn_1letter("b") end,
    [3] = function()  fn_1letter("c") end,
    [4] = function()  fn_specialAction("d")  end,
}

function fn_1letter(c)
  PressKey(c)
  Sleep(100)
  ReleaseKey(c)
end

function fn_specialAction(c)
  OutputLogMessage("Special action "..c.."\n")
end

 Like that, call is all the time Map[1][x]()

Logi Visitor
josick
Posts: 26
Registered: ‎03-20-2012
0

Re: like select case in lua

And, for example, to dynamically change table (between 2 tables in this example)

table_in_use= 1

And obviously, for use --> Map[table_in_use][x]()

 

function fn_change_Table(c)
	table_in_use=c
end

Map[1] = {
	-- (.....)
    [5] = function()  fn_change_Table(2) end,
}

Map[2] = {
	-- (.....)
    [5] = function()  fn_change_Table(1) end,
}

 Sorry if I'm not clear !

Logi Guru
bystander
Posts: 1,121
Registered: ‎06-04-2010
0

Re: like select case in lua

[ Edited ]

I understand you like uniformity, I like simplicity.  The vast majority of the time I'm going to be assigning keystrokes to G keys, and being able to assign it directly is very pleasing on the eyes, easy to see what it does as well as easy to type and change.

 

To each their own.

 

And btw, my way has a standard.  It is set to accept keystrokes directly, and if you need a spcial case function, they are all handled by function call in the same way.

 

EDIT:

One more thought to consider.  I do understand the advantage of having every call the same, which means you don't have to do a type(map[1][x]) test before proceeding, but the map is not your typical programming script.  This is an interactive layout that will be changed from one game to the next and anytime you want to try a different keystroke.  It's a UI in all intents and purposes, therefore I believe kgober is correct in making it easier to input imformation over having a uniform way to call all actions (he has 2 ways to call all actions).

kgober
Posts: 3,754
Kudos: 436
Solutions: 286
Registered: ‎05-28-2009
0

Re: like select case in lua

[ Edited ]

josick, I think you're misunderstanding what bystander means.  perhaps this would be more clear:

Map[1] = {
    [1] = "a",
    [2] = "b",
    [3] = "c",
    [4] = function()  fn_specialAction("d")  end,
}

function fn_specialAction(c)
  PressKey(c)
  Sleep(100)
  ReleaseKey(c)
end

function OnEvent(event, arg, family)
...
local action = Map[table_in_use][arg]
if type(action) == "function" then
action()
elseif type(action) == "string" or type(action) == "number" then
PressAndReleaseKey(action)
end
...
end 

it's not actually necessary that every element of the table be callable in the same way -- you can examine the type of the table element and decide how to treat it on a case-by-case basis.  for functions, the appropriate action is to call them.  for strings, the appropriate action might be to treat them as keys to be pressed.

 

-ken

________________________________
I do not work for Logitech. I'm just a user.
Logi Visitor
josick
Posts: 26
Registered: ‎03-20-2012
0

Re: like select case in lua

no, no, I understand Ken !

no, no I'm not like uniformity bystander !

I'll be back tomorrow for explain me (here in pinoy country it's near midnigth).

Your  comments are very interesting and even surprising for me.

Sincely,

Josick

-------------------------------------

The game is here !!!

-------------------------------------

Logi Guru
bystander
Posts: 1,121
Registered: ‎06-04-2010
0

Re: like select case in lua

Just so you can understand where I'm coming from, I'll try to explain why I think that sort of system is quite good, although I'm not saying it's perfect.

 

The map[x] is a user interface to create an easy way to make scripts for playing games.  This is something that is going to be updated, copy pasted and altered many, many times by the user.  As a result, the object is to make things easy to understand, write and edit.  As a result, it make sense to compromise a little on performance (which is negligable as it is) and uniformity in order to make a pleasing way to interact with the script.

 

The map is most closely related to a form in visual basic/C++ or any other high level visual language.  This is the part the user interacts with, while the real code is running behind the scenes, so you approach things differently.

Logi Visitor
josick
Posts: 26
Registered: ‎03-20-2012
0

Re: like select case in lua

Hello,

In fact, I used Bystander's presentation. In first step (when I started), by Excel, and after with only Lua script.

 

I present to you what I did in step Excel. It helps to better understand what I did next.

 

In Excel sheet, I had a basic table (5 maps) with letter, number, keyword (begin by underscore).

In this way, it is very easy to change the value to execute for a key (or group of keyused. It looks like the preference of Ken and Bystander.

 

And I had a database of letters, numbers, keywords and, in front of, the way to obtain it.

Then, I  used a VBA program that produced the lua code (this code that you do not like).

(I hope than the 2 links are ok -->copy of the Excel sheets)

 

Second step : the same but with Lua script...

The basic table (5 maps) become (extract) :

link = {
[1]={	"a",	"1",	"_Alt",		"_backspacemot",	"@",},
[2]={	"f",	"è",	"_etoile",	"_vermajuscule",	"_maj",},
[3]={	"b",	"2",	"_notused",	"_backspacedeux",	"_notused",},
[4]={	"k",	"_=",	"_notused",	"_notused",			"_notused",},
}

 The database of letters, numbers, keywords, become (extract) :

ansi_ref ={ 
["a"] = "function()  PressKey(maje,maje2,0x10 ) ReleaseKey(maje, maje2,0x10 ) clustera=clusterdefaut  maje=varvide  maje2=varvide ; SetBacklightColor(58, 215, 40) end,",
["_euro"] = "function()  PressKey(0x138,0x12 ) ReleaseKey(0x12 ,0x138) clustera=clusterdefaut end,",
["_etoile"] = "function()  loc_dependance_ansi_ref.pntaccent(0x2b ) clustera=clusterdefaut end,",
["_espace"] = "function()  PressKey(maje,maje2,0x39 ) ReleaseKey(maje, maje2,0x39    ) clustera=clusterdefaut  maje=varvide  maje2=varvide ; SetBacklightColor(58, 215, 40) end,",
["_Escape"] = "function() PressAndReleaseKey(0x01) clustera=clusterdefaut end,",
}

Then, obviously, I  used a Lua script (under AS3) that produced in a file the lua code (this code that you do not like).

 

To do : I have an ideaproduce this code with GLua and llServer...

 

About my coding now :

 

I avoided to mix the data and the "engine" and I prefered have a simple link (Map[table_in_use][x]()) between them.

Why ? Because sometime I completely change the data, or sometime I completely change the engine (for example, now with the use of basic template of Bystander : very, very nice, congratulations Bystander http://www.logitechusers.com/game-profiles-macros-discussion/13910-basic-template.html ).
And because my engines are a little more complexe than a usual use, I'm happy to avoid treatment of the data in.

 

Quite simply !

 

Partials exemples (not really simple) of one engine with Bystander's basic template

keyKeyboard={--
{--to use g7, g6, g5, g14, g13, g,12 =>TYPE_KB = 6  for g13(lhc)		use with LHC_EVENTS = true
{["down"]=function() return lhc.g1m1_down end,["released"]=function() return lhc.g1m1_released end, ["pressed"]=function() return lhc.g1m1_pressed end,},
{["down"]=function() return lhc.g2m1_down end,["released"]=function() return lhc.g2m1_released end, ["pressed"]=function() return lhc.g2m1_pressed end,},
{["down"]=function() return lhc.g3m1_down end,["released"]=function() return lhc.g3m1_released end, ["pressed"]=function() return lhc.g3m1_pressed end,},
{["down"]=function() return lhc.g8m1_down end,["released"]=function() return lhc.g8m1_released end, ["pressed"]=function() return lhc.g8m1_pressed end,},
{["down"]=function() return lhc.g9m1_down end,["released"]=function() return lhc.g9m1_released end, ["pressed"]=function() return lhc.g9m1_pressed end,},
{["down"]=function() return lhc.g10m1_down end,["released"]=function() return lhc.g10m1_released end, ["pressed"]=function() return lhc.g10m1_pressed end,},
},
}

 

function _OnEvent(event, arg, family)

 for i = 1,#keyKeyboard[TYPE_KB] do
 
	if keyKeyboard[TYPE_KB][i].pressed() then KeyB_Num[1].startTimeKey[i]=GetRunningTime() end
	if keyKeyboard[TYPE_KB][i].down() then
		if KeyB_Num[1].olala[i]:isFinished() and not KeyB_Num[1].tKey[i].first then KeyB_Num[1].olala[i]:start() KeyB_Num[1].tKey[i].first=1 end
		KeyB_Num[1].olala[i]:run(KeyB_Num[1].tKey[i])	
	end
	if keyKeyboard[TYPE_KB][i].released() then
		KeyB_Num[1].pressedTime[i]=(GetRunningTime()-KeyB_Num[1].startTimeKey[i])
		KeyB_Num[1].olala[i]:stop() KeyB_Num[1].tKey[i].first=nil KeyB_Num[1].tKey[i].x=0 KeyB_Num[1].infoTime=KeyB_Num[1].infoTime.."\tG"..i..":\t"..KeyB_Num[1].pressedTime[i] --if pressedTime[i]>TIME_SHORT then timeAlert=true end
		x =0 xmax = 0 
		for tt=1,#keyKeyboard[TYPE_KB] do 
			x = x + KeyB_Num[1].tKey[tt].x  
			xmax = xmax + KeyB_Num[1].tKey[tt].xmax 
		end
		if (x ==0 and xmax>0) then 
			-- OutputLogMessage(infoTime.." \n") 	--ou alors stockage dans un fichier  
			--if timeAlert then ll.sCmd("TrayTip", "Time", infoTime, 5, 2) timeAlert=nil end  --Pas terrible cette alerte
			-- ci aprés, affichage à l'écran
			
			if not(ll==nil) then ll.sCmd("TrayTip", "Time", KeyB_Num[1].infoTime, 5, 2) end
			
			if not (xmax>#lili2[clustera]) then 
				lili2[clustera][xmax]()
				--Stockage dans un fichier avec la ligne ci-après
				--ll.sCmd("WriteFile", alphalist[xmax]..infoTime.."\n", pathfile..[[LuaTestFile.txt]], 1)
			else
				OutputLogMessage("Là xmax =%d clustera= %d \n", xmax, clustera) 
			end
			KeyB_Num[1].infoTime=""
		--if not(xmax>#alphalist) then typeString(alphalist[xmax]) else OutputLogMessage("Là xmax = %d \n", xmax) end
		for tt=1,#keyKeyboard[TYPE_KB] do KeyB_Num[1].tKey[tt].xmax=0 end
		end	
	end
 end --for	
end

In this case, what makes the link between the engine and the data is lili2[clustera][xmax]().

Little explanation :

On tray tip (http://upia.de/g-forum/viewtopic.php?f=7&t=15), time for each key pressed appears. This is to control my use of a six keys keyboard (video 30 sec).

At this time, I'm using a single keyboard six keys but I normally use both simultaneously. I have a G13 in each hand. For the right hand, the keys G1, 2, 3, 8, 9 and 10; for the left hand, the keys G7, 6​​, 5, 14, 13, 12.
I started with a Logitech G19 (used rotate), then with two LG 11 (the one of them used in reverse), and now the 2 G13.

Logi Visitor
josick
Posts: 26
Registered: ‎03-20-2012
0

Re: like select case in lua

@bystander : I absolutely agree with your last answerAnd my use is consistent with your approach.