Up to 9000

  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
"222"+(2+2)

The above is js btw. Because the first bit is a string it will instead of simply adding the numbers together make it 2224
 
  • Like
Reactions: GamerwithnoGame

GamerwithnoGame

Over-Achiever
Jan 29, 2015
2,808
1,507
224
2225.

"222"+(2+2)

The above is js btw. Because the first bit is a string it will instead of simply adding the numbers together make it 2224
Ooh! Nice!

I don't have much faith in me being able to learn this stuff - I was reading through a series of Lua tutorials, and midway through my comprehension seemed to just stop. It was this page here, the part about "Assigning and Passing Functions" and I just do not understand what they did with it. It makes me sad :(
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
2226 ok. Let me try to explain it :)
First, lets explain why you would want to use them. Lets say you have a table
Code:
someTable={"test","some other value" ,"the last value"}
Now, lets say you want to loop over it and print out the key and the value. You would use something like this
Code:
for key, value in pairs(someTable) do
    print("The key is: ",key," the value is ", value)
end
It is at this moment that you discover that you also have another table
Code:
someOtherTable={"you didn't expect this", "did you?" }
Just like before you want to loop over it but you recognize that copying the code isn't exactly a good way to program. This is where functions come in, as a function is basically a bit of code that you can run any time you want. You already know how to run functions, as this is the same as using for example print()
So, lets write the code in such a way that it uses a function.
Code:
function printTable(tableToPrint)
    for key,value in pairs(tableToPrint)
        print("The key is: ",key," the value is ", value)
    end
end
printTable(someTable)
printTable(someOtherTable)

So, lets break it down
Code:
function printTable(tableToPrint)
the word function tells our program that we want to create a function. We then give it a name, in this case printTable. After this come the variables that the function gets when we run it. You write these always inside the "()".
[/CODE]
for key,value in pairs(tableToPrint)
print("The key is: ",key," the value is ", value)
end[/CODE]
This is the code that we want to run. Note that instead of someTable like before I now use tableToPrint. This is because tableToPrint contains the value that we passed to the function and thus it works with both "someTable" and "someOtherTable".

We then tell our program that our function is done with the final end. Much like how you tell your program that the loop is done or the if statement is done.

After we made it we simply call it. Again, this is done just like every other function like for example print.

The return statement is a way to have our function return data. Just like math.random() would. Lets write a very basic function
Code:
function add(value1,value2)
    combination=value1+value2
    return combination
end
And if we want to run it we simply do the following
Code:
someNewValue=add(2,3)
print(someNewValue)
 
  • Like
Reactions: GamerwithnoGame

GamerwithnoGame

Over-Achiever
Jan 29, 2015
2,808
1,507
224
2227.
2226 ok. Let me try to explain it :)
First, lets explain why you would want to use them. Lets say you have a table
Code:
someTable={"test","some other value" ,"the last value"}
Now, lets say you want to loop over it and print out the key and the value. You would use something like this
Code:
for key, value in pairs(someTable) do
    print("The key is: ",key," the value is ", value)
end
It is at this moment that you discover that you also have another table
Code:
someOtherTable={"you didn't expect this", "did you?" }
Just like before you want to loop over it but you recognize that copying the code isn't exactly a good way to program. This is where functions come in, as a function is basically a bit of code that you can run any time you want. You already know how to run functions, as this is the same as using for example print()
So, lets write the code in such a way that it uses a function.
Code:
function printTable(tableToPrint)
    for key,value in pairs(tableToPrint)
        print("The key is: ",key," the value is ", value)
    end
end
printTable(someTable)
printTable(someOtherTable)

So, lets break it down
Code:
function printTable(tableToPrint)
the word function tells our program that we want to create a function. We then give it a name, in this case printTable. After this come the variables that the function gets when we run it. You write these always inside the "()".
Code:
for key,value in pairs(tableToPrint)
        print("The key is: ",key," the value is ", value)
    end
This is the code that we want to run. Note that instead of someTable like before I now use tableToPrint. This is because tableToPrint contains the value that we passed to the function and thus it works with both "someTable" and "someOtherTable".

We then tell our program that our function is done with the final end. Much like how you tell your program that the loop is done or the if statement is done.

After we made it we simply call it. Again, this is done just like every other function like for example print.

The return statement is a way to have our function return data. Just like math.random() would. Lets write a very basic function
Code:
function add(value1,value2)
    combination=value1+value2
    return combination
end
And if we want to run it we simply do the following
Code:
someNewValue=add(2,3)
print(someNewValue)
Right, OK, I think I followed most of that? I'm not very bright - I feel I should say this :( So when you write "function add(blah1,blah2)", what is that doing? That's not a mathematical addition is it? Is it adding variables to a generic function? And then you name the function "combination" by stating that it has the values established in the line above?
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
when I wrote function add(value1,value2) I used the comma to separate the 2 variables that his function will get.
When I did
combination = value1+value2
I added the values of value1 and value2 together and stored it inside the variable combination

Best way to learn programming in my eyes at least is to experiment yourself. Knowing the theory about something is nice but if you never used it it is kind of useless. This is especially true when it comes to programming.
 
  • Like
Reactions: GamerwithnoGame

GamerwithnoGame

Over-Achiever
Jan 29, 2015
2,808
1,507
224
2229 (I'm going to assume a 2228 at the start of yours @lenscas)
when I wrote function add(value1,value2) I used the comma to separate the 2 variables that his function will get.
When I did
combination = value1+value2
I added the values of value1 and value2 together and stored it inside the variable combination

Best way to learn programming in my eyes at least is to experiment yourself. Knowing the theory about something is nice but if you never used it it is kind of useless. This is especially true when it comes to programming.
Hmm good point, thank you chap - I'm just trying to get my head around the syntax and stuff that's used - I think you're right, I need to just take myself through some basic steps and learn as I go along. Last question I promise: does the grammar and syntax of the ComputerCraft OS differ from basic Lua, or can I have a go at programming in there, in-game?
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
2230
2229 (I'm going to assume a 2228 at the start of yours @lenscas)

Hmm good point, thank you chap - I'm just trying to get my head around the syntax and stuff that's used - I think you're right, I need to just take myself through some basic steps and learn as I go along. Last question I promise: does the grammar and syntax of the ComputerCraft OS differ from basic Lua, or can I have a go at programming in there, in-game?
There is no difference in the syntax. There are some differences between "normal" lua and CC lua though but that is mostly in that CC has more commands, for example the whole redstone api.

The biggest differences are that doing something like
while true do
{some code that takes no time to execute}
end
will eventually stop executing on CC. To prevent this from happening use sleep() it takes a number and it will stop for that many seconds. (decimals are allowed). The sleep function is also CC only.
Another difference is the fact that you have access to os.pullEvent() which is super useful when writing code for CC .
It basically stops the program from running until an "event" happened, this can be a change in redstone, a message over rednet or a lot of other things.

But even though these differences exist I still recommend to start with something like CC as programming is something that you learn by doing and Computercraft gives plenty of reasons to program :)
 
  • Like
Reactions: GamerwithnoGame

GamerwithnoGame

Over-Achiever
Jan 29, 2015
2,808
1,507
224
2231.

2230

There is no difference in the syntax. There are some differences between "normal" lua and CC lua though but that is mostly in that CC has more commands, for example the whole redstone api.

The biggest differences are that doing something like
while true do
{some code that takes no time to execute}
end
will eventually stop executing on CC. To prevent this from happening use sleep() it takes a number and it will stop for that many seconds. (decimals are allowed). The sleep function is also CC only.
Another difference is the fact that you have access to os.pullEvent() which is super useful when writing code for CC .
It basically stops the program from running until an "event" happened, this can be a change in redstone, a message over rednet or a lot of other things.

But even though these differences exist I still recommend to start with something like CC as programming is something that you learn by doing and Computercraft gives plenty of reasons to program :)
Ah great, OK! I'll do that then :) See, I have heard about the os.pullEvent() ability! Thanks Lens, I appreciate it!
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
2232
"222"+(2+2)

The above is js btw. Because the first bit is a string it will instead of simply adding the numbers together make it 2224
That syntax would also work the same way in Java, by the way. "222"+2+2, on the other hand, would come out to "22222".
 

lenscas

Over-Achiever
Jul 31, 2013
2,015
1,801
248
2233
2232

That syntax would also work the same way in Java, by the way. "222"+2+2, on the other hand, would come out to "22222".
I kind of expected java to throw shit at you for trying to add an integer and string together. Didn't know it would act the same as JS. Which is still an horrible way to work in my eyes. Just separate the concatenation and adding into separate symbols, gives a lot less confusion and bugs
 

Someone Else 37

Forum Addict
Feb 10, 2013
1,876
1,440
168
2235
2233

I kind of expected java to throw shit at you for trying to add an integer and string together. Didn't know it would act the same as JS. Which is still an horrible way to work in my eyes. Just separate the concatenation and adding into separate symbols, gives a lot less confusion and bugs
*shrug* Well, Java does use + for string concatenation as well as addition; there's not much else to say. It also automatically .toString()'s anything you try to add to a string.

That said, using types other than the ones specified (or a subtype of the specified type) will fail to compile. Java's picky about that.