Jump to content
  • Advertisement
Sign in to follow this  
saejox

Context specific variables

This topic is 2280 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

Hi,

I'm in the progress of exposing Npc ai routines to AngelScript.
Every npc is created/destroyed by the game engine and has its own ai context.

i want to do something like this (AngelScript code)


float lastSeenPlayer = 0;
void BasicMeleeAggressive(Npc @npc)
{
PatrolLeftAndRight(npc);
lastSeenPlayer += TimeSinceLastFrame();

if(npc.IsPlayerInLOS(10))
{
lastSeenPlayer = 0;
}

if(lastSeenPlayer < 5)
{
npc.GoTo(pc.GetXPosition(), pc.GetYPosition());
}

}


problem is, more than one npc object call this script. I have read in manual that lastSeenPlayer is global between contexts.

ofcourse i can add something like this to npc object

unordered_map<string, float> scriptFloats;
float GetFloat(const string &varname)
{
...
}
void SetFloat(const string &varname, float value)
{
...
}


passing a string is not really ideal for performance.

Is there a way to let each context own their global variables?

Thank you

Share this post


Link to post
Share on other sites
Advertisement
No, at least not yet.

The context only holds the stack for a script execution, i.e. local variables inside functions. All global variables are stored in the module, thus shared between all executions of scripts from that module.

To have unique variables for each NPC you can use script classes. Each NPC would then have its own instance of the script class.


class NpcController
{
float lastSeenPlayer = 0;

void BasicMeleeAggressive(Npc @npc)
{
PatrolLeftAndRight(npc);
lastSeenPlayer += TimeSinceLastFrame();

if(npc.IsPlayerInLOS(10))
{
lastSeenPlayer = 0;
}

if(lastSeenPlayer < 5)
{
npc.GoTo(pc.GetXPosition(), pc.GetYPosition());
}
}
}


The game sample in the SDK gives a complete example of how this can be done.

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!