Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Squinky on Mon 17/11/2003 03:09:33

Title: Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 03:09:33
It tells me that the end of input is reached in the middle of the expression....I'm sure it's simple, but I just can't wrap my brain around it...


//character attack------------------------------
if (Random (20)<(getstats(1,4)-(getstats(2,1){
  setstats (2,2,getstats(1,3)-Random (3));
  Display ("You swing and hit");
 
else if  (Random (20)>(getstats(1,4)-(getstats(2,1){
  setstats (2,2,getstats(1,3)-Random (3));
  Display ("You swing and miss");
}  
Title: Re:Okay, what am I doing wrong here?
Post by: Fuzzpilz on Mon 17/11/2003 03:17:00
Looks like you're missing a few )s and a }  there.


//character attack------------------------------

if(Random (20)<(getstats(1,4)-(getstats(2,1)))
{
setstats (2,2,getstats(1,3)-Random (3));
Display ("You swing and hit");
}
else if (Random (20)>(getstats(1,4)-(getstats(2,1)))
{
setstats (2,2,getstats(1,3)-Random (3));
Display ("You swing and miss");
}


This should work.
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 03:20:46
Thanks,
But it still says the same message on the first line....Man, I'm frustrated
what is missing here?
Title: Re:Okay, what am I doing wrong here?
Post by: Gilbert on Mon 17/11/2003 03:29:02
Didn't see the problem to fuzz's code, maybe there're more missing stuffs in other perts of the code, you may need to post the whole function here.

Hmmmm I don't know, unless it is your intention, as you called Random(20) twice, so, if the player didn't "hit", due to that extra random(20) in the else part, it would randomize again, and probably won't give you the "missed" response most of the time.I suggest it to be:

//character attack------------------------------
chance=Random(20); //define this variable first of course
if(chance<(getstats(1,4)-(getstats(2,1)))
{
setstats (2,2,getstats(1,3)-Random (3));
Display ("You swing and hit");
}
else if (chance>(getstats(1,4)-(getstats(2,1)))
{
setstats (2,2,getstats(1,3)-Random (3));
Display ("You swing and miss");
}


And currently the code would od nothing if chance equals (getstats(1,4)-(getstats(2,1)), if that's not what you want, the simplest code would be just:

//character attack------------------------------
chance=Random(20); //define this variable first of course
if(chance<(getstats(1,4)-(getstats(2,1)))
{
setstats (2,2,getstats(1,3)-Random (3));
Display ("You swing and hit");
}
else
{
setstats (2,2,getstats(1,3)-Random (3));
Display ("You swing and miss");
}

Title: Re:Okay, what am I doing wrong here?
Post by: Fuzzpilz on Mon 17/11/2003 03:29:32
I'm not sure... what's the exact error message? Could you post that getstats function?

(Also: this code might be wrong for what you're trying to do - I don't exactly know what this is, of course, but it looks as if you should remove the second if.)
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 03:51:36
Gilbot, good catch. With my old script, the second part was even needed so much. But even with these new scripts you guys have provided, it still gives the same error.

The error
end of input reached in the middle of expression
refering to the first line.

What it's for
I'm using a 2*4 array to hold stats for the char and a bad guy. With these stats, I'm hoping to use fashion a battle system based off of Dungeons and dragons mechanics.
so if your familair with that system stats(1,4) is the chars attack rating or thaco, and stats(2,1) is the monsters armor rating.

all the code

//in the monster guys character code:

chance=Random(20); //define this variable first of course
if(chance<(getstats(1,4)-(getstats(2,1)))//this line is the problem one<--
{
setstats (2,2,getstats(1,3)-Random (3));
Display ("You swing and hit");
}
else
{

Display ("You swing and miss");
}

//in the room script, first time enters screen:

int chance=Random (20);

//---char attributes----
setstats (1,1,8);
setstats (1,2,10);
setstats (1,3,-3);
setstats (1,4,18);
//-----mon attribute--------
setstats (2,1,10);
setstats (2,2,10);
setstats (2,3,-2);
setstats (2,4,20);
//-------------------------

//In the global script

int stats[8];

int chance;



function getstats(int x, int y) {
return stats[y * 5 + x];
}
function setstats(int x, int y, int value) {
stats[y * 5 + x] = value;
}

//In the script header

/**/import function getstats(int,int);
/**/import function setstats(int,int,int);

And that should be everything tied to this event....

Title: Re:Okay, what am I doing wrong here?
Post by: After on Mon 17/11/2003 03:52:06
[edit: for latest version]
Remove red:--

if(chance<(getstats(1,4)-(getstats(2,1)))//this line is the problem one<--

I assume spaces are allowed between function names and parentheses? (I never write them like that so I wouldn't know)
Title: Re:Okay, what am I doing wrong here?
Post by: Fuzzpilz on Mon 17/11/2003 03:58:20
Whitespace isn't a problem, no.

Seems Gilbot and I both overlooked the extra ( before the second getstats.
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 04:12:55
Quote from: Fuzzpilz on Mon 17/11/2003 03:58:20
Seems Gilbot and I both overlooked the extra ( before the second getstats.

Yeah, don't feel bad, I did for like an hour....It's compiling and working now, but the math is'nt turning out right, no matter how many times I hit it says the monsters hitpoints are -4 or -3. Any idea what I'm doing there?

Also where would be the proper place to declare that chance=Random (20) bit?
Title: Re:Okay, what am I doing wrong here?
Post by: Gilbert on Mon 17/11/2003 04:18:52
:p

Just declare the chance on top of that script:
int chance;

Hmm I didn't read the codes carefully, but if you're making 2*4 arrays, the correct formula to get the index from [ x ][ y ] is:

x*4+y
Title: Re:Okay, what am I doing wrong here?
Post by: Fuzzpilz on Mon 17/11/2003 04:21:32

setstats(2,2,getstats(1,3)-Random (3));


This seems to be the problem. Using getstats(2,2) - getstats(1,3) + Random(3) should fix it, I think. As for the chance thing, you don't really need it. Just use the Random(20) directly and leave out the second if.

Also, remove the setstats out of the second block.
Title: Re:Okay, what am I doing wrong here?
Post by: After on Mon 17/11/2003 04:35:51
Your arrays are all over the place, man! ;D

Have to fix them first.

int stats[8];
is correct, and will accept indices 0..7 (8 values).

Your coordinates should be x in {0,1}, y in {0,1,2,3}.

And the conversion should be 4*x+y.
(or x+2*y, just choose one though)

I don't know how 'hitpoints' emerge from your code, so I can't say more, but your arrays will be giving you garbage anyway.
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 04:36:47
Thanks for all the great help everybody!

Quote from: Gilbot V7000a on Mon 17/11/2003 04:18:52

Hmm I didn't read the codes carefully, but if you're making 2*4 arrays, the correct formula to get the index from [ x ][ y ] is:

x*4+y

I get the math, but I don't don't what this is for....I remember you telling me this kinda stuff in my arrays thread, but can't I just access it with the neat little function chris put up? using setstats and getstats?

Fuzz
Yeah, I've removed the second block setstats, and changed to getstats(2,2) - getstats(1,3) + Random(3) for dmg calculations, but it is still wonkey:
What It should be doing is 3+random 3 with a max of 6...but the monster starts with 10 hp and it always seems to take him down to at least 0 in the first hit. Then after it gets to 0 it stays around -3 or -4 no matter how much more dmg is inflicted. Sometimes it almost seems like its adding back hp after it hits below zero....
Title: Re:Okay, what am I doing wrong here?
Post by: After on Mon 17/11/2003 04:56:42
Quote from: Squinky on Mon 17/11/2003 04:36:47I get the math, but I don't don't what this is for.
This may help clarify what's going on.

....x..0..1.
.y....------
.0...|.0..4.
.1...|.1..5.
.2...|.2..6.
.3...|.3..7.

Multiplying means adding whole extra columns, we then add the offset from the start of the column to get the exact position.
Imagine starting somewhere in the left column. Adding 4 moves you to the next one.
Title: Re:Okay, what am I doing wrong here?
Post by: Fuzzpilz on Mon 17/11/2003 05:01:49
If it's supposed to be 3 plus random, you should use getstats(2,2) - getstats(1,3) - Random(3).

Concerning the actual problem, I'm not sure. Could you post the getstats and setstats you're using now? If you're doing it differently in each, that might be it.
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 05:18:34
Quote from: After on Mon 17/11/2003 04:35:51
Your arrays are all over the place, man! ;D

Have to fix them first.

int stats[8];
is correct, and will accept indices 0..7 (8 values).

Your coordinates should be x in {0,1}, y in {0,1,2,3}.

And the conversion should be 4*x+y.
(or x+2*y, just choose one though)

I don't know how 'hitpoints' emerge from your code, so I can't say more, but your arrays will be giving you garbage anyway.

Okay, so it should look like this, right? 0 to 3 for stats[8];

(http://www.2dadventure.com/ags/arraypage.jpg)

Fuzz

I'll try fiddling around with both ways. I think I've reached a point where I can statrt a new test game and only use what works and go from there. I don't know if that makes sense, but the old test game was full of garbage code that I'm afraid might interfere....I'm going to make script this one so I can see the random results and figure out why there doing what there doing...Then if that dosen't work...who knows...
Title: Re:Okay, what am I doing wrong here?
Post by: Gilbert on Mon 17/11/2003 05:21:52
Right
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 05:49:52
Well, at least I got that right now :p

On to the next issue

Okay so I made a new test game where I could check the dmg I was generating, now I simplified it and it should have simply been 3, but no it tells me something like 186778788 or some other riduculous number....

So I made a room with my graph above that I could go to to check values of the array. Heres what I set them to:

//---char attributes----
setstats (0,0,8);
setstats (0,1,10);
setstats (0,2,3);
setstats (0,3,18);
//-----mon attribute--------
setstats (1,0,10);
setstats (1,1,10);
setstats (1,2,2);
setstats (1,3,20);
//-------------------------

Problem is that 0,2 and 1,2 both have crazy big numbers and 0,3 and 1,3 read as 0.....

So am I not setting the array properly somehow and writing values into weird places or am I grabbing weird values from other places?


 
Title: Re:Okay, what am I doing wrong here?
Post by: Fuzzpilz on Mon 17/11/2003 05:55:35
Again, post the getstats and setstats you're using. It certainly looks as though that's where the problem is.
Title: Re:Okay, what am I doing wrong here?
Post by: Gilbert on Mon 17/11/2003 05:58:10
hmmm are you sure you are using the correct formula for the indices in setstat and getstat? Seems that you're grabbing values from outside the array boundary.
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 06:02:59
Okay, heres all the code:

monster code
if (UsedMode("Use")){
 
 
//-----character attack-----------
chance=Random(20); //define this variable first of course
damage=getstats(0,2);
//damage=getstats(0,2)+Random (3);
if(chance>(getstats(0,3)-getstats(2,1)))
{
 Display ("%d",chance);
setstats (1,1,getstats(1,1)-(damage));
Display ("%d DMG",damage);
Display ("You swing and hit");
}
else
{
Display ("%d",chance);
Display ("You swing and miss");
}
//---------------------------------------------
Room code

int chance= Random (20);

//---char attributes----
setstats (0,0,8);
setstats (0,1,10);
setstats (0,2,3);
setstats (0,3,18);
//-----mon attribute--------
setstats (1,0,10);
setstats (1,1,10);
setstats (1,2,2);
setstats (1,3,20);
//-------------------------

 
Global

int stats[8];

int chance;
int damage;

function getstats(int x, int y) {
return stats[y * 5 + x];
}
function setstats(int x, int y, int value) {
stats[y * 5 + x] = value;
}
header
/**/import function getstats(int,int);
/**/import function setstats(int,int,int);

Gilbert
If your talking about those formulas you've posted then no. I don't understand still how to use them, or why I would use them...Where would they apply in my script? And why would some values be correct and others not?

Thanks again for the help, I'm getting confused....
Title: Re:Okay, what am I doing wrong here?
Post by: Gilbert on Mon 17/11/2003 06:10:36
That's the problem:

function getstats(int x, int y) {
return stats[x*4 + y];
}
function setstats(int x, int y, int value) {
stats[x*4 + y] = value;
}
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 06:13:24
Heh, How is that the problem? Are the equations wrong....
/me passes out from confusion

Edit---------------

Holy Crap, I just reread the thread and you guys have been telling me to fix this the whole time! Bah, I'm sorry I didn't get it till now....
so a 2*4 array is x*4+y
and a 2*6 ia x*6+y?

And why did I have to switch from y*4+x??

I hope so, or else I'll have to freak out on all this confusing stuff :P
Title: Re:Okay, what am I doing wrong here?
Post by: Gilbert on Mon 17/11/2003 06:38:12
Right, let's illustrate it as an example:

Say, we want an array of 3*6 elements, so we define it by:
int blah[18]; // 3*6=18
Then, to refer to element [ x ][ y ], we use the formula:
(x * 6 + y) to find it from our array.
Actually the indices are arranged like:
(http://www.2dadventure.com/ags/arrays.gif)

Where the red numbers are the real indices of in the 1D array you created which correspond to each of the elements.

For example, to find element [ 2 ][ 3 ] you can calculate:
2*6+3=15

ie it's blah[ 15 ]
Title: Re:Okay, what am I doing wrong here?
Post by: After on Mon 17/11/2003 06:44:36
Quote from: Squinky on Mon 17/11/2003 05:49:52
Problem is that 0,2 and 1,2 both have crazy big numbers and 0,3 and 1,3 read as 0.....

So am I not setting the array properly somehow and writing values into weird places or am I grabbing weird values from other places?
You have reversed your x and y, somewhere.
If "0,3" means x=0, y=3, then then conversion is 4*x+y.
What you are getting is stats[4*y+x], which is out of bounds for y>1.

It's only coincidence that the others look right, because (0,1) and (1,0) both happen to be 10.

[edit: oops. too slow again. Oh well, I'll add some more then]
Whether we use
numrows*column+row
or
numcols*row+column
(note the pattern)
is a design choice, and either will work. But it affects how entries are ordered within the array so we must be consistent once we have decided.
Title: Re:Okay, what am I doing wrong here?
Post by: Squinky on Mon 17/11/2003 06:54:43
Gilbert
Sweet! That helps me make sense of things a little better....So using that chart could I just use something like:
if (blah[15]<2){
Display ("Holy crap");
}
or do I still need to do it like I have been:
if (getblah(2,3)<2){
Display ("This is the old way man");
}
I can't tell you how helpful you guys have been
After
You may be late but you've given me some insight I didn't have before. So thank you.
Title: Re:Okay, what am I doing wrong here?
Post by: Gilbert on Mon 17/11/2003 06:59:36
If you just use a fix index, you can just use the blah[15] method, but the getstat() functions would make your codes more readible actually, moreover if you use a variable indices, it would be better to use the get/set stats too, eg:

setstat(row,column); when row and column are 2 variables that may have their values changed in your game.