Global variables in NQC
#1
Posted 19 August 2012 - 01:04 AM
Anyway, after dusting off the RCX I'm giving NQC a go and I seem to have a problem with a global variable. I can init a global variable and I can set its value in task main. However when I start a second task from main and I try to change the value of my global var in the second task then nothing happens...
So what I'm doing is this:
int x;
task main () {
x = 10;
start task_2;
}
task task_2() {
x = 20;
}
I would expect x to be 20 after running this but it's 10.
Any idea what I'm doing wrong?
#2
Posted 19 August 2012 - 02:01 AM

Rumble is Red,Frenzy is Blue,
On Decepticon Cypertron,
Poem writes you!
My current project: ETF HTX-380, on Brickshelf, and Eurobricks.
#3
Posted 19 August 2012 - 10:47 AM
Duq, on 19 August 2012 - 01:04 AM, said:
Anyway, after dusting off the RCX I'm giving NQC a go and I seem to have a problem with a global variable. I can init a global variable and I can set its value in task main. However when I start a second task from main and I try to change the value of my global var in the second task then nothing happens...
So what I'm doing is this:
int x;
task main () {
x = 10;
start task_2;
}
task task_2() {
x = 20;
}
I would expect x to be 20 after running this but it's 10.
Any idea what I'm doing wrong?
int x is declared globally so in task task_2() you are overwriting x from x = 10 to x = 20. We would expect to get 20 in this case. What's the programming language you are using? Can't you return a value from that function like int task_2() { return 20; }?
Edited by Ultimatemau, 19 August 2012 - 10:49 AM.
#4
Posted 20 August 2012 - 01:14 PM
Ultimatemau, on 19 August 2012 - 10:47 AM, said:
I'm writing this in NQC in Bricx Command Center 3.3.
I've changed the program to do things a different way but I'd still like to know if this is a known issue or whether I'm doing something wrong...
#5
Posted 20 August 2012 - 02:14 PM
Duq, on 20 August 2012 - 01:14 PM, said:
I'm writing this in NQC in Bricx Command Center 3.3.
I've changed the program to do things a different way but I'd still like to know if this is a known issue or whether I'm doing something wrong...
I have no experience with this program whatsoever, but could it be that the language is very sensitive,
and that using the word "task" (task_2) to declare the second function gives the problem.
#6
Posted 20 August 2012 - 02:57 PM
Cheeky question, but is the code you've posted exactly the same as what you're trying to achieve? If you've simplified the example for posting then could there be a typo in your original?
The NQC Progrmaming Guide also mentions that there's a limit of 32 global variables, but doesn't say what the behaviour if exceeded is. Could you be hitting anything like that?
#7
Posted 20 August 2012 - 04:16 PM
chorlton, on 20 August 2012 - 02:57 PM, said:
Cheeky question, but is the code you've posted exactly the same as what you're trying to achieve? If you've simplified the example for posting then could there be a typo in your original?
The NQC Progrmaming Guide also mentions that there's a limit of 32 global variables, but doesn't say what the behaviour if exceeded is. Could you be hitting anything like that?
Nothing wrong with that cheeky question. I have indeed simplified the code but I've checked the original code for typos (long live Notepad++). It's a small program that only uses a handful of variables so I'm nowhere near the limit of 32.
#8
Posted 22 August 2012 - 08:50 PM
Duq, on 20 August 2012 - 04:16 PM, said:
Nothing wrong with that cheeky question. I have indeed simplified the code but I've checked the original code for typos (long live Notepad++). It's a small program that only uses a handful of variables so I'm nowhere near the limit of 32.
Sorry for bumping this up:
Duq,
has this been resolved? I pulled out one RCX1.0, one RCX2.0, and a SCOUT PBrick (The RCX' are running the same firmware, firm0328.lgo. That is to my knowledge the last one TLC has released - the SCOUT just came with one firmware).
All PBricks do show the result "20" after running the program. Once the start task(2) is commented, "10" is indeed the result. I used the "Watching the brick" feature.
I have initialized the variable x ("int x=0;") - does that make any difference? Could timing be an issue? Sometimes multi tasking behaves a little weird on these bricks. A delay of 1 ms before starting task 2 may be worth a try.
Anyways, on my 3 PBricks your code runs fine.
Regards,
Thorsten
#10
Posted 23 August 2012 - 01:04 AM
http://bricxcc.sourc...c/NQC_Guide.pdf
Tasks aren't very well explained. But tasks are some form of multi-tasking. Perhaps you have some race condition here. How did you check that x was 10 or 20?
int x;
task main () {
x = 10;
start task_2;
// x Check here?
}
task task_2() {
x = 20;
}
If you checked where I put the comment (which seams logical as it is the main task. So if the check was placed there that code could very well be executed before task_2 had begun. If you want some linear flow you should use functions instead of tasks. If a function you call doesn't start any tasks you should have the contract that what happens in the function happens before the return of the execution to the caller (and what it continues to do).
So you can probably write it as
int x;
task main () {
x = 10;
myFunction();
// x will be 20 here
}
void myFunction() {
x = 20;
}
But as I said, I never (I think, perhaps I have in real time system course) programmed in NQC. Only using my programming skills to reason about the problem at hand.
Reply to this topic
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users










