is there a this pointer??

Started by Rocco, Sun 05/02/2006 20:54:08

Previous topic - Next topic

Rocco

i have a memeber function in a struct, and when i wright it out:

Code: ags

static function Player_Settings::SetControls(int left,  int right,  int up,  int down,  int shooting)
{
  this.left = left;
  this.right = right;
  .......
}


i noticed that <this.> is not available, is there a equivalent to achieve this??

Pumaman

static functions do not have a "this" pointer, since there is no instance to reference.

monkey0506

It's the basic idea of a static function.  If the function always exists then there is no instance of the struct to access data members of.  If you want to access the data then there has to be an instance of the struct in existance.  Just get rid of that static keyword.

fovmester

First:
A "this"-pointer would reference to an object of the struct. Since your function is static that function wouldn't be able to reference a particular object by "this".

Second:
To my knowledge it is (so far) not possible to create new objects of a struct in AGS. Therefore a non-static function in a struct would have no meaning since you won't be able to call it.

My recommendation is therefore that you have two global variables that you change instead from this static function.

And by the way, CJ: A new-command would have been tremendous! But probably hell for you since it would involve dynamic memory handling and all that crap.

SSH

Quote from: fovmester on Mon 06/02/2006 10:46:57
To my knowledge it is (so far) not possible to create new objects of a struct in AGS. Therefore a non-static function in a struct would have no meaning since you won't be able to call it.

You can declare variables that are structs and arrays of them. However, you can't do so dynamically. And of course you can call non-static functions in structs.

Code: ags

struct a { int b; import function setb(int b); };
a b;
b.setb(4);
12

Rocco

ok, thx
is it possible to declare a static variable in structs?
cause i need values only once for an array of structs.

example:
Code: ags

struct Enemies{
	
Ã,  // Sprites
Ã,  Overlay* enemy_graphics;
Ã,  int enemy_sprites;
Ã,  int width;Ã,  					// enemies sprite proberties
Ã,  int height;
..
..
 stactic int all_enemies;
 static int enemy_type;
 static int amount_of_enemies_per_type;
	
};


if not, i guess i need another struct, where i store values who
dont change for the complete game


monkey0506

Workaround - Make them global.  It gets the job done...and if you name them something like Enemies_all_enemies then it would work essentially the same only with an underscore ('_') in place of the period ('.').

SSH

Also, rather than having an array of structs each with an overlay, etc, why not have:

Code: ags

struct Enemies{

  // Sprites
  Overlay* enemy_graphics[MAX_ENEMIES];
  int enemy_sprites[MAX_ENEMIES];
  int width[MAX_ENEMIES];  // enemies sprite proberties
  int height[MAX_ENEMIES];
..
..
 int all_enemies;
 int enemy_type;
 int amount_of_enemies_per_type;

};

12

Rocco

cause for me it feels more "natural" and makes things easier.

assuming i have in the struct, something like:
Ã,  Overlay* shoot_graphic[MAX_SHOOTS];
which i have in the player struct, then it became more complicated,
cause then i need
Overlay* shoot_graphic[MAX_ENEMIES*MAX_SHOOTS]; for example

there allways a lot of possible ways.
and when the module ist published, i hope that
we discuss the downsides of it (and there are a lot thats for sure)
to improve it.

SSH

Well, no, because you can only have 20 overlays at once anyway...
12

Rocco

well, this is the biggest problem, so i hope for future increase of this limit.
unfortunatly it was to late to stop my work, as i recognized this limit.

Khris

The only way to display more than, say, 50 objects at once is to do it by hand.

pseudo code:
repeatedly_execute() {
Ã,  calculate positions and appropriate sprite slots;
Ã,  draw background; (get rid of drawn objects)
Ã,  draw objects;
}

The order is important; as the calculations take up cpu time, they should be processed right after everything is drawn in place and before the screen is redrawn to avoid flickering.

It shouldn't be that hard to rewrite your overlay-routines to use DynamicSprites instead.

Rocco

thanks, good idea.
ill look into it, after finishing the inital module version.

SMF spam blocked by CleanTalk