Advertisement

using const members in classes

Started by May 06, 2008 03:13 AM
0 comments, last by 39ster 16 years, 6 months ago
As i've mentioned a few times, the objects in my games are code wrapped in an AngelScript class like below:
class MyClass
{
  float x;
  float y;
  float depth;
  
}</pre>

I would like to add &#111;ne more member just under depth called "id" but i &#111;nly want it to be read-only (and use "objectData-&gt;GetPropertyPointer()" to get its pointer and change its value). The value represents the ID of the NPC and can be used in some functions. I tried defining it as "const uint id;" but it woudnt let me use const in a class like that. At the moment i have to use a function called GetId() but i would much rather have "const id".

Heres what an NPC file looks like:

<pre>WIDTH 32
HEIGHT 32

//Tiles for the bush
TILEBLOCK 2 2
  0x0200 0x0300
  0x0201 0x0301
END

CLIENTCODE
int shadowSprite;
int sprite;
int[] tiles;

void &#79;nCreate()
{
  tiles = GetNpcTiles(id);
  shadowSprite = GetSprite("shadow.gif");
  depth = 0xFFFF;
}

void &#79;nDraw()
{
  DrawTiles(tiles, x, y, 2, 2);
}

void DrawShadow(float shadowX, float shadowY)
{
  DrawSprite(shadowSprite, shadowX + GetNpcWidth(id) / 2 - 12, shadowY + 22);
}

void &#79;nCarryDraw(bool drawShadow, float shadowX, float shadowY)
{
  if(drawShadow)
    DrawShadow(shadowX, shadowY);

  DrawTiles(tiles, x, y, 2, 2);
}

bool &#79;nPlayerCollision(int playerId)
{
  //return true to indicate that this will block the player
  return true;
}


bool &#79;nCarry(int playerId)
{
  //return true to indicate that this can be carried by the player
  return true;
}

bool &#79;nWarpCollision(const string& level, float nextX, float nextY)
{
  //If npc touches a warp rectangle than
  //change its level, x and y
  WarpNpc(id, level, nextX, nextY);
  return true;
}
END

SERVERCODE
//server-side equivelant goes here
END
</pre>
Well i figured out a way around it. I register a global called "const uint id" and i set its value just before i call an npc event.

This topic is closed to new replies.

Advertisement