Could someone give me an insight into the "*" pointers/variables e.g. DynamicSpr

Started by mode7, Mon 07/03/2011 14:33:37

Previous topic - Next topic

mode7

I think I can handle AGS coding already pretty well. But sometimes I miss the understanding for basic concept.
Especially these greenish varibvalbe Types like *DynamicSprite *Character *ViewFrame...... I don't even know what they are called. Normally I get along with them pretty well, but I don't really understand them. They seem to have a bunch of variables stored in there like a struct but I can never quite figure out how they work.
Maybe someone has a link explaining.

Well I have a concrete problem relating to this too:
Take this for example:
Code: ags

//Abbreviated code:
DynamicSprite *Dest;
function DrawParticles(int SystemID){
Dest = DynamicSprite.Create(800, 600, true);
DrawAlpha(Dest.Graphic, particle[i].sprite, FloatToInt(particle[i].xpos, eRoundNearest), FloatToInt(particle[i].ypos, eRoundNearest), 0);

Ok here's what i wanna do: "Dest" is pointing to the dynamic sprite. As I'd like the function to manage several dynamic sprites. But "Dest" is only pointing to one dynamic sprite. How to genrealize this? If this was an Array, I'd just do this:
Code: ags

DrawAlpha(dynamicsprite[SystemID].Graphic,....
But since I don't know anything about these kind of variables I dont know how to pull it off.

Khris

The only thing that's missing in your example is the dynamic sprite array declaration.

Code: ags
DynamicSprite* Dest[10];


This creates ten objects (in the sense of object-oriented programming, not AGS objects) in memory of the type DynamicSprite; the ten pointers Dest[0] to Dest[9] point to these objects.

Every single one has all the variables associated with a DynamicSprite, e.g. .Width, .Graphic, etc.
It's exactly like a struct and works the same way.

In order to access the width of the third DynamicSprite, use Dest[2].Width.

As for another example, say the player can use a lot of inventory items on themselves:

Code: ags
  InventoryItem*ai = player.ActiveInventory;


I have just declared a pointer of the type InventoryItem which now points to the used InventoryItem. (Just like an integer variable holds an integer and a String variable holds a string of text. The difference is that at the end of the function, ai is destroyed, but the object it pointed at won't be.)

So now I can conveniently use e.g.
Code: ags
  if (ai == iRootBeer || ai == iBeer || ai == iPotion) {
    player.LoseInventory(ai);
    Display(String.Format("You gulp down the %s.", ai.Name));
  }

mode7

Thanks Khris that really helps!!!
Also the explanation was useful, sometime I'll have to look at some real programming languages to get a basic understanding of e.g. object oriented coding.

monkey0506

I meant to post this the other day, but there is also an article in the wiki that I wrote to try and help people understand pointers both in general, and specifically within AGS.

Calin Leafshade


monkey0506

Just FYI Calin, yes the int type in AGS is in fact 32-bit (4 byte (8 bits per byte of course)). As is the float type and also pointer types (every pointer you declare consumes 4 bytes of memory (I think only if it is non-null though)) (this includes the String type which internally uses a pointer, but also consumes one additional byte of memory per character stored).

SMF spam blocked by CleanTalk