Sign in to follow this  

Context specific variables

This topic is 2062 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)
[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());
}

}
[/CODE]

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
[CODE]
unordered_map<string, float> scriptFloats;
float GetFloat(const string &varname)
{
...
}
void SetFloat(const string &varname, float value)
{
...
}
[/CODE]

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
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.

[code]
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());
}
}
}
[/code]

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

This topic is 2062 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this