Game Development Community

Dynamic NPC Script attachment ?

by Areal Person · in Torque Game Engine · 01/03/2009 (6:48 pm) · 4 replies

Is it possiable to attach a script to an NPC BOT (Kork etc.)
at runtime ?

Lets say I spawn 2 or 3 NPC's at startup at specific spawn points.

Is it possiable to then pull down the console and attach or run
a script that has been designed specificly for the NPC that I choose ?

And what about a generalized script attachment that would have common features
that would be approprate for any of the NPC's ? How would I attach that kind of script ?


What are some thoughts on how I might do this ?

I'm trying to get a better handle on dynamically controlling NPC's


Thanks,

#1
01/03/2009 (7:13 pm)
You don't attach scripts as such, but what you can do is have within a script different versions of a function that the NPC can use according to some criteria. For example, I used to use namespaces in the scripts to do this like:

Goat::attack()

or 

Bot::attack()

And then in the datablock assigned to the NPC, there would be a field like "MOBType" or something that would say:

MOBType = "Goat";

And in the generic AIPlayer scripts, I had a common AI framework that called functions similar to:

%function = %this.getdatablock().MOBType @ "::attack();";
eval(%function);

That's not everything, but if you think about it, you can have the AIPlayer namespace be just a wrapper for calling specific functions in script. You can put that functionality in AIPlayer::attack() and have that do the query for what attack function to call. This way, the script functions you want to use get attached at spawntime, or whenever you set that MOBType-like variable.
#2
01/03/2009 (7:48 pm)
Thats kool !

That will really help with what I'm trying to do !

Any other suggestions anyone ?

Thanks much,
#3
01/03/2009 (7:54 pm)
As a matter of fact, I do have a suggestion: Get friendly with eval() and constructing commands in script like you would any other text statement. You'll find a lot of flexibility in being able to cobble together dynamic functions, objects, GUI's, etc and using eval(%yourthinggoeshere) to execute it on the fly...
#4
01/03/2009 (8:11 pm)
Yep, thats real gold nugget stuff !

It will really help me ! I've had trouble getting a handle on
these type issues.

Thanks !