|
Page 1 of 1
|
[ 7 posts ] |
|
Variables named by program
Author |
Message |
Ghosto
Rookie
Joined: Fri Mar 30, 2012 6:01 pm Posts: 3
|
 Variables named by program
I'm doing a project in which I'm trying to take vex components to a new level. I've discovered happily that I can name variables after the task main, as long as they aren't called prior to being named (ideally it would be better if it assumed an unnamed variable as 0 when it was called upon for what i'm trying to do, my compiler for another program does that). But now i'm having difficulty having the program name the variable.
In my other programs, i was able to make variables names such as int "coorx+varget("fc")" in which it would create a variable named, for example, "coorx6" assuming the "fc" var was on 6. In this sense, the variables would be created as needed upon further usage of the program. I can't preset these variables, they have to be made during the machine's trial. The issue is, i can't seem to find a way to pull off something like explained above.
It always gives me errors when i try to conjuncture variables inside variables. So i'm asking any experts, how can i do this? in the code i want something like this if(this that){ int "coorx(fc)" = ([pot] + [time] - [coory(fc)]) fc = fc +1 }
that's just a silly example where the vars don't relate to my actual code, but this code specifically could be something i can do with what i'm asking for.
So, any ideas?
Thanks.
|
Fri Mar 30, 2012 6:13 pm |
|
 |
sumasmreq
Rookie
Joined: Fri Jan 27, 2012 6:57 pm Posts: 40
|
 Re: Variables named by program
I don't think you can declare variable names derived from the results of other variables or other expressions in ROBOTC. (Or even C for all I know, though I could be wrong.) Also, you can't technically declare any new variables at Run-Time anyway in ROBOTC, as it is all pre-calculated before hand which variable is going to be stored where in the Cortex's memory. What you might want to do is consider using an Array, or using Structs. First off, an Array is a bunch of the same type of variable organized together into one large variable, with the individual parts accessed using the large variable name and a reference number: (I.E MyVar[4] = 20. This would set the fifth element (because the elements start at 0) of the MyVar array to 20.) You can read about them here: (Most of the info on that page applies to ROBOTC*) http://www.cplusplus.com/doc/tutorial/arrays/Secondly, a Struct is bunch of many types of variables (potentially), organized together into one bigger variable, with the individual parts accessed using the bigger variable name and the small parts variable name. This is a little more complicated, but looks like this (after you've made your type and declared your variable): MyVar.NumOfClaws = 16; MyVar.NameOfCat = "Sally" You can read about them here: (Most of the info on that page applies to ROBOTC*) http://cprogramminglanguage.net/c-structure.aspx*The info that wouldn't apply is regarding pointers, as ROBOTC doesn't support those.
|
Sat Mar 31, 2012 9:58 am |
|
 |
Ghosto
Rookie
Joined: Fri Mar 30, 2012 6:01 pm Posts: 3
|
 Re: Variables named by program
Thanks, that's pretty much what i was looking for. Both of them are very helpful.
|
Sat Mar 31, 2012 12:00 pm |
|
 |
Ghosto
Rookie
Joined: Fri Mar 30, 2012 6:01 pm Posts: 3
|
 Re: Variables named by program
I've found what makes me able to do the naming of variables like how i want, maybe someone could assess this and help further with more specifically my issue.
"Well, in aom we are setting QuestVar names dynamically not normal var names and I think Quest Vars are stored in some sort of array/table.
In lua, global variables are accessible via the _G table, which gives you a way to name variables on the fly and you can embed lua in C, so that gives you a round-about way of having dynamically named variables.
For example,
texttouse="MyVar" for i=1,5 do _G[texttouse..i]=i end
will give you the variables MyVar1, MyVar2, MyVar3, MyVar4 and MyVar5"
was the response given to me in another community.
|
Wed Apr 18, 2012 5:24 pm |
|
 |
RoboDesigners
Novice
Joined: Sat Jul 10, 2010 3:06 pm Posts: 86 Location: Roanoke, VA
|
 Re: Variables named by program
 |  |  |  | Ghosto wrote: I've found what makes me able to do the naming of variables like how i want, maybe someone could assess this and help further with more specifically my issue.
"Well, in aom we are setting QuestVar names dynamically not normal var names and I think Quest Vars are stored in some sort of array/table.
In lua, global variables are accessible via the _G table, which gives you a way to name variables on the fly and you can embed lua in C, so that gives you a round-about way of having dynamically named variables.
For example,
texttouse="MyVar" for i=1,5 do _G[texttouse..i]=i end
will give you the variables MyVar1, MyVar2, MyVar3, MyVar4 and MyVar5" |  |  |  |  |
I don't think ROBOTC has that feature. ROBOTC is modeled after the C language, but it doesn't have all of the features of the C language... As sumasmreq said, arrays and/or structs are probably your best options. //Andrew
_________________Check out my website! www.RoboDesigners.comVRC Team 2190 Twitter: @RoboDesigners
|
Sun Apr 29, 2012 8:59 am |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: Variables named by program
While ROBOTC does not support this directly, it's not impossible to implement it yourself. I don't know if this is what sumasmreq was getting at, but all you need is an array of structs. Now, if you want, you could write a hashmap function for ROBOTC (I'm going to attempt to write one now, once finals are over), but it won't be very simple. If you don't want to spend the time writing one, and you don't care too much about computation speed, you could also just store the variables in an array and iterate over each element. Now I haven't tested this code, but you get the idea.
_________________ sudo rm -rf /
|
Sun Apr 29, 2012 2:24 pm |
|
 |
magicode
Moderator
Joined: Tue Sep 14, 2010 9:19 pm Posts: 496
|
 Re: Variables named by program
I made a hashmap that you can use to implement this behavior: viewtopic.php?f=15&t=4483
_________________ sudo rm -rf /
|
Mon Apr 30, 2012 3:38 pm |
|
|
|
Page 1 of 1
|
[ 7 posts ] |
|
Who is online |
Users browsing this forum: No registered users and 2 guests |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|