Can somebody explain how to set yp an array?
by Isaac Barbosa · in Torque Game Builder · 02/08/2007 (5:15 pm) · 38 replies
I never understood that in flash, and I guess is the same thing in TGB since arrays are always arrays.
I want to ask for help with this:
If I want to make an array to store x number of words... what should I do to accomplish that?
And if I want to check if x word is in that array... what should I do to accomplish that?
Thanks
I want to ask for help with this:
If I want to make an array to store x number of words... what should I do to accomplish that?
And if I want to check if x word is in that array... what should I do to accomplish that?
Thanks
#2
When I create an array such as
$myArray[0] = "foobar";
The variable is treated the same as $myArray0.
In other words it's just slightly different formatting of the variable names, but there's no native array support, no sorting, no counting, no add/remove rows etc.
That being said, the "workaround" for this from how I understand it is by using a SimSet as a pseudo-array and SimObjects for your array records. You then get the same sort of functionality. It's not a great solution I agree, but it is a solution nonetheless.
02/08/2007 (5:27 pm)
From how I understand it, there isn't any true array support in torquescript.When I create an array such as
$myArray[0] = "foobar";
The variable is treated the same as $myArray0.
In other words it's just slightly different formatting of the variable names, but there's no native array support, no sorting, no counting, no add/remove rows etc.
That being said, the "workaround" for this from how I understand it is by using a SimSet as a pseudo-array and SimObjects for your array records. You then get the same sort of functionality. It's not a great solution I agree, but it is a solution nonetheless.
#3
from docs
02/08/2007 (5:46 pm)
But:from docs
Quote:TorqueScript is extremely flexible with arrays. Many scripting languages are more flexible with arrays than compiled languages like C++, but TorqueScript is even more flexible than most scripting languages. All this flexibility can get confusing, and deciphering how TorqueScript handles arrays sometimes trips people up. Part of this confusion comes from the various ways in which array variables can be accessed. For instance, $MyArray[1] and $MyArray1 are identical variable names. Also, $MyMultiArray[0,1], $MyMultiArray[0_1] and $MyMultiArray0_1 all reference the same variable.
#4
If you want to reference vars with array syntax you can do so. The only thing you can't do with arrays in TorqueScript is pass them to a function easily with a single var.
In action script you had
which translates in torque script to
02/08/2007 (7:46 pm)
@IsaacIf you want to reference vars with array syntax you can do so. The only thing you can't do with arrays in TorqueScript is pass them to a function easily with a single var.
In action script you had
var myArray:Array = new Array("object1", "object2", "object3" and so on)which translates in torque script to
%myArray[0] = "object1"; %myArray[1] = "object2"; %myArray[2] = "object3"; // and so on
#5
So if I want to store 1000 words, I have to create 1000 arrays?
has somebody worked with search words in TGB? is that possible in a letter by letter basis?
Thanks :)
02/09/2007 (7:36 am)
@Ben:So if I want to store 1000 words, I have to create 1000 arrays?
%myArray[0] = "word1"; ... %myArray[999] = "word1000";
has somebody worked with search words in TGB? is that possible in a letter by letter basis?
Thanks :)
#6
As such, TorqueScript does not have any 'sizeof()' or 'count()' style functions, and arrays are not treated as 'objects' either (though, most languages with array support don't treat them as 'objects' -- ie; add(), remove() functions)
You can, however, create a SimSet if you want the 'add' and 'remove' functionality -- a SimSet is similiar to a Vector in C++ from what I can tell so far ...
As far as 'search words' go, be more specific -- what are you trying to accomplish by storing 1000 "words" somewhere?
You could, if you need dynamic support, store the words in a ScriptObject and store the ScriptObject in a SimSet --
02/09/2007 (8:15 am)
@Isaac -- your not creating '1000' arrays, your creating 'one' array, with 1000 elements -- which is true for any langauge with array support -- the thing that makes TorqueScript odd when it comes to arrays is that it treats them as if they are 1000 unique variables anyhow ... (someone just recently explained this to me)As such, TorqueScript does not have any 'sizeof()' or 'count()' style functions, and arrays are not treated as 'objects' either (though, most languages with array support don't treat them as 'objects' -- ie; add(), remove() functions)
You can, however, create a SimSet if you want the 'add' and 'remove' functionality -- a SimSet is similiar to a Vector in C++ from what I can tell so far ...
As far as 'search words' go, be more specific -- what are you trying to accomplish by storing 1000 "words" somewhere?
You could, if you need dynamic support, store the words in a ScriptObject and store the ScriptObject in a SimSet --
#7
And other important thing: If I'm gonna have independent letter, should I link them using @ or something to build the word?
step 1: player picks an "A", so his first letter is "A" and the word search starts, but since "A" will not be considered a word and the minimum letters for a valid will be three, player need to pick another letter.
step 2: player has an "A" and then choose "P" as second letter, so he have now "AP" but that's not a word so he search for another letter.
step 3: player pick "E", so he have a valid word now: "APE" and the option to submit word is active, if he submit the word, a new word will be started from scratch.
step 4: but if player does'nt submit the word he will be able to search for a larger word, such like "APETITE", "APES" or "APERITIVE".
Is this possible with TGB? how? any directions/ideas?
Thanks!
02/09/2007 (8:54 am)
@David --Hi, well, I'm trying to make a word search game, so I need to store x number of words in my array. So player will build words using single letters, so if a word is created player will submit that word to score points. The specific thing I need to know is: after create my word list stored in the array, how can I perform a search in the words contained to know if one of them matchs with the word created by the player. I hope this make sense.And other important thing: If I'm gonna have independent letter, should I link them using @ or something to build the word?
step 1: player picks an "A", so his first letter is "A" and the word search starts, but since "A" will not be considered a word and the minimum letters for a valid will be three, player need to pick another letter.
step 2: player has an "A" and then choose "P" as second letter, so he have now "AP" but that's not a word so he search for another letter.
step 3: player pick "E", so he have a valid word now: "APE" and the option to submit word is active, if he submit the word, a new word will be started from scratch.
step 4: but if player does'nt submit the word he will be able to search for a larger word, such like "APETITE", "APES" or "APERITIVE".
Is this possible with TGB? how? any directions/ideas?
Thanks!
#8
Given what you want to accomplish, is TGB really the right platform for your game? Obviously, I have no idea what other elements your game contains, but if you're not going to be taking advantage of TGB's strengths (particle effects, physics and collision system, etc.) maybe building it in Flash is a better approach. Especially with Flash 9 and Actionscript 3 on the horizon. Personally, if I'm not going to need the physics and particles, I choose Flash over TGB for my games due to Actionscript's superiority to Torquescript, IMHO, of course. ;)
Just FYI: Another word-oriented game that was built in Flash is Wheel of Fortune.
02/09/2007 (12:00 pm)
@Isaac - I've complained about Torquescript's array situation in the past and unfortunately it is best described as clunky and severely lacking. I'm also a Flash developer and I would absolutely love to see an Actionscript quality Array in TS but I don't think that's ever going to happen as I've heard they don't plan on ever updating it.Given what you want to accomplish, is TGB really the right platform for your game? Obviously, I have no idea what other elements your game contains, but if you're not going to be taking advantage of TGB's strengths (particle effects, physics and collision system, etc.) maybe building it in Flash is a better approach. Especially with Flash 9 and Actionscript 3 on the horizon. Personally, if I'm not going to need the physics and particles, I choose Flash over TGB for my games due to Actionscript's superiority to Torquescript, IMHO, of course. ;)
Just FYI: Another word-oriented game that was built in Flash is Wheel of Fortune.
#9
@Isaac - There are lots of ways to do searches. The simplest way would be a straight linear search, just iterate through the array in a for loop and compare the contents of your array at each index to the word you're looking for. Now that's probably going to have performance problems, because you have to do so many comparisons all the time. But there are other techniques you can use, such as a binary search if you keep your word array sorted, or hash tables, or lots of other options. Basically, though, you're getting into the general programming question of "what search algorithm should I use" rather than a torquescript syntax question. I would suggest trying the "dumb" linear search approach and seeing what kind of performance you get. If it's too slow, you could try some smarter algorithms.
02/09/2007 (12:33 pm)
@Dennis - at a conceptual level, TGB's "array support" is no worse than C's, and in some cases better. To think that you fundamentally can't use an array-like data structure because you don't have lots of built-in functionality seems like an odd mindset to me. What Isaac's trying to do might not work in torquescript for performance reasons, but the lack of object-oriented style arrays is not what's holding him back.@Isaac - There are lots of ways to do searches. The simplest way would be a straight linear search, just iterate through the array in a for loop and compare the contents of your array at each index to the word you're looking for. Now that's probably going to have performance problems, because you have to do so many comparisons all the time. But there are other techniques you can use, such as a binary search if you keep your word array sorted, or hash tables, or lots of other options. Basically, though, you're getting into the general programming question of "what search algorithm should I use" rather than a torquescript syntax question. I would suggest trying the "dumb" linear search approach and seeing what kind of performance you get. If it's too slow, you could try some smarter algorithms.
#10
02/09/2007 (1:13 pm)
@Isaac - You can also take advantage of the somewhat funky way TorqueScript does arrays to do this:%word["hello"] = true;
%word["goodbye"] = true;
%myWord = "hello";
if (%word[%myWord])
{
echo("Hooray!");
}
%myWord = "ashihauiuergi";
if (%word[%myWord])
{
echo("It thinks it's a word...");
}TorqueScript is very flexible about what it takes as an index into an array.
#11
Believe me, I've spent far more time jumping through Torquescript's hoops to get decent array functionality than I would like. But it's been worth it because the other features of the TGB engine have saved me more time than I would've spent recreating them in Flash.
Also, since he mentioned Actionscript in his first post, I assumed that he was most likely more experienced with Flash and new to TGB and therefore should consider it as a viable option instead of TGB.
Regarding your second post: That's a really cool technique! If the performance is decent, that could come in really handy. Thanks for that.
02/09/2007 (1:27 pm)
@Dan: I'm not saying that you "can't" use it because it's not feature rich. I'm just saying that if you have another tool that IS feature rich in a specific area and you have a lot of experience with it... well you might as well use it instead. I mean, you can pound in a nail with a big screwdriver but a hammer works a lot better. ;) Believe me, I've spent far more time jumping through Torquescript's hoops to get decent array functionality than I would like. But it's been worth it because the other features of the TGB engine have saved me more time than I would've spent recreating them in Flash.
Also, since he mentioned Actionscript in his first post, I assumed that he was most likely more experienced with Flash and new to TGB and therefore should consider it as a viable option instead of TGB.
Regarding your second post: That's a really cool technique! If the performance is decent, that could come in really handy. Thanks for that.
#12
@Dan:
That sounds pretty interesting, how may I be able to achieve this straight linear search?
Maybe TGB is not the platform to do the search, but I want to try:
This is my approach now:
I have every letter in an array:
And I have lots of words:
The intended gameplay mechanics are as follow:
I have every letter inside a sprite on stage...
So I state at game beginning that
If I click over sprite "A", then $actualWord[0] = $letter[0];, but that's not a word, so I click over sprite "N" to make a letters string (Not done yet but is my logic now)
but that's not a word yet, so I click over "D" to get
so now AND is a word and I would:
I don't know if this makes any sense, but I have no a clue on how to perform a search in TGB, neither if link single letters to build words is possible...
Any directions are greatly appreciated!
Thanks
PS Eventually if a word starts with "A" Tgb will search only words starting with "A" and so on for every letter if a word starts with that letter?
02/09/2007 (2:30 pm)
@Dennis: I love Flash 8 but I hate the fact that is not a native game platform like TGB. I'm trying to make a game in TGB that I suspend in Flash since the lack of speed and because it was unable to manage lots of sprites (movie clips). I believe the best thing could happen even is a marriage between flash and TGB... hehehe@Dan:
Quote:The simplest way would be a straight linear search, just iterate through the array in a for loop and compare the contents of your array at each index to the word you're looking for.
That sounds pretty interesting, how may I be able to achieve this straight linear search?
Maybe TGB is not the platform to do the search, but I want to try:
This is my approach now:
I have every letter in an array:
$letter[0] = "A"; $letter[1] = "B"; $letter[2] = "C"; ... $letter[25] = "Z";
And I have lots of words:
$words[0] = "ape"; $words[1] = "apes"; $words[2] = "apetite"; $words[3] = "apetites"; $words[4] = "astro"; and so on...
The intended gameplay mechanics are as follow:
I have every letter inside a sprite on stage...
So I state at game beginning that
$currentWordSearch = $actualWord[0] = " ";
If I click over sprite "A", then $actualWord[0] = $letter[0];, but that's not a word, so I click over sprite "N" to make a letters string (Not done yet but is my logic now)
$actualWord[0] = $letter[0] @ $letter[13]; // "AN"?
but that's not a word yet, so I click over "D" to get
$actualWord[0] = $letter[0] @ $letter[13] @ $letter[3]; // "AND"?
so now AND is a word and I would:
if($actualWord $= $word[i++])
{
echo("you have a word");
}I don't know if this makes any sense, but I have no a clue on how to perform a search in TGB, neither if link single letters to build words is possible...
Any directions are greatly appreciated!
Thanks
PS Eventually if a word starts with "A" Tgb will search only words starting with "A" and so on for every letter if a word starts with that letter?
#13
ibarbosar@hotmail.com [it's a public e-mail] I don't know if it can be post here, if not let me know ;)
02/09/2007 (2:37 pm)
@Dennis: I know this is not an actionscript forum... so would you be able to help me with action script code to stablish an array in Flash and then perform a search with the linking letter by letter basis? Some directions would be great :)ibarbosar@hotmail.com [it's a public e-mail] I don't know if it can be post here, if not let me know ;)
#14
Now, the big problem you're likely to have with this method is that every time you make the check you potentially have to do the comparison for every single word you have. If you have a lot of words that can be a lot of comparisons, so the performance of your game might not be acceptable, especially if you're doing this check frequently. Also, the other downside is that the game will basically get slower and slower as you keep adding new words to your list.
Switching to the other data structure I proposed above (i.e. $words["apes"] = true) can potentially take advantage of some of the smarts built into the engine, and could potentially be VERY quick, although I haven't tested it myself for performance. In any question of performance the best way is usually to actually test things out, but my gut says that this technique may be the best way for you to go for what you're trying to achieve.
02/09/2007 (2:52 pm)
@Isaac - You've more or less got it, I think (although you seem to be interchanging $actualWord and $actualWord[0] even though they're not the same variable). You would do something like this:$words[0] = "ape";
$words[1] = "apes";
$words[2] = "apetite";
$words[3] = "apetites";
$words[4] = "astro";
$NUM_WORDS = 5;
for (%i=0; %i<$NUM_WORDS; %i++)
{
if ($words[%i] $= $actualWord)
{
echo("you have a word:" SPC $words[%i]);
break;
}
}(That "break" in there will make the for loop stop iterating, since there's no point in further checking once you've found the match with your word).Now, the big problem you're likely to have with this method is that every time you make the check you potentially have to do the comparison for every single word you have. If you have a lot of words that can be a lot of comparisons, so the performance of your game might not be acceptable, especially if you're doing this check frequently. Also, the other downside is that the game will basically get slower and slower as you keep adding new words to your list.
Switching to the other data structure I proposed above (i.e. $words["apes"] = true) can potentially take advantage of some of the smarts built into the engine, and could potentially be VERY quick, although I haven't tested it myself for performance. In any question of performance the best way is usually to actually test things out, but my gut says that this technique may be the best way for you to go for what you're trying to achieve.
#15
so this will be possible to put letters together into a whole word?
02/09/2007 (2:58 pm)
@Dan: Thanks for that code snipet! I will try it right now ;)so this will be possible to put letters together into a whole word?
$actualWord[0] = $letter[0] @ $letter[13] @ $letter[3];
#16
02/09/2007 (3:08 pm)
@Isaac: Yes, after your line of code is executed you would have "AND" in the variable $actualWord[0] (although not in $actualWord -- in torquescript there's no relationship between $actualWord and $actualWord[0]). I'm not exactly clear on how you're figuring out that the user clicked those three letters in that particular order. I would think you'd want something like:$actualWord = $actualWord @ $newLetter;that you would run each time a new letter was selected.
#17
I will try to do this game in TGB. If not possible then I will try in Flash. But if I'm succeed I will have one of the most word letters up to date... hehehe (I have lots of selfsteem) ;)
02/09/2007 (3:31 pm)
Cool!I will try to do this game in TGB. If not possible then I will try in Flash. But if I'm succeed I will have one of the most word letters up to date... hehehe (I have lots of selfsteem) ;)
#18
@Dan: Thanks for the heads up on TGB's 'array' handling using string instead of numerical indexes (eg myArray["Bob"] instead of myArray[0] . I guess it all makes sense when you realise that it's really just being converted to a string variable name anyway (myArrayBob or myArray0). myArray["Bob"] is a little reminiscent of Director's property lists, but again without any count or getAt positioning abilities.
02/09/2007 (4:07 pm)
@Isaac: unless you're doing some kind of major graphics funkiness in this word game, I find it hard to believe Flash would choke on performance. And it's strength for me is how it handles movieclips, so I was a bit surprised you found it 'unable to manage lots of sprites (movie clips)', unless they all had alphas and were animating or something. I'm not pushing Flash over TGB at all, but I do tend to agree with Dennis that given your game, Flash would seem more suited than TGB. Nothing wrong with learning a new program like TGB by starting with a project though!@Dan: Thanks for the heads up on TGB's 'array' handling using string instead of numerical indexes (eg myArray["Bob"] instead of myArray[0] . I guess it all makes sense when you realise that it's really just being converted to a string variable name anyway (myArrayBob or myArray0). myArray["Bob"] is a little reminiscent of Director's property lists, but again without any count or getAt positioning abilities.
#19
I was working in a very animated full of sprites game with thousand of lines and hundreds of variables and Flash was not enough powerful to me, so I decided to abandon that project... I start to hate the lack of speed (I was working with lots of animated sprites on the stage) and the choppiness (is this a valid word?). I feel that the power of Flash is not enough for my imagination. But I'm not the big expert programmer, so maybe I'm wrong, but now that I have found TGB I have retake that project and until now is working pretty cool, because if possible someday, it will be a very interactive word game with a twist and bonus levels... I believe that TGB would be great due to its power and to something that still amaze me: packages!
If I found that this game is not possible in TGB, I will try again with flash limiting my self to the wird search concept.
But I know something: a game is possible thanks to the communities: thanks everybody!
02/09/2007 (4:18 pm)
@Minty:I was working in a very animated full of sprites game with thousand of lines and hundreds of variables and Flash was not enough powerful to me, so I decided to abandon that project... I start to hate the lack of speed (I was working with lots of animated sprites on the stage) and the choppiness (is this a valid word?). I feel that the power of Flash is not enough for my imagination. But I'm not the big expert programmer, so maybe I'm wrong, but now that I have found TGB I have retake that project and until now is working pretty cool, because if possible someday, it will be a very interactive word game with a twist and bonus levels... I believe that TGB would be great due to its power and to something that still amaze me: packages!
If I found that this game is not possible in TGB, I will try again with flash limiting my self to the wird search concept.
But I know something: a game is possible thanks to the communities: thanks everybody!
#20
This is working! I can link letters to make words and if a word is stored I get points! It's incredible... I will upload to show off as soon as I complete my porting from flash to TGB. WOW
02/09/2007 (4:43 pm)
Everyone:This is working! I can link letters to make words and if a word is stored I get points! It's incredible... I will upload to show off as soon as I complete my porting from flash to TGB. WOW
Torque Owner Isaac Barbosa
IQ Games
var myArray:Array = new Array("object1", "object2", "object3" and so on).
how can I do this using torque?