This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
AppleGamer711's profile picture

Program help

Started by AppleGamer711, 05 January 2018 - 10:49 PM
AppleGamer711 #1
Posted 05 January 2018 - 11:49 PM
Hello!
I have been thinking about an exciting mining turtle programm, however I can't writte it in lua language. If it was in c++ language it would be easy. Can I get some help from someone who knows lua? I can write it in c++ for you to understand it better.
thank you.
Lyqyd #2
Posted 06 January 2018 - 06:37 AM
Moved to General.

That's not really what we do in Ask a Pro. Why not just learn Lua?
Saldor010 #3
Posted 06 January 2018 - 10:29 AM
Arguably, Lua is easier to write in than C++ is, and the underlying programming concepts are mostly staying the same. Why not take the time to learn a new language?
Purple #4
Posted 06 January 2018 - 12:53 PM
If I may provide some input here. Mainly because I can understand his frustration. I'll try to also be helpful near the end.

LUA is definitively not easier to write than C++. This is speaking as someone who knows both, and quite a few others on the side. What they are is roughly equally hard but radically different. C++ is a strongly typed language with very definitive features such as compulsory bracket use and end of command terminators. Lua has none of those things.

For those of you that don't know what all this means TLDR:
  • In C++ a number is a number, a string is a string, a character is a character etc. When you define a variable as an integer that variable can only ever be used as an integer. So you can't just say local C = "name" and than go C = 3 in the next line.
  • Well you can, but the results are going to be interesting in the Chinese proverb kind of way.
  • In C++ when you define a function, a conditional statement (IF THEN ELSE), a loop (FOR, WHILE etc.) or other such stuff you have to put the body of that function in {}. And every time you write a line you have to put ; at the end.
  • So a C++ function looks something like this:

int add(int a, int B)/>
{
	 return a + b;
}

Where as the equivalent LUA function looks like this:

function add(a, B)/>
	return a+b

The consequence of this is that C++ looks and feels much more ordered. You always know what the type of something is. You always know what belongs where. So to someone used to C++ and its beautiful code LUA code basically looks messy.
It's much harder to look at examples written in LUA and figure them out because you have to unlearn all those things you know and try to find just how the hell to guess what belongs where.

So yea, I definitively prefer C++. Anyway, to round this up on a helpful note my advice to you is that if you are a programmer coming from actual programming and into this you should start by making little makework projects for your self in the game that you can acomplish through modifying the existing examples on the Wiki.

So nothing massive, nothing huge. Stuff like: Take input from a sensor and send it over rednet to a remote PC. or Replace my buildcraft pipe warehouse with a turtle that does deliveries. And always keep the LUA reference manual open on the side. Once you get the feel of things, you can start letting your inner engineer take over and have crazy fun building things bigger, faster and stronger.

And trust me this goes remarkably fast once you start. Like in my case I started with a remote inventory sensor to monitor the HP of my IC2 windmill rotor and ended up with a wireless version of the Sensorview utility that you get with OPENCCSensors. Where I basically split the program in twain and implemented a model-view-controller paradigm on top of it to allow you to run it remotely through a network.


PS. I'll also add that Saldor010 is absolutely right. The more languages you know the better.

PPS. Why does this bloody forum keep adding /> at the end of each line in my code tags? WTH?
Edited on 06 January 2018 - 11:56 AM
Sewbacca #5
Posted 06 January 2018 - 05:20 PM
LUA is definitively not easier to write than C++.
– Snip –
Well, that is a thing of perspective. For me is Lua much more easier, because it was the first programming language learned. I am not a expert, but here is my point of view: There are manny languages for manny purposes, Lua is a scripting language, which is designed to be fast and to get fast a result, while C++ is designed for bigger projects. It offers OOP and much control on how behaves your program. You can use it for embeded software or powerful programs, but without a framework, you will get stuck. Java is the world famous programing language, easy to use and cross plattform compatible (Well C++ i its too, but it is much more complicated and it doesn't offers such a big vanilla framework). C# combines Java and C++ (Speed and a easy Syntax), but it is really bound on Windows, if you wanna use the .NET Framework. Every language offers a facility for solving a range of issues, so that there are languages which are better for some issues than other languages, but there are no languages, which are bad in use for a issue, because every language have always the same basic programming concept. Every programmer has some favorite programming languages, but he/she should be able to get along with any other, because you could get always confronted with a new programming language.

Cheers
Sewbacca

P.S I like Lua,because it is like a real language (If this file exists then do the following …. end)
Edited on 06 January 2018 - 04:23 PM
KingofGamesYami #6
Posted 06 January 2018 - 05:34 PM
PPS. Why does this bloody forum keep adding /> at the end of each line in my code tags? WTH?

Turn off the emoji thingy in the editor.

Lua is closer to natural language which would make it somewhat difficult to learn after c++. There are a lot of major syntaxtical differences. That said, Lua is also not terribly difficult to pick up, and a few examples of C++ and Lua side-by-side may not be a bad idea.
Edited on 06 January 2018 - 04:37 PM
SquidDev #7
Posted 06 January 2018 - 05:36 PM
Lua is definitively not easier to write than C++. […] What they are is roughly equally hard but radically different.
There are many ways you can describe C++, but "roughly as hard as Lua" is not one I'd use. C++ seems to revel in making things as hard as possible, and that's without talking about memory management.

C++ is a strongly typed language with very definitive features such as compulsory bracket use and end of command terminators. […] The consequence of this is that C++ looks and feels much more ordered. You always know what the type of something is. You always know what belongs where.
I'm a static typing fanatic, so I can't complain here. Projects such as TypeScript have successfully proven that you can retrofit a static type system to a dynamic language and make the whole thing easier to work with. That being said, Lua's dynamism (dynamicness?) does allow you to do some seriously funky stuff. If you remember that Lua is indented to be very light weight (and thus has no class system, comprehensive standard library, etc…) the choice begins to make sense.

If we're going to get pedantic, C++ and Lua are pretty strongly typed. Note that 123 + nil will error in Lua, but the equivalent in JS (which is a weakly typed language) will result in NaN. However, C++ is also statically typed (type checked at compile time). There's an argument that C++ is weakly typed, as you have union and type casts, but let's not go there :).

On the matter of syntax, I'm going to have to disagree. I also came from Lua from BCPL inspired languages (C# in my case) and found myself missing braces, but Lua has grown on me a lot. There's definitely elements I miss (such as shorter lambda expressions, operator assigns) but there isn't anything fundamentally wrong with it. But hey, I like Lisp so what do I know? :p

programmer coming from actual programming
Ooooh, that hurt. Whilst Lua is used a lot as an embedded scripting language, I don't think it's fair to exclude from the ranks of "actual programming". There's a list of projects using it here, as well as companies which depend on it enough to invest money in its continued development.

PPS. Why does this bloody forum keep adding /> at the end of each line in my code tags? WTH?
B) is an emoticon, so everything get's a little funky. If you go in to the full editor, there's an option to turn them off on the right.

LUA
Looks like you had a Lua Uppercase Accident :).

I realise I've just devolved into a language design rant. Sorry.
Edited on 06 January 2018 - 05:37 PM
Lupus590 #8
Posted 06 January 2018 - 05:49 PM
Write it in simple C++, then fix it where Lua Errors. Eventually you will learn the language (learning more languages is good).

If you get stuck then post in ask a pro. Shameless plug of my ask a pro tutorial.
Purple #9
Posted 06 January 2018 - 06:15 PM
There are many ways you can describe C++, but "roughly as hard as Lua" is not one I'd use. C++ seems to revel in making things as hard as possible, and that's without talking about memory management.
I newer understood why people would think that. Maybe it's just me but I find C++ including its memory management part to just be naturally intuitive.

Than again I am currently workng on a project in OPENGL using C++ for FUN so I refuse to vouch for my own sanity.

I'm a static typing fanatic, so I can't complain here. Projects such as TypeScript have successfully proven that you can retrofit a static type system to a dynamic language and make the whole thing easier to work with. That being said, Lua's dynamism (dynamicness?) does allow you to do some seriously funky stuff. If you remember that Lua is indented to be very light weight (and thus has no class system, comprehensive standard library, etc…) the choice begins to make sense.
As someone who has worked with Python I can tell you I am no opponent of either. Different tools for different things and all that. But I feel that for a beginner at least it's easier to start with static types so as to allow them to start thinking about and in type. And than later when they are comfortable they can open up the floodgates.

If we're going to get pedantic, C++ and Lua are pretty strongly typed. Note that 123 + nil will error in Lua, but the equivalent in JS (which is a weakly typed language) will result in NaN. However, C++ is also statically typed (type checked at runtime). There's an argument that C++ is weakly typed, as you have union and type casts, but let's not go there :)/>.
Yes, I guess we shouldn't. :(/>

On the matter of syntax, I'm going to have to disagree. I also came from Lua from BCPL inspired languages (C# in my case) and found myself missing braces, but Lua has grown on me a lot. There's definitely elements I miss (such as shorter lambda expressions, operator assigns) but there isn't anything fundamentally wrong with it. But hey, I like Lisp so what do I know? :P/>
The lack of {} literally hurts me in languages that don't have it. Literally hurts. It's not bad when writing code, but try reading someone elses code, especially badly commented and it just makes you die inside.

Although I will admit that it's not as horrible as pythons indentation rules. That's just evil.

I realise I've just devolved into a language design rant. Sorry.
We need a forum for that sort of thing or something.
SquidDev #10
Posted 06 January 2018 - 06:53 PM
I newer understood why people would think that. Maybe it's just me but I find C++ including its memory management part to just be naturally intuitive.
This code snippet (and the resulting compilation error) sums up much of my feelings:

#include <variant>
std::variant<int, double> a() {
  return std::get<int>(1);
}
With respect to memory management, it's not too bad, but it's "one more thing" to reason about. I'll confess that I'm one of those people who likes to encode as much information into the type system as possible, but C++'s types do feel rather ill-equipped to deal with some of the nuances of memory management and safety. Rust feels like a step in the right direction but is by no means the perfect solution.

But try reading someone elses code, especially badly commented and it just makes you die inside.
Eh, I guess it's a personal choice. Aside from VBA, there aren't many languages where I find the syntax utterly revolting. I've seen awful code written in a lot of languages, and beautiful code written in a few. Actually scratch that, the only beautiful code is Haskell.
Edited on 06 January 2018 - 05:53 PM
ardera #11
Posted 06 January 2018 - 07:16 PM
Let's not forget about metatables. I don't know if any other languages have a similiar concept since I only know Lua and Java (not too good at it though), but I think they're awesome. You practically have endless possibilities on what to do with them.

For example, I used this syntax for an OOP API 3 years ago:

(public - static - var) "filewriter";
-- this syntax to declare variables
(public - static - void) : main();
-- this syntax to declare methods

I also built my own linking API where you can do things like this:

local a = slink(50) -- 'a' now has the value 50

a.s = 55 -- 'a' now has the value 55
print(a) -- output: 55

local b = a + 20
print(b ) -- output: 75

a.s = 30
print(b ) -- output: 50
Sewbacca #12
Posted 07 January 2018 - 06:09 PM
Let's not forget about metatables. I don't know if any other languages have a similiar concept since I only know Lua and Java (not too good at it though), but I think they're awesome. You practically have endless possibilities on what to do with them.
Endless possibilities, well yeah, it's pretty nice, but I miss __get and __set methods (if the value exists), it would save the workarround to make a mask.

For example, I used this syntax for an OOP API 3 years ago:

(public - static - var) "filewriter";
-- this syntax to declare variables
(public - static - void) : main();
-- this syntax to declare methods

Well, this is not a syntax I like, but it's a matter of taste. Anyway, I don't like it to create functions at runtime (excluding the loading runtime of course), it just feels not good.

I also built my own linking API where you can do things like this:

local a = slink(50) -- 'a' now has the value 50

a.s = 55 -- 'a' now has the value 55
print(a) -- output: 55

local b = a + 20
print(b ) -- output: 75

a.s = 30
print(b ) -- output: 50

Why do you need number objects?
Edited on 07 January 2018 - 05:10 PM
Lupus590 #13
Posted 07 January 2018 - 06:24 PM
I also built my own linking API where you can do things like this:

local a = slink(50) -- 'a' now has the value 50

a.s = 55 -- 'a' now has the value 55
print(a) -- output: 55

local b = a + 20
print(b ) -- output: 75

a.s = 30
print(b ) -- output: 50

Why do you need number objects?

They are linked, so changing a updates b.
Purple #14
Posted 07 January 2018 - 06:57 PM
So kind of like a memory pointer than? Only without the fun.
Sewbacca #15
Posted 07 January 2018 - 08:22 PM
So kind of like a memory pointer than? Only without the fun.

– Snip
They are linked, so changing a updates b.

Ooop, where where my eyeballs at this moment? Okay, but I can't imagine a practical case, where it is usefull. Then, a __setvalue method would be usefull (standard setter for example override()). This would be pretty cool.
Edited on 07 January 2018 - 07:23 PM
Purple #16
Posted 07 January 2018 - 10:54 PM
Ooop, where where my eyeballs at this moment? Okay, but I can't imagine a practical case, where it is usefull.
Every time when you want two separate entities to have access to the same variable without having access or even knowledge of one another. The alternative is to have a 3rd entity both have access to and work with. Maybe that one will let you think of an example more easily.
Edited on 07 January 2018 - 09:55 PM
SquidDev #17
Posted 07 January 2018 - 11:07 PM
Ooop, where where my eyeballs at this moment? Okay, but I can't imagine a practical case, where it is usefull. Then, a __setvalue method would be usefull (standard setter for example override()). This would be pretty cool.
One common use case I've seen is in graphics libraries - an element's width may set to be 50% of the parent. This could be configured as a binding like ardera's code, so updating the parent's width would also adjust the child's automatically. The operator overloading that metatables isn't unique, but Lua's implementation has a fabulous mix of simplicity and power.
Edited on 07 January 2018 - 10:07 PM
Purple #18
Posted 08 January 2018 - 10:18 AM
I prefer C++ pointers to be honest. There is just something incredibly elegant about just initializing the object with a pointer to what ever you want to observe. Although I imagine that the "proper" solution would be to implement an observer pattern for the sake of robustness.
Sewbacca #19
Posted 09 January 2018 - 07:28 PM
I prefer C++ pointers to be honest. There is just something incredibly elegant about just initializing the object with a pointer to what ever you want to observe. Although I imagine that the "proper" solution would be to implement an observer pattern for the sake of robustness.
I prefer ref for observing.
LoganDark2 #20
Posted 15 January 2018 - 05:30 PM
[snip] a __setvalue method would be usefull (standard setter for example override()). This would be pretty cool.

Use metatables. They have two methods that may be useful: __index for getter, __newindex for setter


function tableLog()
  return setmetatable({}, {
	__index = function(self, key)
	  print(key .. ' was accessed!')
	  return rawget(self, key)
	end,
	__newindex = function(self, key, value)
	  print(key .. ' = ' .. value)
	  return rawset(self, key, value)
	end
  })
end
Edited on 16 January 2018 - 12:53 AM
Dave-ee Jones #21
Posted 17 January 2018 - 11:03 PM
Arguably, Lua is easier to write in than C++ is..
LUA is definitively not easier to write than C++…
There are many ways you can describe C++, but "roughly as hard as Lua" is not one I'd use. C++ seems to revel in making things as hard as possible, and that's without talking about memory management.
programmer coming from actual programming
Ooooh, that hurt. Whilst Lua is used a lot as an embedded scripting language, I don't think it's fair to exclude from the ranks of "actual programming". There's a list of projects using it here, as well as companies which depend on it enough to invest money in its continued development.

He did say arguably, guys. Calm down.
Purple #22
Posted 19 January 2018 - 08:12 PM
programmer coming from actual programming
Ooooh, that hurt. Whilst Lua is used a lot as an embedded scripting language, I don't think it's fair to exclude from the ranks of "actual programming". There's a list of projects using it here, as well as companies which depend on it enough to invest money in its continued development.
I can't believe I missed to reply to this one.

When I said "actual programing" I was referring to actual programming. As opposed to just playing around with programing inside a video game mod which is programing but not actual proper programing like what you'd do as a job. It was not a jab against Lua the language but me trying to give advice to someone who is used to working with this in a more professional environment how to adapt to using those skills in Computercraft.

That is why I suggested using the sort of tools that would be imposing to someone who just wants to have some quick fun in the game but would be completely normal to someone working in the field because frankly not only are we used to reading dictionary sized reference manuals we do it for fun.
Edited on 19 January 2018 - 07:17 PM
valithor #23
Posted 20 January 2018 - 05:48 AM
programmer coming from actual programming
Ooooh, that hurt. Whilst Lua is used a lot as an embedded scripting language, I don't think it's fair to exclude from the ranks of "actual programming". There's a list of projects using it here, as well as companies which depend on it enough to invest money in its continued development.
I can't believe I missed to reply to this one.

When I said "actual programing" I was referring to actual programming. As opposed to just playing around with programing inside a video game mod which is programing but not actual proper programing like what you'd do as a job. It was not a jab against Lua the language but me trying to give advice to someone who is used to working with this in a more professional environment how to adapt to using those skills in Computercraft.

That is why I suggested using the sort of tools that would be imposing to someone who just wants to have some quick fun in the game but would be completely normal to someone working in the field because frankly not only are we used to reading dictionary sized reference manuals we do it for fun.

Lua is undeniably simpler than c++. If you are coming from a language that requires strict adherence to a format it is understandable to see the simplicity as messy and unorganized. The more you get into the language, just like any language, the more you will find ways to organize your code so it no longer looks messy. Knowing both c++ and Lua I can easily say that you can write Lua code that is just as neat as the neatest c++ code, and you can write code that is just as messy as the convoluted code that I find sometimes. It boils down to planning ahead, and following good conventions.

For example you mentioned the curly braces in c++, in Lua anywhere you would have a beginning curly brace you have a keyword such as do or then and anywhere there is a closing curly brace there is things such as end or while. And the exact same logical structure is represented with indentation.
Even though you can use variables for any type most of the time you should stick to a single type, and many people prefix their variables with a letter denoting what the type of the variable is. Such as tWords for a table containing words.

Once you get the hang of Lua you always know what belongs where and there is no question about it. As long as good programming techniques are followed Lua, is no less professional than any other language out there.

P.S. You are also commenting on a site where mostly everyone knows Lua and for many it was there first language (me included). Lua is a amazing way to get into programming, it gave me a massive head start over everyone else in my class. If you do things right, almost all of the concepts that you learn in Lua carries over to just about any other language you find.
Edited on 20 January 2018 - 04:52 AM
Purple #24
Posted 20 January 2018 - 11:00 AM
P.S. You are also commenting on a site where mostly everyone knows Lua and for many it was there first language (me included). Lua is a amazing way to get into programming, it gave me a massive head start over everyone else in my class. If you do things right, almost all of the concepts that you learn in Lua carries over to just about any other language you find.
And you are talking to a person whose favorite language is x86 Assembly. Something tells me we'll have to agree to disagree.
Lupus590 #25
Posted 20 January 2018 - 11:36 AM
Hello!
I have been thinking about an exciting mining turtle programm, however I can't writte it in lua language. If it was in c++ language it would be easy. Can I get some help from someone who knows lua? I can write it in c++ for you to understand it better.
thank you.

Write it, and we'll see what needs to be done.

@everyone else, we have gotten very off-topic.
Edited on 20 January 2018 - 10:37 AM
Lignum #26
Posted 20 January 2018 - 01:09 PM
And you are talking to a person whose favorite language is x86 Assembly. Something tells me we'll have to agree to disagree.

What does that have to do with anything? Assembly languages are more technical details than programming languages, but the same principles valithor mentioned still apply, you're going to find your way around programming a processor similar to how you're going to find your way around Lua. This applies to basically any formal language that models computations. And since you keep referring to "actual programming", your use of assembly language discredits your point more than you intend to back it up with.
Saldor010 #27
Posted 20 January 2018 - 03:58 PM
@everyone else, we have gotten very off-topic.

Already checked on it: the guy was only online for one day, not even long enough to see Lyqyd's reply. I think it's safe to say that we can repossess his topic for a rather interesting discussion.

P.S. You are also commenting on a site where mostly everyone knows Lua and for many it was there first language (me included). Lua is a amazing way to get into programming, it gave me a massive head start over everyone else in my class. If you do things right, almost all of the concepts that you learn in Lua carries over to just about any other language you find.
And you are talking to a person whose favorite language is x86 Assembly. Something tells me we'll have to agree to disagree.

.. and? Maybe I'm just not seeing it, but what does that contribute to the conversation, other than wanting to let everyone know that you program in assembly?
Edited on 20 January 2018 - 02:58 PM
Purple #28
Posted 20 January 2018 - 08:48 PM
.. and? Maybe I'm just not seeing it, but what does that contribute to the conversation, other than wanting to let everyone know that you program in assembly?
*sigh*

It was my way of saying that our contexts for what we prefer in our languages are so far apart that we shall newer reach a consensus on the topic of preferred languages. Ultimately they are all the same in the end, different tools for different jobs or even the same job in different ways. And we just have different preferences when it comes to our taste of those.

What does that have to do with anything? Assembly languages are more technical details than programming languages, but the same principles valithor mentioned still apply, you're going to find your way around programming a processor similar to how you're going to find your way around Lua. This applies to basically any formal language that models computations. And since you keep referring to "actual programming", your use of assembly language discredits your point more than you intend to back it up with.
Obviously I don't work with x86 assembly. I am not sure anyone is. Although I would not be surprised. Crazier things have happened in the industry. But I do like the language and it's ultimate simplicity and precision. And sure, it might take you a week worth of man hours to print up "hello world" on the screen but that's ok. And no, I don't have an explanation as to why that is ok. I just like it.
Edited on 20 January 2018 - 07:54 PM
Saldor010 #29
Posted 20 January 2018 - 08:52 PM
It was my way of saying that our contexts for what we prefer in our languages are so far apart that we shall newer reach a consensus on the topic of preferred languages. Ultimately they are all the same in the end, different tools for different jobs or even the same job in different ways. And we just have different preferences when it comes to our taste of those.

Thank you for elaborating, sorry if I came off rude, that was not my intent.
Purple #30
Posted 20 January 2018 - 08:55 PM
Thank you for elaborating, sorry if I came off rude, that was not my intent.
Nor mine. Just for the record. And this needs saying because I honestly don't know how I come off half the time because english is not my first language and I've been told that I come off as confrontational when I don't intend to.
Edited on 20 January 2018 - 07:56 PM