My Block equivalent in NXC
My Block equivalent in NXC
I am just starting to learn NXC, and I was wondering if there was an equivalent for NXT-G My Blocks in NXC
Re: My Block equivalent in NXC
'My Blocks' in NXC probably have more functionality than NXT-G My Blocks.
They're called subroutines/procedures and functions.
Subroutines:
Guess what that code does?
-----
I recommend learning C++ (or at least, the C parts of it) from this tutorial. It's explained very well for a beginner.
They're called subroutines/procedures and functions.
Subroutines:
Code: Select all
void MyVoid()
{
TextOut(0, 0, "Hello MyVoid!");
Wait(2000);
}
task main()
{
MyVoid();
}
-----
I recommend learning C++ (or at least, the C parts of it) from this tutorial. It's explained very well for a beginner.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: My Block equivalent in NXC
Yes, but is there any way to save a section of code that you can call on in any of your programs?muntoo wrote:'My Blocks' in NXC probably have more functionality than NXT-G My Blocks.
They're called subroutines/procedures and functions.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: My Block equivalent in NXC
Yes there is. Make a library.
What I do, is have a folder that contains all my libraries, and include that path with the compiler settings. If I want to use one of libraries, all I have to do is #include it, and all the functions are available.
What I do, is have a folder that contains all my libraries, and include that path with the compiler settings. If I want to use one of libraries, all I have to do is #include it, and all the functions are available.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: My Block equivalent in NXC
Ah, I get what you're talking about now. Use #includes.
A demonstration:
main.nxc
myblocks.nxc
Just put #include "myblocks.nxc" in all of your programs from now on. (And don't forget to put "myblocks.nxc" in the same folder as each of your programs.)
-----
A better way would be to create a library, as Matt (who ninja'd me) suggested. That way, your "myblocks.nxc" code won't have to be compiled unnecessarily for one function/"block" you want to use out of a few unrelated hundred.
A demonstration:
main.nxc
Code: Select all
#include "myblocks.nxc"
task main()
{
HelloWorld();
}
Code: Select all
void HelloWorld()
{
TextOut(0, 0, "Hello, World!", 0);
Wait(2000);
}
-----
A better way would be to create a library, as Matt (who ninja'd me) suggested. That way, your "myblocks.nxc" code won't have to be compiled unnecessarily for one function/"block" you want to use out of a few unrelated hundred.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: My Block equivalent in NXC
Muntoo, I think you missed my point. You do not have to save the libraries (or "myblocks.nxc") in the same directory... you can make the compiler look in an additional directory (where all the libraries or whatever else is located). It keeps me from have a zillion duplicates all over the place. It also means I only have to modify it in one place, and everything get modified.muntoo wrote:...Just put #include "myblocks.nxc" in all of your programs from now on. (And don't forget to put "myblocks.nxc" in the same folder as each of your programs.)
-----
A better way would be to create a library, as Matt (who ninja'd me) suggested. That way, your "myblocks.nxc" code won't have to be compiled unnecessarily for one function/"block" you want to use out of a few unrelated hundred.
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: My Block equivalent in NXC
How do you do that?mattallen37 wrote: ... you can make the compiler look in an additional directory (where all the libraries or whatever else is located).
Also is there any way to pass parameters into and out of voids? (Like data wires in NXT-G?)
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: My Block equivalent in NXC
Like this:See how I did that? In that simple (example) function, I passed three ints into it (int value, int min, int max), and it send one back (int &end_value). The "&" means that it is an "output wire".
However, for that specific function, I would probably normally use something more like this:Instead of calling the function like this "SetLimits(value, min, max, result);", you would call it like this "NumOut(0, LCD_LINE1, SetLimits(value, min, max));". Because it is a "int" function, it has a numerical value (chosen by the "return" command).
Also, you are not limited to passing ints in and out of functions. You can use bool, char, byte, int, Uint, long, Ulong, array, string...
Yes you can pass values in and out of a void function. Here is an example:Code: Select all
void SetLimits(int value, int min, int max, int &result){
if (value>max)value=max;
if (value<min)value=min;
result=value;
}
However, for that specific function, I would probably normally use something more like this:
Code: Select all
int SetLimits(int value, int min, int max){
if (value>max)value=max;
if (value<min)value=min;
return value;
}
Also, you are not limited to passing ints in and out of functions. You can use bool, char, byte, int, Uint, long, Ulong, array, string...
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: My Block equivalent in NXC
Thanks!
Who is online
Users browsing this forum: Semrush [Bot] and 0 guests