forums

TNT Basic Forums > Programming Q&A
If statemtns inside loops
< Last Thread     Next Thread >
Author
Thread        Post A Reply

05-29-2003 17:57

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Hi,
All the else if statement of the below code has two highlighted errors (11 in total! This is more than the entire amount for the rest of my game before I put this bit in!) on the lines between the else if statements, although, to the best of my knowledge, it should work fine with the string variable, probably just something simple, but I'm stuck. Have also tried putting the string and count variables in the loop, but this has no effect, as do the spaces between the where the square variable is called and the number after it to reference the Global integers. The relevant code is included below,
thanks in advance for any help.

Global int A1=518, A12=218 'This is what the square variables is referencing
Global int A2=323, A22=173 'Few more of these

int spriteID=file read int(field) 'This is pre-selected by the user and held in a file for reference
string square="A"
int n=0
int count=1 'Count no of loops
repeat

if pressed(n) and pressed(18)
sprite spriteID,square 1, square 12
else if pressed(n) and pressed(19)
sprite spriteID,square 2, square 22
else if pressed(n) and pressed(20)
sprite spriteID,sqaure 3, square 32
else if pressed(n) and pressed(21)
sprite spriteID,square 4, square 42
else if pressed(n) and pressed(22)
sprite spriteID,square 5, square 52
draw frame
end if

int count=count+1

if count=2
string square="B"
int n=11
else if count=3
string square="C"
int n=8
else if count=4
string square="D"
int n=2
else if count=5
string square="E"
int n=4
end if
until count=6

05-29-2003 21:05

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

The error is with the sprite statements within the else ifs. Eg

sprite spriteID,square 1, square 12

You're passing 'square 1' and 'square 12' as parameters but they aren't valid variable names as they have spaces in them. If you want square to be an array declare it like this:

int square[10]

'... fill out square array...
sprite spriteId, square[0], square[1], image

If you want to use variables called square1 and square12 then declare and use them like this

int square1=3
int square12=44

sprite spriteId, square1, square12, image

Hope this helps!

Mark

05-31-2003 17:09

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Just a few points,
Quote
The error is with the sprite statements within the else ifs
End quote

I knew that! That's what the post is about!
Also, I tried both with and without spaces in the variable names, it did not make a difference. The variables I'm trying to reference are declared as Globals earlier on in the code.

e.g. Global int A1=518, A12=218 'This is what the 'square' variable with the number after it is referencing

For some reason, TNT is not recognising the string variable "square" as the letter which I want to make up the first part of the Global variable name (declared earlier) which I then need it to reference.

Hope this clears a few things up,
Mark_667

05-31-2003 18:51

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

> Global int A1=518, A12=218 'This is what the square variables is referencing
> Global int A2=323, A22=173 'Few more of these
>
> int spriteID=file read int(field) 'This is pre-selected by the user and held in a file > for reference
> string square="A"
> int n=0
> int count=1 'Count no of loops
>
> repeat
>
> if pressed(n) and pressed(18)

if pressed (n) ?
The first time, n is 0, meaning A.
The second time, n is 1, meaning S.
The third time, n is 2, meaning D.
The fourth time, n is 3, meaning F.
The fifth time, n is 4, meaning H.
The sixth time, n is 5, meaning G.

I don't know why the user should press these keys in the same time as numbers????

if you press A and 1 at the same time, show image 12
if you press S and 2 at the same time, show image 22
if you press D and 3 at the same time, show image 32
...
If you press nothing, the program won't stop and will check for the next keypresses.

I hope you realise it doesn't make any sense.

> sprite spriteID,square 1, square 12
> else if pressed(n) and pressed(19)
> sprite spriteID,square 2, square 22
> else if pressed(n) and pressed(20)
> sprite spriteID,sqaure 3, square 32
> else if pressed(n) and pressed(21)
> sprite spriteID,square 4, square 42
> else if pressed(n) and pressed(22)
> sprite spriteID,square 5, square 52
> draw frame
> end if
>

And what will happen if the player doesn't hit any key? The program will you go on and end after 0.25 second if you are in 24 fps!!! And it's often 3 times more!!!

> int count=count+1
>

You don't need to redeclare the kind of variable it is once it is initialiezed. Write count = count+1, not int count = count+1;

> if count=2
> string square="B"
> int n=11

You don't need to redeclare the kind of variable it is once it is initialiezed. Write n=11, not int n=11; I will not repeat this error, correct it everywhere.

> else if count=3
> string square="C"
> int n=8
> else if count=4
> string square="D"
> int n=2
> else if count=5
> string square="E"
> int n=4
> end if
>
> until count=6

06-02-2003 08:17

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Hi,
Firstly, I did't want n to increment continously, which is why I re-declared its value in the if statement before the loop repeated back on itself so it can be changed as needed for each pass of the loop. Also, now I relaise the mistake about the user having to press the letters at the same time as the numbers how can I get it so that the user can press one key, then another in succession? Would I need a separate loop for this?

Quote I hope you realise it doesn't make any sense.
End quote

Ermm, I do now - thanks for that!

Quote And what will happen if the player doesn't hit any key?
End quote

With this being a board based game needing co-ordinates, if the player doesn't press anything they can't move.

Quote The program will you go on and end after 0.25 second if you are in 24 fps!!! And it's often 3 times more!!!
End quote

In that case do you think it would be a good idea to re-declare the FPS for this procedure? If so will this re-set the game? It could be re-drawn quickly enough, but it might slow things down a little. Thanks for the advice all,
Mark_667

06-02-2003 22:04

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

No No No! I mean ( unless I do not have the whole code ) that the program may go like this:

1 - Verify keypressed
2- Nothing: continue loop
3 - Verify keypressed
4- Nothing: continue loop
5 - Verify keypressed
6- Nothing: continue loop
7- end of code
8- quit

So if the player does not hit any key, the program will quit after less than a second.


------------------------------------------------

About the key thing: ( this example only supports A,S,1,2 but you can adapt it easily )


int pressedS=false, pressed1=false
int pressedA=false, pressed2=false

if pressed("S number here")
pressedS=true
pressedA=false
end if
if pressed("A number here")
pressedA=true
pressedS=false
end if
if pressed("1 number here")
pressed1=true
pressed2=false
end if
if pressed("2 number here")
pressed2=true
pressed1=false
end if

if pressedS and pressed1 then anyAction()
if pressedA and pressed2 then anyAction()


' this should work

06-03-2003 00:59

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Mark,

You're trying to reference the global variables by using a string variable, right? As in


Global int globalVariable1,globalVariable2
string BeginningOfName="globalVariable"

BeginningOfName 1 = 6 '<--This is trying to set globalVariable1 to 6


I don't think you can do that, can you?

06-04-2003 10:54

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

At last! Someone (eekaydee) has the right idea of what I'm trying to do. I know it isn't the easiest code to understand, but declaring variables like this is possible in other BASIC languages such as VB, VBA, so I assume TNT can do something at least similar...

06-04-2003 20:49

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Ahh....

Oh sorry, I hadn't realised. I'm afraid you can't do that in TNT Basic at the moment. Sorry. The best you could do would be to use an array, and use a variable to index into the array.

Eg

int myValues[200]

int wantValue=45

print myValues[wantValue]

Hope this helps,

Mark

06-04-2003 23:17

Posted by:
matteo

Location:
Venice, ITALY!

Click Here to Email matteo   Find more posts by matteo

Using arrays you can do exactly the same stuff and I guess it' s much more tidy

06-04-2003 23:48

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

"I'm afraid you can't do that in TNT Basic at the moment"

Does this mean you plan to implement it in the future?

06-05-2003 12:09

Posted by:
matteo

Location:
Venice, ITALY!

Click Here to Email matteo   Find more posts by matteo

oh please... it' s totally useless... let them work on the important stuff !

06-05-2003 12:23

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Someone said this can be done in an array, but I don't see how as the sprites still need to access the Global variables, referenced through the string, for their x and y co-ordinates. The only way I can think of doing this so it corresponds exactly with the user input is as above, unless, of course, I have an if statement for each one :-(

thanks for the information,
Mark_667

06-05-2003 22:53

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

either that, or you change your variables; rather than have A1, A2...A10 and B1, B2,...B10 and C1, C2,...C10 etc, you could put all of them in one array

A=0
B=10
C=20
int Array[30] ' an array with 0 to 29

sprite spriteID,Array[square+1], Array[square+12]

or whatever

06-05-2003 22:57

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

and yeah, I agree with you matteo. It isn't a problem

06-08-2003 13:14

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

that array...

Hi,
Thought that last suggestion would work-it ALMOST did. In the included code below TNT highlights the first call of the Globals_Array as an error because, for some reason, it either can't access it or can't recognise it. The numbers 1 and 12, declared after the brackets as errors.
Also, it looks like when I tried to call it as suggested:
(User_String_Input+1)
TNT must assume you want to add 1 (an integer) onto a string value.

I declared the array thus so this is OK...

int Globals_Array[49] 'Actually only 25 squares, but 2 variables are needed to reference one square, one for the x, one for the y

Globals_Array[0]=518 'A1
Globals_Array[1]=218 'A12
'47 (!) more of these...

int count=1 'Count no of loop passes

repeat
GetText()

if User_String_Input="A" and pressed(18) 'A&1 next time it checks for B&1, if C&1 is pressed, etc.
sprite spriteID,Globals_Array(User_String_Input)1, Globals_Array(User_String_Input)12
else if User_String_Input="A" and pressed(19)
sprite spriteID,Globals_Array(User_String_Input)1, Globals_Array(User_String_Input)12
else if User_String_Input="A" and pressed(20)
sprite spriteID,Globals_Array(User_String_Input)1, Globals_Array(User_String_Input)12
else if User_String_Input="A" and pressed(21)
sprite spriteID,Globals_Array(User_String_Input)1, Globals_Array(User_String_Input)12
else if User_String_Input="A" and pressed(22)
sprite spriteID,Globals_Array(User_String_Input)1, Globals_Array(User_String_Input)12
draw frame
end if

if count=2
User_String_Input="B"
else if count=3
User_String_Input="C"
else if count=4
User_String_Input="D"
else if count=5
User_String_Input="E"
end if

count=count+1
until count=6

With any luck this will be my last posting-this is driving me INSANE!!!

06-08-2003 20:24

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Hi Mark,

if User_String_Input="A" and pressed(18) 'A&1 next time it checks for B&1, if C&1 is pressed, etc.
sprite spriteID,Globals_Array(User_String_Input)1, Globals_Array(User_String_Input)12
....


You've got an error here, you get values from an array the same was as you put them in, using the square brackets []. So value 4 in the array is Globals_Array[4] NOT Globals_Array(4).

Secondly, you appear to be trying to index the array with a string called User_String_Input. You must use integers to index into an array, you cannot use strings.

Thirdly, you cannot put two expressions next to each other without using a binary operator, such as + - / or * to combine them. I'm not sure what Globals_Array(User_String_Input)1 is supposed to mean, but the 1 following the value from the array won't work.

If you explain more clearly what you are trying to do, me or someone else may be able to give you a code example.

Cheers,

Mark

06-11-2003 10:54

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Quote:
you get values from an array the same was as you put them in, using the square brackets []. So value 4 in the array is Globals_Array[4] NOT Globals_Array(4).
End quote

I tried putting the array entities in squared brackets, but it just generated a different illegal error.

Quote:
Secondly, you appear to be trying to index the array with a string called User_String_Input. You must use integers to index into an array, you cannot use strings.
End quote

Ermm, didnít know that. Iíll have to get the user to input the array value and have an on-screen table or something, not the preferred way of doing it, but there seems to be no way to convert the usersí (text) input into a number the array can reference, because even if they were all done continuously to make up the first part of the array entity number e.g. A=1, B=2, then another integer so we get sprite SpriteID,Globals_Array 00,Globals_Array01 This moves the sprite (whatever the sprite ID happens to be) to square A1 (x=00, y=01) the array entities still have to be sequential.

Quote:
Thirdly, you cannot put two expressions next to each other without using a binary operator, such as + - / or * to combine them. I'm not sure what Globals_Array(User_String_Input)1 is supposed to mean, but the 1 following the value from the array won't work.
End quote

Yes this bit did get a bit confusedÖI donít need the 1 and 12 variables after it now the variables are in the array, sorry for any unnecessary confusion.

Iím trying to get TNT to recognise the entity as such:

sprite spriteID,Globals_Array[User_Input], Globals_Array[User_ Input]
equivalent to:
sprite SpriteID,518,218
equivalant to:
sprite SpriteID,referencearrayvalue0,referencearrayvalue1

06-11-2003 16:39

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Maybe someone already pointed this out, but you have to declare the variables as arrays.

e.g.:

string input[10]

06-12-2003 12:44

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

OK, OK, I give up! I think I've got it to work now!
Thanks for all the feedback,
mark_667

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.