[Programming] Binding a function to a function which determines something (complicated)

I didn’t know how to call this thread…

I stumbled upon a problem where I want to assign one function to another function which determines what happens next and both functions are triggered always at the same time by multiple triggers.

Here’s a “visual” explanation:

Function(“trigger1”, “setting”)
Function(“trigger2”, “setting”)
Function(“trigger3”, “setting”)

//functions use strings and “triggers” are keyboard buttons

Function Setting()
{
this function sets a variable to appropriate “state”, which is then identified by another function which triggers something. After this point everything is clear to me.
}

User triggers “trigger1” -> “Setting” is triggered -> “Setting” sets a variable to 1 -> Next function does its job
User triggers “trigger2” -> “Setting” is triggered -> “Setting” sets a variable to 69 -> Next function does its job
User triggers “trigger3” -> “Setting” is triggered -> “Setting” sets a variable to “Zantai is a nerd” -> Next function identifies it is not an int -> crash -> Developer of this program is retarded

Basically, I could do it that every trigger uses a different function, which sets the state of the variable, but this is redundant as fuck, would require me to make about 100 new functions (which do the same thing) and I am pretty sure I can do it another way. The “function” function is not my own function, but a built-in one, so I can’t do anything about it. I simply want to trigger one function by every single button I press (and have assigned). I was thinking about making a button identifier for that, but 1. I’d need to make a new big function for that (not a big deal, I guess) and 2. I am afraid that if button A will trigger button B at some point, and both will be assigned, I’ll get an endless loop if B also contains A (otherwise I’ll always get two triggers instead of the one I want).

Is this even doable? I can not think of any reasonable fix for that. The only thing I thought of is using arguments in “Setting” function, but then I can not set anything in there by “Function” function.

I wonder if this thread even makes sense… I need only theory, I didn’t say where I want it nor how because there are people who’d give me ready scripts, and I don’t want that.

For some of us, a programming language would be clearer than your rainbow pseudocode, which I do not understand at all.

My code is kind of messy anyway, but it’s in AutoIt (dunno if I should even call it a language, but they call it that). The pseudocode is basically exactly how the code looks, but if it’s needed I’ll post the code instead. I also didn’t use their forum because the last time I had an issue they just assumed I am trying to hack a game and closed my thread (even though I had an issue with GUI :rolleyes: ).

At the beginning of the script:

function name, keyboard key, function

HotKeySet("{NUMPAD1}", "HotkeyOnPress1")
HotKeySet("{NUMPAD2}", "HotkeyOnPress2")
HotKeySet("{NUMPAD0}", "HotkeyOnPress3")

empty while loop to make this program run forever or till closed by-hand

While 1

WEnd

the function that determines wtf


Func HotkeyOnPress1()

	$HotkeyButtonInstance = 10
	$HotkeyButtonInstance2 = 11
	$HotkeyButtonInstance3 = 12
	Call("MoveMouseTo")

EndFunc


Func HotkeyOnPress2()

	$HotkeyButtonInstance = 102
	$HotkeyButtonInstance2 = 81
	$HotkeyButtonInstance3 = 12
	Call("MoveMouseTo")

EndFunc

Func HotkeyOnPress3()

	$HotkeyButtonInstance = 34
	$HotkeyButtonInstance2 = 11
	$HotkeyButtonInstance3 = 99
	Call("MoveMouseTo")

EndFunc

Right now I just use a random code to check if it even works, so it’s an even bigger mess than the pseudocode I posted above. There are exactly 3 of those, and as you can see I even use random variables for that as it’s only like alpha version.

And again, I want to put everything to one function (which calls Call(“MoveMouseTo”)).

Have you thought of using a programming language to write a program that creates the code you need?

I suspect it might be easier to write some code that iterates over whatever it is you are doing to create the 100 new functions as output.

But I admit, I am not sure exactly what is going on with this code yet.

For example, the top part:


for( i in 1:3 ) {
	writeLines( sprintf( "HotKeySet(\"{NUMPAD%d}\", \"HotkeyOnPress%d\"", i, i ) );
}

Does that make sense? Make it easier to tackle?

Yeah, this would make sense, but the point is that I will be binding multiple buttons with one function, because I’ll have about 40 buttons assigned and I don’t want to make additional 40 functions.

Although, I have an idea now, thanks to you. I’ll simply write every needed parameter to one file and then check if the file contains the appropriate parameters and go further. In other words, if I press K, the program will check if it is in the file, if it is, it’ll then trigger the function it should. As for now it should be okay, as I’ll have to iterate through the whole file anyway.

But if someone has another idea, feel free to post.
TLDR if someone doesn’t want to read the whole thread: multiple keyboard keys trigger the same function that sets variables according to which key has been pressed to then trigger some actions. How to check which button has been pressed? should’ve said it this way before…
Searching for other options than making a function that checks what has been pressed and making multiple functions that do literally the same thing.

unless that one function can have parameters, I don’t see how you can do this… you might still need 100 parameter-less functions which then individually call the parameterized function with the right values however, as I assume your ‘triggers’ cannot have parameters themselves.

Yeah, they can not have parameters, which is the main reason I have troubles with this. If I could’ve had parameters in my function, it’d be way easier and just a few mins of work.

What mamba said:


HotKeySet( NUM1, "callback", 10, 11, 12 );
HotKeySet( NUM2, "callback", 102, 81, 12 );

Func callback( x, y, z )
  $HotkeyButtonInstance = x;
  $HotkeyButtonInstance2 = y;
  $HotkeyButtonInstance3 = z;
  Call( "MoveMouseTo" )
EndFunc

sorry for semicolons

Yeah, sadly I can’t do that at all. :confused:
https://www.autoitscript.com/autoit3/docs/functions/HotKeySet.htm

Well, I started this post like an hour ago, but my lawyer called and well, RL happens. Here is what I was about to post:

Ok then do this:

Text file (csv) to drive a program (below) to generate the AutoIt code:


NUM1,10,11,12
NUM2,102,81,12

Program to write AutoIt code:



i = 1;
for( line in readLines( textFile ) ) {
	tok = strsplit( line, "," )[[1]];
	writeLines( sprintf( "HotKeySet( %s, HotkeyOnPress%d", tok[1], i ) );

	writeLines( sprintf( "Func HotkeyOnPress%d()", i ) );
		writeLines( sprintf( "$HotkeyButtonInstance = %d", tok[2] ) );
		writeLines( sprintf( "$HotkeyButtonInstance2 = %d", tok[3] ) );
		writeLines( sprintf( "$HotkeyButtonInstance3 = %d", tok[4] ) );
	writeLines( "EndFunc" );
        i = i + 1;
}

I don’t quite get what you mean by that. Could you explain?

Well, first off the code is not even close to production, it was just hastily typed up, but essentially you make 1 text file, that acts as a driver for a script/program that reads that text file line by line and uses the 4 tokens on each line to pump out AutoIt code.

In this way, you have 1 small program/script to write and a text/csv file you could build in vi (or excel if masochist enough) that is used to get the unique parts for each function. The script reads the csv, and dumps tons of AutoIt code, without you having to write the code. When you decide to add more keys/x,y,z values or change something, you just modify the csv and re-run the script and you have the new AutoIt version.

Hell, I feel like I have said the same thing 3 times, did it make sense ever?

I’ve been learning with AutoIt and it is a programming language and they consider it as such. It’s not as full-fledged as the more “official” ones but it can get the job done and I am finding it perfect for learning.

They are purposefully distancing themselves from anything to do with games no matter whether it’s legit or not. I guess they ran into too many issues thanks to script kiddies and they said fuck it, no more, and they are sticking to their guns and refusing to be associated with it in any regard.

If you want them to help you make NO mention of games and make sure any code you provide for examples doesn’t show anything to do with manipulating a game.

That sounds fancy, but I rather type because I am very slow at it. :smiley:
I’ll definitely use that at some point, but as for now I’ll stick to hands only.

Yeah I did say back then I have an issue with my GUI, and even though there was nothing about games, nor anything that would connect to that, they still considered it hacking. Since then I just try to do everything by my own and eventually looking for general advises so that everyone can help me no matter the language I use. This thread is, kind of, an example how I do it (except I had no idea how to word stuff properly to make sense). :stuck_out_tongue:

I promise to give up after this, but I suspect I have not explained it well enough still.

While the script I wrote is not functional, it would take 2 minutes to make it actually work. The script reads a text file line by line and splits it into 4 strings, string 1 is the key-pressed, string 2 3 and 4 are the 3 numbers you set to your “$HotkeyButtonPressed” variables.

Instead of copy pasting 100 times the basic function and modifying all those numbers and key presses, you can store them all in a simple text file that the script uses to generate the mass of AutoIt functions.

If you type slow or fast is meaningless, typing out (even with copy paste) 100 functions that are all basically the same thing is a bad idea for a lot of reasons, but most of all because it is tedious and horrendous to modify, maintain, and/or fix.

But, to each his own I guess.

So, this is like a GPS to a buried treasure. Now I totally get what you mean and I actually want to use that. You’re a genius. :smiley:

Well, if you want to use R as the scripting language, I can fix what I have and post it here for you.

Never heard of that R thing, but if you can make fancy programs like macro things, programs that could run through certain folders and modify files (something like Notepad ++ has, but way more complicated :stuck_out_tongue: ) etc, I may get myself into learning it. :smiley:

But as for now I’ll stick to AutoIt till I finish my work with it… And clean the codes so they don’t look like a toilet that has been used for 100 years and been never cleaned. Again, thanks for the help. :smiley:

You can download R for windows here
https://cran.r-project.org/bin/windows/base/

once installed and open, put this first block into a text editor and alter the paths to suit your system.


project.dir = "C:/Users/ey0r3/letsdothis";
input.filename = "keydriver.csv";
output.filename = "autoit.autoit";

input.path = sprintf( "%s/%s", project.dir, input.filename );
output.path = sprintf( "%s/%s", project.dir, output.filename );

in.dat = read.csv( input.path, header = FALSE );
N = dim( in.dat )[ 1 ];

out.buffer = c();
for( i in 1:N ) {
	out.buffer = c( out.buffer,
	 sprintf( "HotKeySet( \"{%s}\", \"HotkeyOnPress%d\" )", in.dat[ i, 1 ], i ) );
}

for( i in 1:N ) {
	out.buffer = c( out.buffer,
	 sprintf( "Func HotkeyOnPress%d()", i ) );
	out.buffer = c( out.buffer,
		sprintf( "	$HotkeyButtonInstance = %d", in.dat[ i, 2 ] ) );
	out.buffer = c( out.buffer,
		sprintf( "	$HotkeyButtonInstance2 = %d", in.dat[ i, 3 ] ) );
	out.buffer = c( out.buffer,
		sprintf( "	$HotkeyButtonInstance3 = %d", in.dat[ i, 4 ] ) );
	out.buffer = c( out.buffer, "	Call( \"MoveMouseTo\" )" );
	out.buffer = c( out.buffer, "EndFunc" );
}

writeLines( out.buffer, output.path );

keydriver.csv should look like this with your actual data


NUMPAD1,10,11,12
NUMPAD2,102,81,12
NUMPAD3,34,11,99

Then copy paste the R code into the R window. If the paths are all correct and existing, this is the output you should find in “autoit.autoit”


HotKeySet( "{NUMPAD1}", "HotkeyOnPress1" )
HotKeySet( "{NUMPAD2}", "HotkeyOnPress2" )
HotKeySet( "{NUMPAD3}", "HotkeyOnPress3" )
Func HotkeyOnPress1()
	$HotkeyButtonInstance = 10
	$HotkeyButtonInstance2 = 11
	$HotkeyButtonInstance3 = 12
	Call( "MoveMouseTo" )
EndFunc
Func HotkeyOnPress2()
	$HotkeyButtonInstance = 102
	$HotkeyButtonInstance2 = 81
	$HotkeyButtonInstance3 = 12
	Call( "MoveMouseTo" )
EndFunc
Func HotkeyOnPress3()
	$HotkeyButtonInstance = 34
	$HotkeyButtonInstance2 = 11
	$HotkeyButtonInstance3 = 99
	Call( "MoveMouseTo" )
EndFunc

Just wondering but do you find R easy to learn? I’ve looked at it a time or 2 briefly I think but I am unsure. I remember years ago playing some bullet-hell shmup’s made by some Japanese developer that coded them in R (the games were free too) I guess and that’s about all I know of the language.

Haven’t been able to find those games thru search yet unfortunately, which kinda sucks because they were pretty good.

Edit: haha finally found it! the game was rRootage by Kenta Cho -
http://www.asahi-net.or.jp/~cs8k-cyu/index.html <-- scroll down to where rRootage is listed and just under it - listed horizontally - is a handful of other shmup games which are different variations of rRootage. Interesting stuff and pretty fun.

I had to dig deep and first find some old freeware games sites I used to scour to find this damned game. Got lucky and found it.

Edit2: might be wrong about the language he used. Seems he favored D - https://dlang.org - with many of his games.