Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Looping through array problem

Author
Message
Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 31st Oct 2011 22:47
Ive struggled at least 1/2 an hour to come up with a title that wasnt "Help", but im not sure what this problem is. Plus its 2 problems really.

I cant give any compiling code as its far too long for anyone to want to read so i can only give snippets and explain what i think its going wrong

Basically this program splits the screen up into 7 (a top box then 6 even boxes below it like a table) and displays some things that are related to the "faction" that has been created.




This displays everything in the right place. It works and its great, puts everything right where i want it ish (see below).

Now there are 6 boxes and 6 desired factions when the game first runs. But for some reason when creating a faction using this code



It creates factions 1 - 3 perfectly. But factions 4 - 6 do not show up properly. What i mean by this is there is no name, no tactic, no random amount of money designated to them (which the spawnfaction function does, allocates them about 200 - 500 or something randomly - randomize seed has been used and works fine)
So its setting nothing up.

1) Why do the first 3 work, what would be the reason for only the first 3 working then abandoning the other 3?

2) What displaying my output everything is fine, sort of. The Faction(factions number).TextX NEVER changes once its been set up and this is also backup up by the fact that if i place a character at the beginning of every string they all line up perfectly. Now each of the strings is the same length, i have merely pressed space enough times to make the : all fit together. When its run though they are all over the place.

So if a string has 15 characters, wont it be the same size as another string with 15 characters. If its going off some sort of pixel by pixel count of the font i guess i could understand only the last version of this that i did (which i am completely redoing) used the old system font and worked fine but had a slightly different method of outputting the text in the right place. This time i am using arial. So do strings with different fonts represent different sizes even if the have the same number of characters in that string and aer the same font.

E.G
"Faction Leader Name : "
" Peasants : "
" Coins : "

These all line up as they should here but not in my program.
Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 1st Nov 2011 00:06
Quote: "Dec DesiredFactions"
This doesn't look right, why do you need to decrease this variable?

Another thing is that you do not need to use [EXIT] on a for loop which repeats to its target; which in your case is [DesiredFactions].

Also, you do not need to check if DesiredFactions is > 1; it has got to be because the loop would not start if it is less than one, because A, which is 1, is the minimal value in your incremented for loop.



Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 1st Nov 2011 00:14
Yeah i have been working on it the last hour or so and i think i know what it is, because its decreasing it then going back through the loop its not working properly on the other iterations. If i use maxfactions as a constant it works fine.
Im not 100% sure what it was doing as i couldnt follow ti but it was the culprit and adding that constant finally fixed it. It works great.

Just struggling with the font issue. Im sure its to do with pixel width for arial characters.
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 1st Nov 2011 10:17
Quote: "Just struggling with the font issue. Im sure its to do with pixel width for arial characters. "


If you're having that much trouble with the fonts you could always turn them into sprites by rendering your text to a separate bitmap and then spriting them to the screen (put texture smoothing ON when getting the image that you plan to scale very large, otherwise turn it off for a finer image with smaller fonts). It uses more resources but it might be easier (You'll also have more flexibility over appearance). NOTE: This can be done with a 3D plain too.



Chris Tate
DBPro Master
16
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 1st Nov 2011 13:05
Quote: "If i use maxfactions as a constant it works fine."


Cool, do you know why it behaved that way when you changed it to a constant?

Perhaps it is a good idea to post screenshots of your font issue, so we can get a better look of what you are experiencing; in the text thread.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 1st Nov 2011 17:50 Edited at: 1st Nov 2011 17:56
Quote: "So if a string has 15 characters, wont it be the same size as another string with 15 characters. If its going off some sort of pixel by pixel count of the font i guess i could understand only the last version of this that i did (which i am completely redoing) used the old system font and worked fine but had a slightly different method of outputting the text in the right place. This time i am using arial. So do strings with different fonts represent different sizes even if the have the same number of characters in that string and aer the same font."


The system font is designed to be the same size but if you use other fonts each character can have a different size so it won't line up properly. The easiest way to line up text is to use the TEXT WIDTH command.



Somarl
13
Years of Service
User Offline
Joined: 11th Feb 2011
Location: UK
Posted: 1st Nov 2011 20:51
Quote: "turn them into sprites by rendering your text to a separate bitmap and then spriting them to the screen "

Aye thats a good idea, i will keep that one in mind for any future projects. The one im working on at the mo doesnt really need it but its all good to know, thanks.

Quote: "Cool, do you know why it behaved that way when you changed it to a constant?"

DesiredFactions was a global variable that allowed me to create another faction or 2 on the fly based on a general game director that overlooks the game and decides whether to create more factions, at first on initial setup this was six, the maximum number of factions allowed. As it decreases and gets to 0 no more factions are spawned. If later on a few get destroyed then there may be a need for more factions so that number will then increase, on the game loop if it sees there is a need to make a faction it will create one in the nearest space that has a 0 for the factionnumber.
What i did instead was create a constant (could have been a variable but seen as it would never change i thought id make it a constant) called "MaxFactions" which will always be 6. Now i can loop through each faction in a for loop using things like
For a = 1 to MaxFactions
etc
When i tried to do that at first with the desiredfactions variable as it was always decresing i think it messed it up on its 4th, 5th and 6th iterations.

@Grog, youve hit the nail on the head regarding the font issue. I had a feeling it would be that. As each letter is a different width between it for example a space will be smaller in pixels that an 'L' which would be larger than just 'l'. This was throwing off the way the string looked when printed. Changing it back to the system font works fine as like grog said they all must be designed the same width including spaces.
I will have a play around with the text width command and if it doesnt look good then ill skip it and just use the system font. Im only trying to demo a very very simple text based desicion making AI. Nothing fancy. If i do make something fancy i will obviously have to bear what the fonts are going to look like in mind.

Thanks folks!!
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 2nd Nov 2011 04:21
Quote: "When i tried to do that at first with the desiredfactions variable as it was always decresing i think it messed it up on its 4th, 5th and 6th iterations."
Yep, the reason that it was missing the 4th,5th, and 6th iterations was because the loop equated to:
1) a=1 to 6
2) a=2 to 5
3) a=3 to 4
4) a=4 to 3 = ERROR: break from the loop

Login to post a reply

Server time is: 2024-11-22 12:04:13
Your offset time is: 2024-11-22 12:04:13