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
#22
Enjoy your weekend!
Well, it was a pain to me catch the logic of programmation (I'm 32 years old and my background is as psychologist, so I'm learning by my self and thanks to people as you, from the community). I don't want to waste my time with flash because my interest is now in TGB and I see it is perfect for the games I want to do. I want to forget about keyframes... hehehe... but I will try Flash 9 when released.
Take care everyone :)
02/09/2007 (4:57 pm)
@Dennis:Enjoy your weekend!
Well, it was a pain to me catch the logic of programmation (I'm 32 years old and my background is as psychologist, so I'm learning by my self and thanks to people as you, from the community). I don't want to waste my time with flash because my interest is now in TGB and I see it is perfect for the games I want to do. I want to forget about keyframes... hehehe... but I will try Flash 9 when released.
Take care everyone :)
#23
But then I realised TGB doesn't have string < or > comparisons! So I've got a new suggestion.
First, let me define the problem. You have a user input of a minimum of 3 letters. You need ALL words from a big list that start with those letters (so as you said APE would match APE, APES and APETITE).
My solution, to help performance, would be to first parse your big word list. You might take a performance hit at the start, but then it will make things fast during gameplay.
This will give you a whole load of variables with words grouped by their first 3 letters.
In the above example, it'd be:
$checkant=" ant ants"
$checkape=" ape apes apetite"
$checkapp=" apple application"
Parsing through only those words with at least the first 3 matching letters obviously reduces your iterations hugely!
To check user input for matching words, you'd do something like:
for (%i=0; %i<$NUM_WORDS; %i++)
{
// grab the next word from our small string
// of words starting with the first 3 chars
%oneWord=getWord(%wordsToCheck) ;
if(getSubStr(%oneWord,%userWordLength,3)$=$userWord){
// got a match, so do whatever you need to do
// with %oneWord - add it to a list of matches?
}
}
I've typed all this in here and haven't tested it (gotta fly out the door with my son!) but it should give you a good start...
Let me know how you get on or if you've got more questions.
02/09/2007 (5:08 pm)
Isaac, I started to write up a solution to your problem, using a classic sorting routine where you check the middle word in your wordlist and check whether your comparison word is less or greater. Then you jump up or down by half that much again. By doing this, within a few quick comparisons you find the nearest match, even on a huge list of words. It's so much faster than iterating through the entire list.But then I realised TGB doesn't have string < or > comparisons! So I've got a new suggestion.
First, let me define the problem. You have a user input of a minimum of 3 letters. You need ALL words from a big list that start with those letters (so as you said APE would match APE, APES and APETITE).
My solution, to help performance, would be to first parse your big word list. You might take a performance hit at the start, but then it will make things fast during gameplay.
$myBigListOfWords="ant ants ape apes apetite apple application" etc etc etc.
$NUM_WORDS = getWordCount($myBigListOfWords);
for (%i=0; %i<$NUM_WORDS; %i++)
{
// grab the next word from our huge string of words
%oneWord=getWord($myBigListOfWords) ;
// get the first three chars- not sure if TGB is 0-based!
%first3chars=getSubStr(%oneWord,1,3) ;
// add this to a variable that keeps track of ALL words starting with those 3 letters
$check[%first3chars]=$check[%first3chars] SPC %oneWord ;
}This will give you a whole load of variables with words grouped by their first 3 letters.
In the above example, it'd be:
$checkant=" ant ants"
$checkape=" ape apes apetite"
$checkapp=" apple application"
Parsing through only those words with at least the first 3 matching letters obviously reduces your iterations hugely!
To check user input for matching words, you'd do something like:
$userWord="apes" ; // for instance %userWordLength=strlen($userWord) %first3chars=getSubStr($userWord,1,3) ; // use those first 3 chars to work out which variable we're checking %wordsToCheck=check[%first3chars] ; $NUM_WORDS = getWordCount(%wordsToCheck);
for (%i=0; %i<$NUM_WORDS; %i++)
{
// grab the next word from our small string
// of words starting with the first 3 chars
%oneWord=getWord(%wordsToCheck) ;
if(getSubStr(%oneWord,%userWordLength,3)$=$userWord){
// got a match, so do whatever you need to do
// with %oneWord - add it to a list of matches?
}
}
I've typed all this in here and haven't tested it (gotta fly out the door with my son!) but it should give you a good start...
Let me know how you get on or if you've got more questions.
#24
Thanks for that new contribution... looks very interesting, but I can't understand at all because there is a new element... that I have never used before: string.
This means that I can store my words in a string? and that an array can store strings? if that's so... that's great!
I will try that tomorrow. Enough for today! :)
Good night everyone
02/09/2007 (5:17 pm)
@Minty:Thanks for that new contribution... looks very interesting, but I can't understand at all because there is a new element... that I have never used before: string.
This means that I can store my words in a string? and that an array can store strings? if that's so... that's great!
I will try that tomorrow. Enough for today! :)
Good night everyone
#25
And arrays are really just data containers or organisers, so they usually end up containing the basic types of data.
So strings aren't that complex. You'll get up and running soon.
02/10/2007 (1:22 am)
Isaac: You have 3 main basic types of data : Numerical (numbers), strings (alphanumeric) and boolean (true/false). In your case, as your game deals with words, regardless of whether you're using Flash or TGB, you'll be dealing with strings.And arrays are really just data containers or organisers, so they usually end up containing the basic types of data.
So strings aren't that complex. You'll get up and running soon.
#26
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4711
I am going to try compiling it today...but it seems (if it works) it allows one to use arrays semi functionality with sorting and counting options.
02/10/2007 (7:45 am)
As far as array functionality goes...I did come across thishttp://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4711
I am going to try compiling it today...but it seems (if it works) it allows one to use arrays semi functionality with sorting and counting options.
#27
It seems that using the array list TGB can handle more than 12000 words in less that you can click the mouse button again :)
@Craig: Thanks for that link, I dont need taht functionality and I can risk to mess up my engine ;) not so programmer! :)
02/10/2007 (9:10 am)
@Minty, I can't use your code, it seems to works except for the %wordsToCheck=check[%first3chars] ; thing :(It seems that using the array list TGB can handle more than 12000 words in less that you can click the mouse button again :)
@Craig: Thanks for that link, I dont need taht functionality and I can risk to mess up my engine ;) not so programmer! :)
#28
I've finally had a little time to tweak (and debug!) my script. I'm a bit of a TBG newbie, so there may be better ways of doing things, but this seems to be working fine.
At the moment, you won't see much unless you open the console (~ key). Then you can see various tests working. You should be able to hook into the checkWord function with your user-inputted word easily enough, and then it's up to you what you do with the matches. I've commented it fairly well, so you should be able to work out what it's doing.
Good luck with your project! When you get a working version up, let me know :)
Cheers
Minty
02/11/2007 (3:33 pm)
Hi Isaac,I've finally had a little time to tweak (and debug!) my script. I'm a bit of a TBG newbie, so there may be better ways of doing things, but this seems to be working fine.
At the moment, you won't see much unless you open the console (~ key). Then you can see various tests working. You should be able to hook into the checkWord function with your user-inputted word easily enough, and then it's up to you what you do with the matches. I've commented it fairly well, so you should be able to work out what it's doing.
Good luck with your project! When you get a working version up, let me know :)
Cheers
Minty
// Script by Minty to solve a problem posted by Isaac Barbosa, feb 2007
// (I'm a TGB newbie, so any edits/suggestions welcomed!)
//
// Intended usage: a word matching game.
// You have a user input of a minimum of 3 letters.
// You need ALL words from a big list that start with those letters
// (so APE would match APE, APES and APETITE and any other words starting with APE).
//
// Run it with the console open (~ key) to see some tests
function initWordLists(%mainWordList)
{
// this pushes the words from our big wordlist
// into variables based on the first 3 characters of the words,
// using TGB's 'array' implementation.
//
// As an example, the variable $checkant will end up ="ant ants antelope"
// (or as many words that begin with 'ant' in your big wordlist)
// and $checkape will = "ape apes apetite"
// (or as many words that begin with 'ape' in your big wordlist)
// etc.
// This means our later tests against a user's input in the checkWord function
// will only need to check against words with at least those
// 3 matching first chars, which obviously helps performance
//
$NUM_WORDS = getWordCount(%mainWordList);
echo("sorting" SPC $NUM_WORDS SPC "into variables based on first 3 chars");
for (%i=0; %i<$NUM_WORDS; %i++)
{
// grab the next word from our huge string of words
%oneWord=getWord(%mainWordList, %i) ;
// get the first three chars- (TGB is 0-based, so the 'first' character is at 0)
%first3chars=getSubStr(%oneWord,0,3) ;
// grab any current data from this to a variable
// that keeps track of ALL words starting with those 3 letters
%currentData=$check[%first3chars];
if(%currentData$=""){
// no data in there yet, so let's just start with this word
%newData = %oneWord ;
}else{
// add this word to the words already there
%newData =%currentData SPC %oneWord ;
}
// update the data back into our variable
$check[%first3chars]=%newData;
}
echo("---------- done ------------" NL "");
}
function checkWord(%userWord)
{
// finds all words that at least begin with %userWord
//
// get the length of the word to check
%userWordLength=strlen(%userWord);
// grab it's first 3 characters
%first3chars=getSubStr(%userWord,0,3);
// get the list of words to check that begin with these 3 chars
// (see initWordLists function above)
%wordsToCheck=$check[%first3chars] ;
echo("checking word" SPC %userWord SPC "against these words:" SPC %wordsToCheck);
// how many words are we checking against?
%numCheckWords = getWordCount(%wordsToCheck);
for (%i=0; %i<%numCheckWords; %i++)
{
// grab the next word from our list
// of words starting with the first 3 chars
%oneWord=getWord(%wordsToCheck, %i) ;
// make sure this word at least _begins_ with the userword
if(getSubStr(%oneWord,0, %userWordLength)$=%userWord){
// got a match, so do whatever you need to do
// with %oneWord - add it to a list of matches?
echo("MATCH" SPC %oneWord);
}
}
echo("---------- done ------------" NL "") ;
}
// if we haven't set up our big list of words, do it now!
if($myBigListOfWords$=""){
// obviously, you'd replace the following with your big list of words
// or perhaps load it in from an external file
$myBigListOfWords="ant ants anticipate ape apes apetite apple application approach";
initWordLists($myBigListOfWords);
}
// let's run some tests and see what we get
checkWord("ant");
checkWord("ants");
checkWord("ape");
checkWord("apet");
checkWord("appl");
#29
That's not a code for a newbie! hahaha I can't understand it at first glance, so I need to put some effort to do so :)
I will try it today. But I have some questions:
I have a word list of 3000 words now, using the method of save every word in the array using
$words[0] = "about";
$words[...] = "abbot";
$words[3000] = "abanic";
when I search a word using this method the answer is instantaneous. So if I change to your code the gained performance will be in speed or in memory usage or both? I think that TGB has to found that a player input is not a match so maybe the resources usage will be the same... or not?
Maybe this method will word for a very huge list of words?
where may I be able to check if the performance/memory usage is better/less?
I will try to use this code right now -it will take me the whole day I guess, ;) -
again, thanks!
02/12/2007 (7:56 am)
Wow Minty!That's not a code for a newbie! hahaha I can't understand it at first glance, so I need to put some effort to do so :)
I will try it today. But I have some questions:
I have a word list of 3000 words now, using the method of save every word in the array using
$words[0] = "about";
$words[...] = "abbot";
$words[3000] = "abanic";
when I search a word using this method the answer is instantaneous. So if I change to your code the gained performance will be in speed or in memory usage or both? I think that TGB has to found that a player input is not a match so maybe the resources usage will be the same... or not?
Maybe this method will word for a very huge list of words?
where may I be able to check if the performance/memory usage is better/less?
I will try to use this code right now -it will take me the whole day I guess, ;) -
again, thanks!
#30
The way player is searching for word is on a letter by letter basis, player is not searching for a whole word at once, it begin to play clicking -if we are searching for apple- over an "a", then "p" "p", "l" and "e". so every time player picks over a new letter TGB is searching for a matching word.
02/12/2007 (7:59 am)
Another important point is this:The way player is searching for word is on a letter by letter basis, player is not searching for a whole word at once, it begin to play clicking -if we are searching for apple- over an "a", then "p" "p", "l" and "e". so every time player picks over a new letter TGB is searching for a matching word.
#31
So if I use this code every word in my list will be translated into a variable once game is launched for the first time and then every time the player searches for a word the process will be faster and better than searching every single word for the entire list each time?
Sounds cool!
02/12/2007 (8:30 am)
@minty:So if I use this code every word in my list will be translated into a variable once game is launched for the first time and then every time the player searches for a word the process will be faster and better than searching every single word for the entire list each time?
Sounds cool!
#32
02/12/2007 (9:43 am)
@minty: my game is working pretty cool using my method... I will keep it unless I see the game turns choppy or something :) I will show you once done ;)
#33
Now, that said, I'm kind of surprised that it's working so fast for you, but as you can see from this thread, there are plenty of ideas out there for how to speed up your search if you figure out later that you need it.
02/12/2007 (10:19 am)
Quote:my game is working pretty cool using my method... I will keep it unless I see the game turns choppy or somethingIsaac, this is actually a very good idea. A lot of game developers spend a lot of time "optimizing" parts of their code that they suspect will be a performance problem without actually checking if they ARE a performance problem first. Making games is hard enough without spending your time solving problems that don't need to be solved!
Now, that said, I'm kind of surprised that it's working so fast for you, but as you can see from this thread, there are plenty of ideas out there for how to speed up your search if you figure out later that you need it.
#34
Thanks to both :)
02/12/2007 (10:35 am)
@Dan: I believe that Minty code is awesome but use it implies for me start from scratching with a lot of already working functions :( As you said, I will use it later if I notice that my game needs more speed or if I discover a problem. But now is working pretty fast with more than 3500 words -faster than my clicks!- And of course, I will not use hundred of thousands of words... I will limit the game to maybe 10000 common words... I can't see a women searching for "quintessence" or "meretricious"... but "mountain" "bike" hammer" "monkey" and so are common words easy to search and use :)Thanks to both :)
#35
I think I misread your post at some point and thought performance was an issue, but perhaps you were talking about Flash then.
My code is definitely faster than iterating the whole word list because it only makes you match against words starting with the same 3 letters as the user input. But if you're talking minor milliseconds of improvement, hey, until you overload your game with particle effects and see it bog down, go with what you've got :)
I'm trying to wrap my head around TGB too, so I saw it as a chance for me to tackle a focused problem and just begin getting familiar with TGB. I learnt about what string operators TGB has (good word count ones, no <> comparisons!), and that you can create dynamically named variables like $check[%var], and a few other things. So whether you use the code or not, it's been fun for me :)
Good luck with the rest of your project!
02/12/2007 (12:15 pm)
@Isaac: If you've got something that's working, I entirely agree with Dan: go with that! I think I misread your post at some point and thought performance was an issue, but perhaps you were talking about Flash then.
My code is definitely faster than iterating the whole word list because it only makes you match against words starting with the same 3 letters as the user input. But if you're talking minor milliseconds of improvement, hey, until you overload your game with particle effects and see it bog down, go with what you've got :)
I'm trying to wrap my head around TGB too, so I saw it as a chance for me to tackle a focused problem and just begin getting familiar with TGB. I learnt about what string operators TGB has (good word count ones, no <> comparisons!), and that you can create dynamically named variables like $check[%var], and a few other things. So whether you use the code or not, it's been fun for me :)
Good luck with the rest of your project!
#36
I agree too :) and you're right: I need to finish with all the action and particles to see if there is a loss in the performance. If that happens I will change to your solution!
I don't know if I read well, but in Taurina's book there is a trick to do <> text comparisons! If you need that I will post it for you ;)
Thanks
02/12/2007 (1:35 pm)
@Minty:I agree too :) and you're right: I need to finish with all the action and particles to see if there is a loss in the performance. If that happens I will change to your solution!
Quote:(good word count ones, no <> comparisons!)
I don't know if I read well, but in Taurina's book there is a trick to do <> text comparisons! If you need that I will post it for you ;)
Thanks
#37
Does case-sensitive lexicographic comparison of string1 and string2.
Returns the following.
-1 if string1 comes before string2 in alphabetical order.
0 if string1 is equivalent to string2.
1 if string1 comes after string2 in alphabetical order.
Does case-insensitive lexicographic comparison of string1 and string2.
Returns the following
-1 if string1 comes before string2 in alphabetical order.
0 if string1 is equivalent to string2.
1 if string1 comes after string2 in alphabetical order.
from Maurina's GPGT.
02/14/2007 (8:44 am)
@Minty:strcmp(string1, string2)
Does case-sensitive lexicographic comparison of string1 and string2.
Returns the following.
-1 if string1 comes before string2 in alphabetical order.
0 if string1 is equivalent to string2.
1 if string1 comes after string2 in alphabetical order.
stricmp(string1, string2)
Does case-insensitive lexicographic comparison of string1 and string2.
Returns the following
-1 if string1 comes before string2 in alphabetical order.
0 if string1 is equivalent to string2.
1 if string1 comes after string2 in alphabetical order.
from Maurina's GPGT.
#38
I'll post a request for it to be added to that area, as it would make sense under 'Relations and Logical Operators'.
02/14/2007 (12:16 pm)
@Isaac: Duh. Fantastic. Thanks for the heads up on string comparison. Somehow I missed it in the TGB reference (probably because I was looking for <> symbols), and it's not in the Torquescript Reference (taken from Maurina's book). I'll post a request for it to be added to that area, as it would make sense under 'Relations and Logical Operators'.
Torque Owner Dennis Harrington
Also, if Flash 8 really can't handle your game, download the AS3 add-on which uses the new Flash virtual machine. It's been clocked at up to 100 times faster than the old Flash VM for some types of processing and it's significantly faster across the board.
Again, don't get me wrong, I love TGB! For certain types of games it's head and shoulders above Flash. But always try to use the best tool for the job at hand and sometimes Flash is that tool.
I can try to send you some code next week if you'll still need it. I'm about to head out for the weekend so no more programming for me for a few days. ;)
Edit: It looks like you posted your Eureka! moment just before I posted this. So congrats and good luck!