Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Missingno50

"Machine:744:Bad Argument" thrown when loading program

Question

So I'm writing a library meant to help with the keycodes, and so far, everything else works just fine. However, for some strange reason, my program throws this exception.

"machine:744: bad argument #1 to 'load' (function expected, got nil)"

This offending code seems to be keycode.lua.

--Keycodes version 1.0
function main()
trick.print("This program is meant to be run on startup only.")
trick.res()
end
do
_G.keycodes = {
2 = "1",
3 = "2",
4 = "3",
5 = "4",
6 = "5",
7 = "6",
8 = "7",
9 = "8",
10 = "9",
11 = "0",
12 = "-",
13 = "=",
15 = "  ",
16 = "Q",
17 = "W",
18 = "E",
19 = "R",
20 = "T",
21 = "Y",
22 = "U",
23 = "I",
24 = "O",
25 = "P",
26 = "[",
27 = "]",
30 = "A",
31 = "S",
32 = "D",
33 = "F",
34 = "G",
35 = "H",
36 = "J",
37 = "K",
38 = "L",
39 = ";",
40 = "'",
41 = "`",
43 = "\",
44 = "Z",
45 = "X",
46 = "C",
47 = "V",
48 = "B",
49 = "N",
50 = "M",
51 = ",",
52 = ".",
53 = "/",
54 = "",
55 = "*",
57 = " "
}
end
function trick.convert(key)
return keycodes[key]
end

However, I don't quite see the issue. What's wrong with my code?

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hey,

So the given error code does not belong the the code fragment shown, but I might be able to help you anyways.

Your issue in this snippet comes from an incorrect way of using the table constructor. There are basically 3 possibilities to use it.

#1 array-type tables

--# for generating array type tables with table costructors,
--# simply list the values using a comma as a seperator

local t = {"a", "b", "c"}

--# this initializes a table t with the values "a", "b" and "c" at
--# indices 1, 2 and 3

#2 tables with strings as keys

--# tables with key-value pairs where the key is a valid identifier
--# a valid identifier is what you can name variables
--# it's a string which starts with a letter or underscore followed by 0 or more letters, underscores or numbers
--# they cannot be keywords like 'end', 'local', etc

local t = {
  first = 1,
  last = 5,
  _middle = 3
 }

#3 universal

--# the universal way, where you can use values of every type as keys
--# simply enclose variables or objects in sqaure brackets

local t = {
  [6] = 4,              --# using a number as key
  [print] = "test",     --# variable and function as key
  [{1,2,3}] = "table",  --# table as key
  [true] = 5,           --# booleans as key
  [false] = 3,
  ["hello"] = "world",  --# string as key
  ["end"] = true        --# keyword as key
}

In your code, you are using the constructor for tables with valid identifiers as keys, which you can't, since numbers aren't valid identifiers.

You either have to use the universal approach or the array type constructor

_G.keycodes = {
  [2] = "1",
  [3] = "2",
  [4] = "3",
  [5] = "4",
  [6] = "5",
  [7] = "6",
  [8] = "7",
  [9] = "8",
  [10] = "9",
  [11] = "0",
  [12] = "-",
  [13] = "=",
  [15] = "  ",
  [16] = "Q",
  [17] = "W",
  [18] = "E",
  [19] = "R",
  [20] = "T",
  [21] = "Y",
  [22] = "U",
  [23] = "I",
  [24] = "O",
  [25] = "P",
  [26] = "[",
  [27] = "]",
  [30] = "A",
  [31] = "S",
  [32] = "D",
  [33] = "F",
  [34] = "G",
  [35] = "H",
  [36] = "J",
  [37] = "K",
  [38] = "L",
  [39] = ";",
  [40] = "'",
  [41] = "`",
  [43] = "\\",	--# also, you cant have an single '\' in a lua file, since it is the escape character, for a literal '\' in a lua file you use '\\' instead
  [44] = "Z",
  [45] = "X",
  [46] = "C",
  [47] = "V",
  [48] = "B",
  [49] = "N",
  [50] = "M",
  [51] = ",",
  [52] = ".",
  [53] = "/",
  [54] = "",
  [55] = "*",
  [57] = " "
}
--# or using the array type constructor, but remember to fill in nils at the positions where you dont want to have values

_G.keycodes = {
  nil, "1", "2", "3", "4", "5", "6", "7", "8", "9",
  "0", "-", "=", nil, "  ", "Q", "W", "E", "R", "T",
  "Y", "U", "I", "O", "P", "[", "]", nil, nil, "A",
  "S", "D", "F", "G", "H", "J", "K", "L", ";", "'",
  "`", nil, "\\", "Z", "X", "C", "V", "B", "N", "M",
  ",", ".", "/", "", "*", nil, " "
}

As I said, the shown part of the error code does not help with the given code fragment, but I guess it belongs to a 'require' function call, but I may be wrong.

Anyways I hope this fixes it for you. If you have further questions, just let me know.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.