My Block equivalent in NXC
Posted: 28 Apr 2011, 01:59
I am just starting to learn NXC, and I was wondering if there was an equivalent for NXT-G My Blocks in NXC
Give a child a robot, he'll play for an hour. Teach him to build, and he'll play forever.
https://mindboards.org:443/
Code: Select all
void MyVoid()
{
TextOut(0, 0, "Hello MyVoid!");
Wait(2000);
}
task main()
{
MyVoid();
}
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.
Code: Select all
#include "myblocks.nxc"
task main()
{
HelloWorld();
}
Code: Select all
void HelloWorld()
{
TextOut(0, 0, "Hello, World!", 0);
Wait(2000);
}
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.
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).
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;
}
Code: Select all
int SetLimits(int value, int min, int max){
if (value>max)value=max;
if (value<min)value=min;
return value;
}