Author Topic: [SOLVED] Adding Keybinds Breaks Game  (Read 705 times)

I made a thread in the Help section about this earlier. I've recently been made aware that it's likely an error in my coding. Essentially, add-ons I create with keybinds tend to conflict with other add-ons. When they do, Blockland will load the same add-ons over and over on startup, until I decide to close the game.

Here's the code for an add-on I recently posted that has caused the problem for me and another user:
Code: [Select]
$remapDivision[$remapCount] = "Avatar Favorites";
for (%i = 1; %i < 10; %i++) {
$remapName[$remapCount] = "Switch to Avatar Favorite #" @ %i;
$remapCmd[$remapCount] = "AK_select" @ %i;
$remapCount++;
}
$remapName[$remapCount] = "Switch to Avatar Favorite #0";
$remapCmd[$remapCount] = "AK_select0";
$remapCount++;

function AK_select(%isDown, %i) {
if (%isDown) {
%fn = "config/client/AvatarFavorites/" @ %i @ ".cs";
if (isFile(%fn)) {
exec(%fn);
echo("Switched to avatar favorite #" @ %i);
}
else {
echo("No config found for avatar favorite #" @ %i);
}
clientCmdUpdatePrefs();
}
}

function AK_select1(%isDown) { AK_select(%isDown, 1); }
function AK_select2(%isDown) { AK_select(%isDown, 2); }
function AK_select3(%isDown) { AK_select(%isDown, 3); }
function AK_select4(%isDown) { AK_select(%isDown, 4); }
function AK_select5(%isDown) { AK_select(%isDown, 5); }
function AK_select6(%isDown) { AK_select(%isDown, 6); }
function AK_select7(%isDown) { AK_select(%isDown, 7); }
function AK_select8(%isDown) { AK_select(%isDown, 8); }
function AK_select9(%isDown) { AK_select(%isDown, 9); }
function AK_select0(%isDown) { AK_select(%isDown, 0); }

What could I be doing wrong? Nothing stands out to me.

I should also note that the same problem has occurred for other, even simpler add-ons, with single keybinds. Usually I just put something like this at the top of my code:
Code: [Select]
$remapDivision[$remapCount] = "Test Division";
$remapName[$remapCount] = "Test Keybind";
$remapCmd[$remapCount] = "TEST_callback";
$remapCount++;
« Last Edit: December 11, 2017, 01:12:36 AM by Platypi »

try taking out the call to updateprefs

try taking out the call to updateprefs
I'll try to see if I can replicate the issue again and do this. How would this be a problem on startup, though? Are commands in $remapCmd ran on startup?

EDIT: And... now I can't seem to replicate it. Let me see if the user mentioned below would be willing to test the add-on without clientCmdUpdatePrefs().



I should note, too, that removing the keybinding/$remap... section of the script does solve the problem (I recall trying it earlier).



Another user has supplied his console.log demonstrating the problem. It shows the same add-ons being loaded multiple times. File attached.
« Last Edit: December 05, 2017, 12:12:52 AM by Platypi »

So I tested out the modified version of the add-on and I still can't start the game up when installed. I attached my console log.
« Last Edit: December 05, 2017, 12:29:18 AM by Cowboy Dude »

The only difference I can see in your code and other add-ons is that you don't have any sort of check to prevent the binds from applying multiple times.
Which... should in no shape or form cause this to happen but let me show you a way to at least fix that.

Code: [Select]
if(!$loadedAvatarFavsBinds)
{
$remapDivision[$remapCount] = "Avatar Favorites";
for (%i = 1; %i < 10; %i++) {
$remapName[$remapCount] = "Switch to Avatar Favorite #" @ %i;
$remapCmd[$remapCount] = "AK_select" @ %i;
$remapCount++;
}
$remapName[$remapCount] = "Switch to Avatar Favorite #0";
$remapCmd[$remapCount] = "AK_select0";
$remapCount++;
$loadedAvatarFavsBinds = 1;
}

function AK_select(%isDown, %i) {
if (%isDown) {
%fn = "config/client/AvatarFavorites/" @ %i @ ".cs";
if (isFile(%fn)) {
exec(%fn);
echo("Switched to avatar favorite #" @ %i);
}
else {
echo("No config found for avatar favorite #" @ %i);
}
clientCmdUpdatePrefs();
}
}

function AK_select1(%isDown) { AK_select(%isDown, 1); }
function AK_select2(%isDown) { AK_select(%isDown, 2); }
function AK_select3(%isDown) { AK_select(%isDown, 3); }
function AK_select4(%isDown) { AK_select(%isDown, 4); }
function AK_select5(%isDown) { AK_select(%isDown, 5); }
function AK_select6(%isDown) { AK_select(%isDown, 6); }
function AK_select7(%isDown) { AK_select(%isDown, 7); }
function AK_select8(%isDown) { AK_select(%isDown, 8); }
function AK_select9(%isDown) { AK_select(%isDown, 9); }
function AK_select0(%isDown) { AK_select(%isDown, 0); }


See if that makes a difference.

The only difference I can see in your code and other add-ons is that you don't have any sort of check to prevent the binds from applying multiple times.
Which... should in no shape or form cause this to happen but let me show you a way to at least fix that.

Code: [Select]
if(!$loadedAvatarFavsBinds)
{
$remapDivision[$remapCount] = "Avatar Favorites";
for (%i = 1; %i < 10; %i++) {
$remapName[$remapCount] = "Switch to Avatar Favorite #" @ %i;
$remapCmd[$remapCount] = "AK_select" @ %i;
$remapCount++;
}
$remapName[$remapCount] = "Switch to Avatar Favorite #0";
$remapCmd[$remapCount] = "AK_select0";
$remapCount++;
$loadedAvatarFavsBinds = 1;
}

function AK_select(%isDown, %i) {
if (%isDown) {
%fn = "config/client/AvatarFavorites/" @ %i @ ".cs";
if (isFile(%fn)) {
exec(%fn);
echo("Switched to avatar favorite #" @ %i);
}
else {
echo("No config found for avatar favorite #" @ %i);
}
clientCmdUpdatePrefs();
}
}

function AK_select1(%isDown) { AK_select(%isDown, 1); }
function AK_select2(%isDown) { AK_select(%isDown, 2); }
function AK_select3(%isDown) { AK_select(%isDown, 3); }
function AK_select4(%isDown) { AK_select(%isDown, 4); }
function AK_select5(%isDown) { AK_select(%isDown, 5); }
function AK_select6(%isDown) { AK_select(%isDown, 6); }
function AK_select7(%isDown) { AK_select(%isDown, 7); }
function AK_select8(%isDown) { AK_select(%isDown, 8); }
function AK_select9(%isDown) { AK_select(%isDown, 9); }
function AK_select0(%isDown) { AK_select(%isDown, 0); }


See if that makes a difference.
This will at least make testing easier. I'll give it a shot later tonight.

Sorry for taking so long to post an update (been really busy). I have heeded the suggestions of Shift Kitty and Kyuande.
Your mod is causing console errors because you put a local variable outside of a function. The %i is already used while the game boots up so you're setting it again which is why it causes the huge problem. Please fix this immediately.
Here's the code now:
Code: [Select]
if (!$AK_isLoaded) {
$remapDivision[$remapCount] = "Avatar Favorites";
for (%AK_i = 1; %AK_i < 10; %AK_i++) {
$remapName[$remapCount] = "Switch to Avatar Favorite #" @ %AK_i;
$remapCmd[$remapCount] = "AK_select" @ %AK_i;
$remapCount++;
}
$remapName[$remapCount] = "Switch to Avatar Favorite #0";
$remapCmd[$remapCount] = "AK_select0";
$remapCount++;
$AK_isLoaded = 1;
}

function AK_select(%isDown, %i) {
if (%isDown) {
%fn = "config/client/AvatarFavorites/" @ %i @ ".cs";
if (isFile(%fn)) {
exec(%fn);
echo("Switched to avatar favorite #" @ %i);
}
else {
echo("No config found for avatar favorite #" @ %i);
}
clientCmdUpdatePrefs();
}
}

function AK_select1(%isDown) { AK_select(%isDown, 1); }
function AK_select2(%isDown) { AK_select(%isDown, 2); }
function AK_select3(%isDown) { AK_select(%isDown, 3); }
function AK_select4(%isDown) { AK_select(%isDown, 4); }
function AK_select5(%isDown) { AK_select(%isDown, 5); }
function AK_select6(%isDown) { AK_select(%isDown, 6); }
function AK_select7(%isDown) { AK_select(%isDown, 7); }
function AK_select8(%isDown) { AK_select(%isDown, 8); }
function AK_select9(%isDown) { AK_select(%isDown, 9); }
function AK_select0(%isDown) { AK_select(%isDown, 0); }

I was able to replicate the original error, and independently verify that Kyuande's suggestion fixed the problem. Here's a link to the fixed add-on if anyone would like to verify that this fix works for others.

Hey dude it worked for me!

Hey dude it worked for me!
Awesome! I suppose I'll mark the topic as [SOLVED], as long as no one else has any more issues with it.