- Home /
Activate objects/crafting in a game.
So, i was wondering how i should do this. Lets say I have 3 different boxes, one represent a anvil, one a cooking pot, and one a saw. I want to get my toon to do different things when i activate them. I would prefer to walk up to them and press 'F' and it would activate a script on the box i selected. But when i think about it, all the scripts need to have the same name, and if they have the same name they must be the same script right?
So the only solution that comes to mind is to have a long script with alot of if-statements (attatched to my char), but that doesn't seem so effective, and i want NPC's to be able to use them too.
Any suggestions of alternative routes to solve my problem?
Thanks in Advance /Dusinn
Answer by Lab013 · Mar 14, 2011 at 07:53 AM
Either have the scripts inherit from the same superclass with a function (example)
class ScriptA {
function InheritedFunction(value : int) : boolean {
return false;
}
}
class ScriptB extends ScriptA {
function InheritedFunction(value : int) : boolean {
return (value % 2);
}
}
class ScriptC extends ScriptA {
function InheritedFunction(value : int) : boolean {
return true;
}
}
Then you could have a handler script also that would be attached to the object you want to be clicked/have the function called on. It'd look like this
var ascript : ScriptA;
function InheritedFunction(value : int) : boolean { return ascript.inheritedFunction; }
You would then call it like this
print(gameObjectThatYouClicked.GetComponent(Handler).InheritedFunction(someValue));
or you could use the SendMessage function.
Thank you, gonna try this out But does the script that i activate know, if he (the object) is an anvil or a stove? Or should i have a list of possible objects, that translates into a number that i send to the handler script (and then it choose the correct function to call)?
Or is it possible to like: Name the scripts Crafting_001, Crafting_002 etc, and then try to call them by GetComponent("Crafting_*"), or something like that..? (could be buggy if more than one crafting script is attached to the GameObject)