Author Topic: A plugin to choose and run a random ROM...  (Read 284 times)

wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
A plugin to choose and run a random ROM...
« on: November 02, 2020, 01:28:45 AM »
It's always driven me a bit nuts that the "random" button in Attract Mode doesn't actually run the ROM, it just chooses it. Maybe there should be a second "random" control to choose and then run it?

Anyway, in the meantime, I made a plugin to choose *and* run the ROM. Whoo hoo! Loving having it choose all these crappy old games for me, ha.

EDIT: attaching the updated version here, this should work on all platforms. The rest of the thread is about the old version, before rand0m helped fix it and made it cross platform.

To use, put this plugin in Attract Mode's plugin folder, then go to AM's Controls menu and assign whatever key you'd like to "custom2", which is what the plugin listens for. You can edit the plugin to change that.

« Last Edit: November 02, 2020, 09:46:27 PM by wrybread »

wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #1 on: November 02, 2020, 02:45:12 PM »
[Edit: this is outdated, don't use this verison]

I made a Windows version, it uses an Auto Hot Key script to send the keys, but it's weird, Attract Mode freezes when the compiled AHK script runs. The AHK script is as simple as it gets:

Code: [Select]
Send r

Sleep, 500

Send {Enter}

Works great when I run it outside Attract Mode (will even choose and select a ROM), but when run from the plugin it freezes the AM interface. Hmm.

I don't use AM on Windows, I just installed it to test the plugin, so it's possible it's something wrong with my AM installation? Or maybe antivirus? (I disabled AVG for the test, but still had the issue).

Does anyone have any ideas?

Attaching all the files.

« Last Edit: November 02, 2020, 08:32:08 PM by wrybread »

rand0m

  • Sr. Member
  • ****
  • Posts: 337
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #2 on: November 02, 2020, 07:07:13 PM »
Try the following code

Code: [Select]
function random_press(sig)
{
if (sig == "random_game")
{
fe.signal("random_game");
fe.signal("select");
    }
  return false;
}

fe.add_signal_handler( "random_press" );

wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #3 on: November 02, 2020, 07:15:23 PM »
[Edit: this is outdated, don't use this version.]

Thanks! Not sure if I'm just implementing it incorrectly, but it freezes AM for me.

I made an AM plugin with your code, attached.

I then enable it and press the random signal (the R key on my system) and then AM freezes. Nothing printed to console.

Not sure if it matters but that's on Linux.

Any ideas?
« Last Edit: November 02, 2020, 08:32:34 PM by wrybread »

rand0m

  • Sr. Member
  • ****
  • Posts: 337
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #4 on: November 02, 2020, 07:29:48 PM »
This is not a plugin, add the code at end of your layout.nut.

wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #5 on: November 02, 2020, 07:41:50 PM »
Aha.

Still doesn't work for me though, looks like it continually runs the code without exiting. Adding this print statement makes that clear:

Code: [Select]
function random_press(sig)
{
if (sig == "random_game")
{
print ("choosing random game...");
fe.signal("random_game");
fe.signal("select");
    }
  return false;
}

fe.add_signal_handler( "random_press" );

Adding the above to my layout.nut and it freezes, and keeps printing "choosing random game".

I see the docs for add_signal_handler() show having to return true at some point, but I'm not clear on exactly how that's used:

https://attractmode.gitlab.io/wiki/reference/functions/#feadd_signal_handler

Also, any idea how to make this work in a plugin, so it works across multiple layouts? No biggie if not, but I think the functionality would be better as a plugin if possible.

wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #6 on: November 02, 2020, 07:44:41 PM »
And looks like it's the same freeze when running it as a plugin. It's just firing that event over and over. So I guess if we solve the issue in the layouts it'll work as a plugin too.

rand0m

  • Sr. Member
  • ****
  • Posts: 337
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #7 on: November 02, 2020, 08:01:16 PM »
Thats strange Its working on my end (win 8.1) but there is nothing in the code which should cause trouble in *nix. try after removing return false from code.



wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #8 on: November 02, 2020, 08:07:24 PM »
I think I figured it out. I think it keeps firing again and again (recursion) because it's sending the "select" signal, which in turn gets caught by the function again so it fires the "select" signal, and on and on.

This works for me as a plugin, note that it's now intercepting the "custom2" signal.

Code: [Select]
function random_press(sig)
{
if (sig == "custom2")
{
//print ("choosing something....");
fe.signal("random_game");
fe.signal("select");
return true;
}
  return false;
}

fe.add_signal_handler( "random_press" );







wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #9 on: November 02, 2020, 08:10:54 PM »
And thank you! Been wanting to solve that forever.

rand0m

  • Sr. Member
  • ****
  • Posts: 337
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #10 on: November 02, 2020, 08:33:08 PM »
I tried at my end and using "return true" caused the current title to be selected and launched ignoring "random_game". Tested a bit more and following codes work with "return true":

Code#1
Code: [Select]
function random_press(sig)
{
if (sig == "random_game")
{
fe.signal("random_game") & fe.signal("select")
return true;
        }
  return false;
}

fe.add_signal_handler( "random_press" );

Code#2
Code: [Select]
function random_press( sig )
{
switch ( sig )
{
case "random_game":
fe.signal( "random_game" ) & fe.signal( "select" );
return true;
}
return false;
}
fe.add_signal_handler("random_press");

(Use only one of them at a time)

rand0m

  • Sr. Member
  • ****
  • Posts: 337
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #11 on: November 02, 2020, 08:33:48 PM »
And thank you! Been wanting to solve that forever.

np, urw :)

wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #12 on: November 02, 2020, 08:37:17 PM »
That's really odd, both of those freeze my system. As does:

Code: [Select]
function random_press(sig)
{
if (sig == "random_game")
{
fe.signal("random_game");
fe.signal("select");
return true;
        }
  return false;
}

fe.add_signal_handler( "random_press" );

Hmm.
« Last Edit: November 02, 2020, 08:39:29 PM by wrybread »

wrybread

  • Full Member
  • ***
  • Posts: 89
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #13 on: November 03, 2020, 11:43:06 AM »
And looking closer, I see that both versions you posted with the line 'fe.signal("random_game") & fe.signal("select")' throw this error:

Code: [Select]
LOCALS
[sig] "random_game"
[this] TABLE
Script Error in signal handler: random_press - bitwise op between 'null' and 'null'

AN ERROR HAS OCCURED [bitwise op between 'null' and 'null']

CALLSTACK
*FUNCTION [random_press()] /home/wrybread/.attract/plugins/Random ROM2.nut line [9]


And they throw the error over and over again, without exiting, freezing the system, so the recursion issue is still there. Odd that that would work in Windows but not Linux.

What is the & supposed to do here?

"fe.signal("random_game") & fe.signal("select")"

No biggie, all working fine with the version that listens for one of the "custom" signals, just curious.



rand0m

  • Sr. Member
  • ****
  • Posts: 337
    • View Profile
Re: A plugin to choose and run a random ROM...
« Reply #14 on: November 04, 2020, 10:34:27 AM »
fe.signal is used to simulate a mapped key signal (key press), "random_game" and "select" signal simulate their respective key presses. "random_press" is simply the function name (can be named anything).

I have no idea why this code wouldn't work on *nix, there is nothing in code which is specific to *win systems. Perhaps you can try without "select" signal to narrow down the culprit.

PS: One reason might be direct copy/pasting of code, try adding the code by hand to avoid format errors.