Jump to content
  • Advertisement
Sign in to follow this  
39ster

using const members in classes

This topic is 3817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
  [code here]
}
I would like to add one more member just under depth called "id" but i only want it to be read-only (and use "objectData->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:
WIDTH 32
HEIGHT 32

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

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

void OnCreate()
{
  tiles = GetNpcTiles(id);
  shadowSprite = GetSprite("shadow.gif");
  depth = 0xFFFF;
}

void OnDraw()
{
  DrawTiles(tiles, x, y, 2, 2);
}

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

void OnCarryDraw(bool drawShadow, float shadowX, float shadowY)
{
  if(drawShadow)
    DrawShadow(shadowX, shadowY);

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

bool OnPlayerCollision(int playerId)
{
  //return true to indicate that this will block the player
  return true;
}


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

bool OnWarpCollision(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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!