Jump to content




Help with a typing program.


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

#1 Bossman201

  • New Members
  • 92 posts

Posted 17 May 2012 - 12:58 AM

Hello, I'm having some trouble with my code. I've declared a few functions or whatever and used them in a loop and the genius in me has gotten a bug in 5 lines of code. Anyway, I've looked through it a ton and can't find any reasonable errors. Then again, I've been like that since high school.

A little about me before I get on to posting the code. I'm 19, graduated from HS 2 years ago and started college but had to move so now I'm not in school and I haven't programmed in 3 years. Anyway now that you're all bored here's the code.

Spoiler

I'll explain the error I'm having but putting the script on an in-game computer might help more. I guess my syntax is all okay since I can run the program, but when I try to give it input, the first time i press a button it's okay, it outputs normally, but when you hit another button it will print out again the last letter you typed and the new letter. I really just can't figure it out. Hopefully some fresh eyes will be able to help. Thanks in advance, guys.

EDIT: I think I messed up the commenting, sorry. I'll figure it out and fix it right away.

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 17 May 2012 - 01:09 AM

First, some suggestions:
1) Don't use global variables, and definitely don't use them to pass vars between functions, that's what parameters and return are for.
2) The char event already returns the correct character when shift is pressed, so the Decode() function is kinda pointless.

The problem is that the variable letter stores the last input key, and you print it whenever there's an event, so you get a "key" event and then a "char" event, and you write the letter for both events (that's why it repeats the last one). It would also happen for every event you get (like "rednet_message", "redstone", etc.).
Try with the suggestions i gave you, that should help you solve the problem.

#3 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 17 May 2012 - 01:16 AM

View PostMysticT, on 17 May 2012 - 01:09 AM, said:

First, some suggestions:
1) Don't use global variables, and definitivly don't use them to pass vars between functions, that's what parameters and return are for.
2) The char event already returns the correct character when shift is pressed, so the Decode() function is kinda pointless.

The problem is that the variable letter stores the last input key, and you print it whenever there's an event, so you get a "key" event and then a "char" event, and you write the letter for both events (that's why it repeats the last one). It would also happen for every event you get (like "rednet_message", "redstone", etc.).
Try with the suggestions i gave you, that should help you solve the problem.

Another thing is the decode() function is broken since the event "key" doesn't return strings, it returns integers. So it would be:
function Decode()  --This is how the program is supposed to decode os.pullEvent() events and prepare them for sending.
		if event == "key" and param1 == 42 and shift == false then shift = true  --Allows shift key functionality.
		elseif event == "key" and param1 == 42 and shift == true then shift = false
		end
instead of

function Decode()  --This is how the program is supposed to decode os.pullEvent() events and prepare them for sending.
		if event == "key" and param1 == "42" and shift == false then shift = true  --Allows shift key functionality.
		elseif event == "key" and param1 == "42" and shift == true then shift = false
		end


#4 BigSHinyToys

  • Members
  • 1,001 posts

Posted 17 May 2012 - 01:27 AM

Simple bug keys are integers not strings.
		if event == "key" and param1 == "42" and shift == false then shift = true  --Allows shift key functionality.
		elseif event == "key" and param1 == "42" and shift == true then shift = false
-- Should be --
		if event == "key" and param1 == 42 and shift == false then shift = true  --Allows shift key functionality.
		elseif event == "key" and param1 == 42 and shift == true then shift = false

EDIT I got Ninja-ed :P/>

#5 Bossman201

  • New Members
  • 92 posts

Posted 17 May 2012 - 01:36 AM

So I'm guessing my global variables are event, param1, and param 2? I didn't really pay attention in that lecture haha so I don't know what global variables are and how to use them, so I try to stay away from them.

1. How can I get variable letter to only store either the key and number or the char and letter, and which one would be better? (I want to be able to use all the characters on a standard keyboard ex. | ] [ { } ' " ; : / ` ~ in addition to uppercase and lowercase letters and numbers and symbols.)

2. Okay I see what you mean about the key event not returning strings, so I need to keep myself from putting quotation marks around them.

3. When I declare a function, how do I declare and use the parameters? I know you need to put them in like this Decode(letter, compID) or whatever but not how to use them. (I may have slightly dozed off during this lecture as well.)

I'll add these things to the program and take out Decode() as soon as I figure out how to only grab the char event. Maybe os.pullEvent(char)?

#6 ComputerCraftFan11

  • Members
  • 771 posts
  • LocationHawaii

Posted 17 May 2012 - 01:42 AM

View PostBossman201, on 17 May 2012 - 01:36 AM, said:

So I'm guessing my global variables are event, param1, and param 2? I didn't really pay attention in that lecture haha so I don't know what global variables are and how to use them, so I try to stay away from them.

1. How can I get variable letter to only store either the key and number or the char and letter, and which one would be better? (I want to be able to use all the characters on a standard keyboard ex. | ] [ { } ' " ; : / ` ~ in addition to uppercase and lowercase letters and numbers and symbols.)

2. Okay I see what you mean about the key event not returning strings, so I need to keep myself from putting quotation marks around them.

3. When I declare a function, how do I declare and use the parameters? I know you need to put them in like this Decode(letter, compID) or whatever but not how to use them. (I may have slightly dozed off during this lecture as well.)

I'll add these things to the program and take out Decode() as soon as I figure out how to only grab the char event. Maybe os.pullEvent(char)?

For 3*
you would call Decode like this:
"Decode(3, 3)"
Then in decode(), it would print 33 if you type:
function decode(p1, p2)
print(p1..""..p2)
For the end*

its evt, letter = os.pullEvent("char")

#7 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 17 May 2012 - 01:49 PM

1) Well, I didn't see the local definition of the variables :P/>, but what i meant was that you could make the functions receive parameters and return values instead of having them change those vars. So you could have a function to get a letter like this:
local function getLetter()
  local evt, letter = os.pullEvent("char") -- get a "char" event
  return letter -- return the typed character
end
And then you use it like:
while true do
  local c = getLetter() -- get the letter and store it in a var
  write(c) -- write the letter
end
This would make the program write anything you type, but only characters, so you can't move the cursor, delete or press enter. You need a loop that checks for any type of event you want, like this:
while true do
  local evt, p1, p2 = os.pullEvent()
  if evt == "char" then
    writeChar(p1)
  elseif evt == "key" then
    handleKey(p1)
  elseif evt == "rednet_message" then
    messageReceived(p1, p2)
  end
end
And then you make the functions to handle them.

2) The "char" event gives a string containing the character typed, and if shift is pressed it handles that for you.

3) To use functions with parameters you do:
local function doSomething(p1, p2, p3) -- give a name to the received parameters
  print(p1) -- print the first parameter
  return p2 + p3 -- return the sum of the other parameters
end
And use it like:
local n = doSomething("Hello", 2, 4) -- give parameters "Hello", 2 and 4. Store the returned value in a variable
print(n)
It would print the first parameter ("Hello") and the print the returned value, wich is the sum of the second and third parameters (2 and 4, prints 6).

#8 Bossman201

  • New Members
  • 92 posts

Posted 18 May 2012 - 06:33 PM

Okay guys, two unfinished versions and three days later I finally have a functional program.

Spoiler

It's a host/client program so run the same program on two computers to use. I think I may stick with the project and add more features/stuff. Everybody say "inb4 'or you could just use minecraft chat'."!!!

EDIT: Question. What command do I use to copy/move a program into a disk, either using the lua program or in the programming environment? I've been wondering that for the past three days.

EDIT2: Simple typo in my code. typed "of" instead of "or" in the handleCommand() function.

EDIT3: Don't forget to edit the rednet.open() command if your modem is placed anywhere other than the right side or else it won't work. It's right under my variable declarations. Might add support to do this in the program later.

Edited by Bossman201, 18 May 2012 - 06:52 PM.


#9 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 18 May 2012 - 06:40 PM

From a program you can use the fs api to manage files (copy, move, delete, open). To copy a file use: fs.copy("sourcePath", "destPath"). and move is the same but "move" instead of "copy".

#10 Bossman201

  • New Members
  • 92 posts

Posted 18 May 2012 - 08:34 PM

So how exactly would I move "myFile" to the disk using fs.copy()?

#11 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 18 May 2012 - 09:24 PM

If its on the computers root directory

fs.copy("myFile", "disk/myFile")


#12 Bossman201

  • New Members
  • 92 posts

Posted 18 May 2012 - 09:26 PM

So then if it's in another directory it would be

fs.copy("myFolder/myFile", "disk/myNewFolder/myFile")

right?

#13 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 18 May 2012 - 09:28 PM

Yes, but the directory must already exist on the disk (the "myNewFolder" folder).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users