Archive: global variable problem


26th February 2004 04:07 UTC

global variable problem
Ok, I got a large problem here. I am working on a AVS port of the classic Space Invaders arcade game. Now, in the first level, there is a total of 55 enemy sprites on screen. However, each sprite uses 3 global variables: 1 for x possission, 1 for y possission, and 1 for whether it exists or not (IE, whether you have destroyed it or not). Plus, I have a bunch of other global variables (about 20) for various things such as mouse possission, state of the left mouse button, and whether or not your mouse is in the AVS window or not (the game goes into a demo mode if your mouse is not in the AVS window). In total, I would need about 185 global variables.

Now, the enemies are in one giant rectangular group, consisting of 5 rows of 11 sprites. Each row is a different enemy. Would it be possible to do each row as an array? I have no idea how to use the gmegabuf() functions, and yes, I searched the forum, yes I downloaded and looked at the example files, and yes, I still don't understand it.

PS: Hi #ff guys! I got kicked off the internet for 2 weeks (due to going over the bandwidth limit too many times) last Friday, so I should be back next weekend and such. Oh, and I'm using a library computer to type all this.


26th February 2004 18:33 UTC

This should definitely be possible. Just make a multidimensional array with gmegabuf(). You can assign variables to it like this (example):

assign(gmegabuf(5000*c+25*r+v),number)

c=column number (from 1 to 200)
r=row number (from 1 to 200)
v=variable (25 variables are available for each row/column position).

And 'number' would be the value that you are assigning to the variable. Also, you can tweek the constants (5000 and 25) to have different limits for each dimension. Just keep in mind that gmagabuf() can only handle a maximum of 1,000,000 variables (I think).


26th February 2004 18:36 UTC

OOH! :D I think I am beginning to understand this whole array thing! Thanks! Oh, and what is the 5000 for? Also, for v, would I just put a variable name?

Now if I could only figure out how to work the movement for the enemies...


26th February 2004 23:29 UTC

(Dang edit limit, I hate double-posting.)

The problem seems to be that I don't know how to set up a multidimensional array.


27th February 2004 06:45 UTC

here it is in quick. :)

Use one array, but break it up in your head.

instead of XXXXXXXXXXXXXXXXXXX
think of XXXXX XXXXX XXXXX XXXXX

You can index the above example with something like index=row*5+col


27th February 2004 11:54 UTC

BTW, I really can't see why you need such an array !
IMO, the only useful global variables are :
- the status of a sprite : Killed, Stationary, On attack,
- the current position of a line of sprites : each sprite coordinates are computed depending on its state (K: nothing, S: static offset, O: dynamic offset),
- the coordinates of the shots : to know if someone has been hit,
- the status of each wall (if you manage them),
- the mouse coordinates and state (not really, in fact, as they are only useful for your ship ...).

Knowing all the coordinates of all the ships is only meaningful if you want to manage collisions between them ...


27th February 2004 15:31 UTC

well, the way each sprite works is a bit complicated. Don't bother downloading the attachment I posted, cause it's old and I redid the entire sprite system. And since each sprite is animated on beat, I have 2 effects list, and one is enabled and the other is disabled every other beat. Therefore, I need two more variables for the on beat movement so that the movement of the sprite in the first effects list transfers over to the second.

Each sprite uses 5 global variables:

- whether it exists or not (IE, you killed it)
- X posission
- Y posission
- On beat X movement
- On beat Y movement

Now, 55*5=275. IE, I need 275 global variables, and I only have 99 without using the gmegabuf(), which I still have no idea how to work.

And I need to know the cordinates of each sprite so that I can tell if the shot has hit the sprite or not. IE, if the cordinates of the shot equal the cordiantes of the sprite, then said sprite has been hit.


27th February 2004 16:51 UTC

I think you only realy need the position of one and can derive the position of the rest relative to that one. Then you only need keep whether the ship still exists global.


28th February 2004 18:18 UTC

Hmmm, interesting, but how would I keep whether the ship still exists global? If I destroy the ship that all the other ship's movements are based off of, then all the other ships would disapear as well, right?


1st March 2004 12:50 UTC

The fact that the ship still exists or not has nothing to do with its position on the screen. It is always computed but you just don't draw anything if it doesn't exist anymore (using skip=1 or n=0, if you use a superscope).