Jump to content




[CC]Code Cleanup


  • You cannot reply to this topic
170 replies to this topic

#61 Creeper367

  • Members
  • 62 posts

Posted 07 July 2013 - 09:48 PM

Ill post a new error scince no one has...

YourFace = "Hello"
Print(YourFace)
Worldz = "World!"
Print(World)
end


#62 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 08 July 2013 - 01:59 AM

View PostCreeper367, on 07 July 2013 - 09:48 PM, said:

Ill post a new error scince no one has...

YourFace = "Hello"
Print(YourFace)
Worldz = "World!"
Print(World)
end
The "end" at the end shouldn't be there, you have one time Worldz and one time World and print has wrong capitalization.

#63 Tjakka5

  • Members
  • 256 posts

Posted 08 July 2013 - 10:41 AM

YourFace = "Hello"
print(YourFace)
Worldz = "World!"
print(Worldz)

My turn :D

local symbols = {"@", "#", "$", "%", "&", "*"}
local colors = {colors.green, colors.blue, colors.yellow, colors.lime, colors.white, colors.magenta, colors.black}

local x == 0
local y == 0

function printScreenRan(amoSym)
  for i = 1, 19 do
	y = i
	for I = 1, 20
	  x = I
	  num = Math.random(amoSym)
	  term.setCursorPos(xPos, yPos)
	
	  term.setBackGroundColor(colors[8])
	  term.setTextColor(colors[num])
	  print("" ..symbols[numbers])
	end
  end
end

printScreenRan(6)

Spoiler


#64 reububble

  • Members
  • 72 posts
  • LocationBehind you

Posted 18 July 2013 - 06:40 AM

local symbols = {"@", "#", "$", "%", "&", "*"}
local colors = {colors.green, colors.blue, colors.yellow, colors.lime, colors.white, colors.magenta, colors.black}

local x = 0
local y = 0

function printScreenRan(amoSym)
  for i = 1, 19 do
	y = i
	for I = 1, 20
	  x = I
	  num = math.random(amoSym)
	  term.setCursorPos(x, y)
	  term.setBackgroundColor(colors[7])
	  term.setTextColor(colors[num])
	  print(symbols[num])
	end
  end
end

printScreenRan(6)

parallel.waitForBoth(
 Function()
  while True do
   os.pullevent("redstone")
  end
 end,
  print read(*)


#65 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 19 July 2013 - 02:22 AM

View Postreububble, on 18 July 2013 - 06:40 AM, said:

local symbols = {"@", "#", "$", "%", "&", "*"}
local colors = {colors.green, colors.blue, colors.yellow, colors.lime, colors.white, colors.magenta, colors.black}

local x = 0
local y = 0

function printScreenRan(amoSym)
  for i = 1, 19 do
	y = i
	for I = 1, 20
	  x = I
	  num = math.random(amoSym)
	  term.setCursorPos(x, y)
	  term.setBackgroundColor(colors[7])
	  term.setTextColor(colors[num])
	  print(symbols[num])
	end
  end
end

printScreenRan(6)

parallel.waitForBoth(
Function()
  while True do
   os.pullevent("redstone")
  end
end,
  print read(*)

There is no parallel.waitForBoth()
Function should be function
True should be true
os.pullevent should be os.pullEvent
You didn't close waitForBoth()

#66 CraftedCart

  • Members
  • 67 posts
  • LocationUnited Kingdom, Earth

Posted 11 August 2013 - 04:04 AM

x = 2
y = 3
Z = 4

term.write("This is a string)
term.setCursorPos(x, y)
term.write(x + z)
If x + z = 7 then
   print "yay"
else
   print "nay"
End


#67 jay5476

  • Members
  • 289 posts

Posted 11 August 2013 - 04:48 PM

First print does not close the string and If is if and End is end
function a()
print(a)
end
function b(nomnom)
print(nomnom)
end
while true do
parallel.waitForAny(b(), a())
end


#68 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 14 August 2013 - 11:33 AM

View Postjay5476, on 11 August 2013 - 04:48 PM, said:

First print does not close the string and If is if and End is end
function a()
print(a)
end
function b(nomnom)
print(nomnom)
end
while true do
parallel.waitForAny(b(), a())
end
The only thing that will actually make it crash is the brackets after the a and the b in in the 8th line.

#69 Sangar

  • Members
  • 30 posts

Posted 14 August 2013 - 01:58 PM

If you won't post code, I will ;)

This is using two files, but an all-in-one is below if that's a requirement I missed.
File testapi looks like this:
function printName()
  print(getfenv(2).shell.getRunningProgram())
end
File testprogram looks like this:
os.loadAPI("testapi")
return testapi.printName()
In the shell, run testprogram like so:
> testprogram
testapi:2: attempt to index ? (a nil value)
> _

All in one:
Spoiler


#70 jay5476

  • Members
  • 289 posts

Posted 24 August 2013 - 12:30 AM

Quote

Posted 14 August 2013 - 05:58 AM
If you won't post code, I will ;)

This is using two files, but an all-in-one is below if that's a requirement I missed.
File testapi looks like this:

function printName()
print(getfenv(2).shell.getRunningProgram())
endFile testprogram looks like this:

os.loadAPI("testapi")
return testapi.printName()In the shell, run testprogram like so:

> testprogram
testapi
:2: attempt to index ? (a nil value)
> _
All in one:

Spoiler


_G.testapi = { printName = setfenv(function()
print(getfenv(2).shell.getRunningProgram())
end,
setmetatable({}, {__index = _G})) }
local function program()
return testapi.printName()
end
local success, result = pcall(setfenv(program, setmetatable({shell = shell}, {__index = _G})))
if not success then
print(result)
end

you havnt defined the function 2 so getfenv(2) will error
mine :)
str = "table"
someTable = {"some","useless","table","thing"}
for k, v in pairs(someTable)
if str = v then
table.remove(someTable, k)
end
end


#71 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 24 August 2013 - 01:19 AM

View Postjay5476, on 24 August 2013 - 12:30 AM, said:

you havnt defined the function 2 so getfenv(2) will error
Well that isn't the problem....
Firstly, you can't name a function or variable with a number, or have it begin with a number, that is invalid syntax...
Secondly, before attempting to solve when people use environments (one of the more complicated topics in Lua), I suggest that you have a better understanding of how it works! `getfenv(2)` is perfectly valid and will not error!

The better thing to say here that on a freshly booted computer, environment 2 does not contain a shell table, environment 1 does, meaning that you'll get the error

Quote

attempt to index ? (a nil value)

View Postjay5476, on 24 August 2013 - 12:30 AM, said:

mine :)
str = "table"
someTable = {"some","useless","table","thing"}
for k, v in pairs(someTable)
if str = v then
table.remove(someTable, k)
end
end
Error #1

Quote

bios:337: [string "tester"]:4: 'do' expected

Error #2

Quote

bios:337: [string "tester"]:4: 'then' expected

I guess I better post one to keep the game going......
local t = {
  ['key'] = "value",
  ['foo'] = "bar"
}

local function 1one()
  for k,v in next(t) do
	t[k] = nil
  end
end

one()


#72 jay5476

  • Members
  • 289 posts

Posted 24 August 2013 - 02:09 AM

View Posttheoriginalbit, on 24 August 2013 - 01:19 AM, said:

View Postjay5476, on 24 August 2013 - 12:30 AM, said:

you havnt defined the function 2 so getfenv(2) will error
Well that isn't the problem....
Firstly, you can't name a function or variable with a number, or have it begin with a number, that is invalid syntax...
Secondly, before attempting to solve when people use environments (one of the more complicated topics in Lua), I suggest that you have a better understanding of how it works! `getfenv(2)` is perfectly valid and will not error!

The better thing to say here that on a freshly booted computer, environment 2 does not contain a shell table, environment 1 does, meaning that you'll get the error

Quote

attempt to index ? (a nil value)

View Postjay5476, on 24 August 2013 - 12:30 AM, said:

mine :)
str = "table"
someTable = {"some","useless","table","thing"}
for k, v in pairs(someTable)
if str = v then
table.remove(someTable, k)
end
end
Error #1

Quote

bios:337: [string "tester"]:4: 'do' expected

Error #2

Quote

bios:337: [string "tester"]:4: 'then' expected

I guess I better post one to keep the game going......
local t = {
  ['key'] = "value",
  ['foo'] = "bar"
}

local function 1one()
  for k,v in next(t) do
	t[k] = nil
  end
end

one()

well theres one more error that I know of and you helped me solve it for me yesterday :P
but as for your error, you state one in your reply
#error1
you cannot start a function with a number
#error2
no function called one has been defined

#73 Anavrins

  • Members
  • 775 posts

Posted 24 August 2013 - 02:15 AM

View Posttheoriginalbit, on 24 August 2013 - 01:19 AM, said:

local t = {
  ['key'] = "value",
  ['foo'] = "bar"
}

local function 1one()
  for k,v in next(t) do
	t[k] = nil
  end
end

one()


local t = {
  ['key'] = "value",
  ['foo'] = "bar"
}

local function one()
  for k,v in next, t do
	t[k] = nil
  end
end

one()

Variable cannot start with a number (you can have a number in it, but not as the first character);
Not sure about this one but the "next" iterator is used like this "for k,v in next, t do....";
The "one" function doesn't exist since it would have errored at "function 1one()".

local mon = peripheral.warp("buttom")
local mSize = mon.getSizes()

for i == 1, mSize.y, 1 then
  for j == 1, mSize.x then
	mon.setTextColors(2^random(0,16)
	mon.setCursorPos(i, j)
	mon.print(" ")
end


#74 Sangar

  • Members
  • 30 posts

Posted 24 August 2013 - 05:06 PM

View Postjay5476, on 24 August 2013 - 12:30 AM, said:

you havnt defined the function 2 so getfenv(2) will error
As theoriginalbit thoroughly explained, that's not the point, but...

View Posttheoriginalbit, on 24 August 2013 - 01:19 AM, said:

The better thing to say here that on a freshly booted computer, environment 2 does not contain a shell table, environment 1 does, meaning that you'll get the error

That is also not the right answer ;)

Consider, when adjusting testprogram to this:
os.loadAPI("testapi")
return testapi.printName() or nil
An then run it:
> testprogram
testprogram
> _

Why? Because otherwise it's a tail call. Therefore getfenv(2) fails, because the calling environment (that of testprogram, which does contain the shell table) doesn't exist anymore. In fact, any good Lua VM will tell you this when you run getfenv(2) (try it on ideone.com for example). But in CC it appears to silently fail and return nil instead.

#75 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 August 2013 - 01:22 AM

View Postjay5476, on 24 August 2013 - 02:09 AM, said:

well theres one more error that I know of and you helped me solve it for me yesterday :P
It will actually work, there wont be the cannot get next to nil error as it's working with indexed values, not key/values.

#76 RatcheT2497

  • Members
  • 72 posts
  • LocationSlovakia

Posted 30 August 2013 - 11:21 AM

Welp, you didn't post code, so here's my turn.

guy = "@"
guyX = 9
guyY = 9
while true do
event, key = os.pullEvent("key")
If key == keys.Left then
guyX = guyX-1
elseIf key == keys.right then
guyX = guyX+1
Elseif key == keys.up then
guyY = guyY -1
elSeif key == keys.down then
guyY = guyY+1
end
term.clear()
term.setCursorPos(guyX, guyY)
print(guy)
end


#77 DarkEspeon

  • Members
  • 57 posts

Posted 30 August 2013 - 11:58 PM

View PostRatcheT2497, on 30 August 2013 - 11:21 AM, said:

Welp, you didn't post code, so here's my turn.

guy = "@"
guyX = 9
guyY = 9
while true do
event, key = os.pullEvent("key")
if key == keys.left then
guyX = guyX-1
elseif key == keys.right then
guyX = guyX+1
elseif key == keys.up then
guyY = guyY -1
elseif key == keys.down then
guyY = guyY+1
end
term.clear()
term.setCursorPos(guyX, guyY)
print(guy)
end

HOLY CAPITALIZATION BATMAN(see what i did there?)

term.print "Bite my face"
term.SetCursorPOS("5", "6")
term.print "NO WAY!'

(Hurts my brain to purposfully write incorrect code...)

#78 jay5476

  • Members
  • 289 posts

Posted 31 August 2013 - 12:32 AM

there is no term.print and its term.setCursorPos(num,num) you use 5 and 6 as strings instead of numbers and the last term.print uses to different quotation marks
my code :)
w,h = term.getSize()
someTable = {}
for x = 1, w do
someTable[x] = x
for y = 1, h do
someTable[x][y] = y
end
end


#79 adencraft2000

  • Members
  • 43 posts

Posted 04 September 2013 - 09:20 PM

View Postjay5476, on 31 August 2013 - 12:32 AM, said:

there is no term.print and its term.setCursorPos(num,num) you use 5 and 6 as strings instead of numbers and the last term.print uses to different quotation marks
my code :)
w,h = term.getSize()
someTable = {}
for x = 1, w do
someTable[x] = x
for y = 1, h do
someTable[x][y] = y
end
end

I think it needs to be:
w,h = term.getSize() -- Get the size of the screen
someTable = {} -- Define a variable
for x = 1, w do -- Loop from 1 through width
someTable[x] = x -- idk
end -- End
for y = 1, h do -- Loof from 1 through height
someTable[x][y] = y -- idk lol
end -- End
A non-commented version of the above:
w,h = term.getSize()
someTable = {}
for x = 1, w do
someTable[x] = x
end
for y = 1, h do
someTable[x][y] = y
end

Now my code:
(You can find all my scripts at pastebin.com/u/ADENCRAFT20000)

local timesToRepeat = tonumber(sTimes)
if timesToRepeat == nil then
print("Error: You need to say how many times to loop")
error()
end
for i = 1, timesToRepeat do
print("lol") - Print a message
end


#80 jay5476

  • Members
  • 289 posts

Posted 08 September 2013 - 03:08 AM

nope your wrong... and sTimes isnt defined





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users