Script help needed
by Infinitum3D · in Torque Game Engine · 05/29/2007 (3:31 pm) · 29 replies
I've made a file called NPC.cs
I've exec'ed it in game.cs.
In NPC.cs I'm using this script
SpawnNPC(%Name,%Script,%Portrait,%startQuestion,%location) for custom NPC spawning.
I don't understand how to 'define(?)' the parameters %Name, %location, etc.
I've tried
%Name = Joe;
%location = 200 -179 108;
but that gives me a parse error.
How do I define the parameters?
Thanks!
Tony
I've exec'ed it in game.cs.
In NPC.cs I'm using this script
SpawnNPC(%Name,%Script,%Portrait,%startQuestion,%location) for custom NPC spawning.
I don't understand how to 'define(?)' the parameters %Name, %location, etc.
I've tried
%Name = Joe;
%location = 200 -179 108;
but that gives me a parse error.
How do I define the parameters?
Thanks!
Tony
#2
www.geocities.com/dk_uo/
05/31/2007 (5:47 am)
This link may help you out. Click on download. RPGDialog resource for Torque. You can spawn a npc with it.www.geocities.com/dk_uo/
#3
SpawnNPC("Frank", TestDataBlock, 0, 0, "100 200 50");
As long as you have a dataBlock called TestDataBlock, this should spawn a player, name him Frank, and place him at position "100 200 50". Since %Portrait and %startQuestion weren't being used, I used 0 in there place.
I hope I made since.
05/31/2007 (6:28 am)
%Name, %Script, %Portrait, %startQuestion, and %location or things you fill in when you call this function. For an example, Lets say I want an add function.function add(%num1 %num2)
{
echo(%num1 + %num2);
}Then I can call it like this, add(5, 20); and it will echo 25. For your function you can set it up like this:function SpawnNPC(%Name, %Script, %Portrait, %startQuestion, %location)
{
%bot = new AIPlayer()
{
dataBlock = %Script;
};
%bot.setShapeName(%name);
%bot.setTransform(%location);
}Then assuming %Name is what you want to name the AI, %Script is the dataBlock you want him to use, and %location is he should come in at, this is how you would call this function.SpawnNPC("Frank", TestDataBlock, 0, 0, "100 200 50");
As long as you have a dataBlock called TestDataBlock, this should spawn a player, name him Frank, and place him at position "100 200 50". Since %Portrait and %startQuestion weren't being used, I used 0 in there place.
I hope I made since.
#4
Parse error basically means you entered something wrong and the compiler can't read it, or something is there that shouldn't be. Parsing is when the compiler reads/sorts/analyzes lines of code. For instance you have:
%location = 200 -179 108;
when the compiler might be expecting:
%location = "200 -179 108";
This was just an example btw I don't know if that was the error but just to explain parsing.
05/31/2007 (6:35 am)
Tony, Parse error basically means you entered something wrong and the compiler can't read it, or something is there that shouldn't be. Parsing is when the compiler reads/sorts/analyzes lines of code. For instance you have:
%location = 200 -179 108;
when the compiler might be expecting:
%location = "200 -179 108";
This was just an example btw I don't know if that was the error but just to explain parsing.
#5
@Caleb - Thanks! That's perfect!
@mb - Thanks. Basically a syntax error.
Tony
05/31/2007 (4:03 pm)
@Fucifer - Thanks for the reply! I've already got the RPGDialog resource up and running. That's why I need to learn how to Spawn the NPC's in script.@Caleb - Thanks! That's perfect!
@mb - Thanks. Basically a syntax error.
Tony
#6
05/31/2007 (5:54 pm)
That what I have been try to do. Right now I spawn the NPC with a GUI button, that was pretty easy. Your very welcome.
#7
This gives a console error;
Loading compiled script GameOne_Starter.FPS/server/scripts/NPC.cs.
Object 'TestDataBlock' is not a member of the 'GameBaseData' data block class
GameOne_Starter.FPS/server/scripts/NPC.cs (10): Register object failed for object (null) of class AIPlayer.
GameOne_Starter.FPS/server/scripts/NPC.cs (11): Unable to find object: '0' attempting to call function 'setShapeName'
GameOne_Starter.FPS/server/scripts/NPC.cs (12): Unable to find object: '0' attempting to call function 'setTransform'
That means I forgot to code a datablock called TestDataBlock. Can I just copy/paste into NPC.cs the player.cs datablock and rename it?
Nope, now I get the console error-
So now I need help writing the datablock. What comes after datablock TestDataBlock(NPC)?
Thanks!
06/01/2007 (4:48 pm)
So I now have NPC.cs like this;Quote:
//------
//Spawning an NPC - start script
//------
function SpawnNPC(%Name, %Script, %Portrait, %startQuestion, %location)
{
%bot = new AIPlayer()
{
dataBlock = %Script;
};
%bot.setShapeName(%name);
%bot.setTransform(%location);
}
//------
//Spawning an NPC - end script
//------
SpawnNPC("Frank", TestDataBlock, 0, 0, "207 -750 108");
This gives a console error;
Loading compiled script GameOne_Starter.FPS/server/scripts/NPC.cs.
Object 'TestDataBlock' is not a member of the 'GameBaseData' data block class
GameOne_Starter.FPS/server/scripts/NPC.cs (10): Register object failed for object (null) of class AIPlayer.
GameOne_Starter.FPS/server/scripts/NPC.cs (11): Unable to find object: '0' attempting to call function 'setShapeName'
GameOne_Starter.FPS/server/scripts/NPC.cs (12): Unable to find object: '0' attempting to call function 'setTransform'
That means I forgot to code a datablock called TestDataBlock. Can I just copy/paste into NPC.cs the player.cs datablock and rename it?
Quote:
datablock TestDataBlock(NPCdts)
{
baseShape = "./player.dts";
sequence0 = "./player_root.dsq root";
sequence1 = "./player_forward.dsq run";
sequence2 = "./player_back.dsq back";
sequence3 = etc, etc, etc...
};
Nope, now I get the console error-
Quote:
GameOne_Starter.FPS/server/scripts/NPC.cs (35): Unable to instantiate non-conobject class TestDataBlock.
So now I need help writing the datablock. What comes after datablock TestDataBlock(NPC)?
Thanks!
#8
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6566
www.garagegames.com/mg/forums/result.thread.php?qt=32949
www.garagegames.com/mg/forums/result.thread.php?qt=16083
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=12784
06/02/2007 (5:47 am)
Some links I've found useful.www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=6566
www.garagegames.com/mg/forums/result.thread.php?qt=32949
www.garagegames.com/mg/forums/result.thread.php?qt=16083
www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=12784
#9
06/02/2007 (7:25 am)
In the starter.fps/data/shapes/player/player.cs file, you'll see, datablock TSShapeConstructor(PlayerDts). This loads all the animations and is compiled from the starter.fps/server/scripts/player.cs file witch also has this, datablock PlayerData(PlayerBody). for AIPlayers, you would have:datablock TSShapeConstructor(PlayerDts)
{
baseShape = "./player.dts";
sequence0 = "./player_root.dsq root";
sequence1 = "./player_forward.dsq run";
sequence2 = "./player_back.dsq back";
sequence3 = etc, etc, etc...
};
datablock AIPlayerData(NPCDataBlock)
{
shapeFile = "~/data/shapes/player/player.dts";
//etc, ect, ect
};Or you could inherit from the players datablock like this:datablock PlayerData(NPCDataBlock : PlayerBody)
{
//Don't need anything here
};Then you would call your function, function SpawnNPC("Jim", NPCDataBlock, 0, 0, "20 20 0");.
#10
To "call a function", do I just use the line
after the function? Or is there a special way to "call" functions?
Tony
06/03/2007 (5:22 am)
Thanks Caleb! You've been a big help. Just one last question (I hope)...To "call a function", do I just use the line
Quote:SpawnNPC("Jim", NPCDataBlock, 0, 0, "20 20 0");.
after the function? Or is there a special way to "call" functions?
Tony
#11
Calling == Using == Invoking
06/03/2007 (5:53 am)
Calling a function, happens when the function's identifier is followed by the Parenthese () which may or may not contain parameters.Calling == Using == Invoking
//this makes a new function
function myCoolFunction(){
echo("myCoolFunction has just been called");
}
//this calls the function
myCoolFunction();
#12
That is probably the most important/useful information a non-programmer could learn.
Thanks to everyone who has helped!!!!
Tony
06/03/2007 (8:08 am)
Thank you!That is probably the most important/useful information a non-programmer could learn.
Thanks to everyone who has helped!!!!
Tony
#13
Now I'm getting the console error
GameOne_Starter.FPS/server/scripts/NPC.cs (34): preload failed for PlayerDts:.
GameOne_Starter.FPS/server/scripts/NPC.cs (70): Unable to instantiate non-conobject class AIPlayerData.
Object 'NPCDataBlock' is not a member of the 'GameBaseData' data block class
GameOne_Starter.FPS/server/scripts/NPC.cs (83): Register object failed for object (null) of class AIPlayer.
GameOne_Starter.FPS/server/scripts/NPC.cs (84): Unable to find object: '0' attempting to call function 'setShapeName'
GameOne_Starter.FPS/server/scripts/NPC.cs (85): Unable to find object: '0' attempting to call function 'setTransform'
However, my echos do appear in the console
function SpawnNPC has been LOADED
function SpawnNPC has been CALLED
If I try to inherit using this as my NPC.cs
I get the console message Line: 4 - parse error but line 4 is
};.
AND, my echos no longer appear in the console.
I'm sorry, but I still need help!
Thanks!
Tony
06/05/2007 (5:04 am)
This is my current NPC.cs file.Quote:
datablock TSShapeConstructor(PlayerDts)
{
baseShape = "./player.dts";
sequence0 = "./player_root.dsq root";
sequence1 = "./player_forward.dsq run";
sequence2 = "./player_back.dsq back";
sequence3 = "./player_side.dsq side";
sequence4 = "./player_lookde.dsq look";
sequence5 = "./player_head.dsq head";
sequence6 = "./player_fall.dsq fall";
sequence7 = "./player_land.dsq land";
sequence8 = "./player_jump.dsq jump";
sequence9 = "./player_diehead.dsq death1";
sequence10 = "./player_diechest.dsq death2";
sequence11 = "./player_dieback.dsq death3";
sequence12 = "./player_diesidelf.dsq death4";
sequence13 = "./player_diesidert.dsq death5";
sequence14 = "./player_dieleglf.dsq death6";
sequence15 = "./player_dielegrt.dsq death7";
sequence16 = "./player_dieslump.dsq death8";
sequence17 = "./player_dieknees.dsq death9";
sequence18 = "./player_dieforward.dsq death10";
sequence19 = "./player_diespin.dsq death11";
sequence20 = "./player_looksn.dsq looksn";
sequence21 = "./player_lookms.dsq lookms";
sequence22 = "./player_scoutroot.dsq scoutroot";
sequence23 = "./player_headside.dsq headside";
sequence24 = "./player_recoilde.dsq light_recoil";
sequence25 = "./player_sitting.dsq sitting";
sequence26 = "./player_celsalute.dsq celsalute";
sequence27 = "./player_celwave.dsq celwave";
sequence28 = "./player_standjump.dsq standjump";
sequence29 = "./player_looknw.dsq looknw";
};
datablock AIPlayerData(NPCDataBlock)
{
baseShape = "./player.dts";
sequence0 = "./player_root.dsq root";
sequence1 = "./player_forward.dsq run";
sequence2 = "./player_back.dsq back";
sequence3 = "./player_side.dsq side";
sequence4 = "./player_lookde.dsq look";
sequence5 = "./player_head.dsq head";
sequence6 = "./player_fall.dsq fall";
sequence7 = "./player_land.dsq land";
sequence8 = "./player_jump.dsq jump";
sequence9 = "./player_diehead.dsq death1";
sequence10 = "./player_diechest.dsq death2";
sequence11 = "./player_dieback.dsq death3";
sequence12 = "./player_diesidelf.dsq death4";
sequence13 = "./player_diesidert.dsq death5";
sequence14 = "./player_dieleglf.dsq death6";
sequence15 = "./player_dielegrt.dsq death7";
sequence16 = "./player_dieslump.dsq death8";
sequence17 = "./player_dieknees.dsq death9";
sequence18 = "./player_dieforward.dsq death10";
sequence19 = "./player_diespin.dsq death11";
sequence20 = "./player_looksn.dsq looksn";
sequence21 = "./player_lookms.dsq lookms";
sequence22 = "./player_scoutroot.dsq scoutroot";
sequence23 = "./player_headside.dsq headside";
sequence24 = "./player_recoilde.dsq light_recoil";
sequence25 = "./player_sitting.dsq sitting";
sequence26 = "./player_celsalute.dsq celsalute";
sequence27 = "./player_celwave.dsq celwave";
sequence28 = "./player_standjump.dsq standjump";
sequence29 = "./player_looknw.dsq looknw";
};
//------
//Spawning an NPC - start script
//------
function SpawnNPC(%Name, %Script, %Portrait, %startQuestion, %location)
{
%bot = new AIPlayer()
{
dataBlock = %Script;
};
%bot.setShapeName(%name);
%bot.setTransform(%location);
echo ("function SpawnNPC has been LOADED");
}
//------
//Spawning an NPC - end script
//------
SpawnNPC("Frank", NPCDataBlock, 0, 0, "373 375 222");
echo ("function SpawnNPC has been CALLED");
Now I'm getting the console error
GameOne_Starter.FPS/server/scripts/NPC.cs (34): preload failed for PlayerDts:.
GameOne_Starter.FPS/server/scripts/NPC.cs (70): Unable to instantiate non-conobject class AIPlayerData.
Object 'NPCDataBlock' is not a member of the 'GameBaseData' data block class
GameOne_Starter.FPS/server/scripts/NPC.cs (83): Register object failed for object (null) of class AIPlayer.
GameOne_Starter.FPS/server/scripts/NPC.cs (84): Unable to find object: '0' attempting to call function 'setShapeName'
GameOne_Starter.FPS/server/scripts/NPC.cs (85): Unable to find object: '0' attempting to call function 'setTransform'
However, my echos do appear in the console
function SpawnNPC has been LOADED
function SpawnNPC has been CALLED
If I try to inherit using this as my NPC.cs
Quote:
datablock PlayerData(NPCDataBlock : PlayerBody)
{
//Don't need anything here
};
//------
//Spawning an NPC - start script
//------
function SpawnNPC(%Name, %Script, %Portrait, %startQuestion, %location)
{
%bot = new AIPlayer()
{
dataBlock = %Script;
};
// %bot.setShapeName(%name);
// %bot.setTransform(%location);
echo ("function SpawnNPC has been LOADED");
}
//------
//Spawning an NPC - end script
//------
SpawnNPC("Frank", NPCDataBlock, 0, 0, "373 375 222");
echo ("function SpawnNPC has been CALLED");
I get the console message Line: 4 - parse error but line 4 is
};.
AND, my echos no longer appear in the console.
I'm sorry, but I still need help!
Thanks!
Tony
#14
Your datablock doesnt look right to me. That looks like the animation cs file in the shapes folder...not the actual datablock. Are you doing a tutorial/resource or just making something up? Edit: the datablock you wanna look at is in server/scripts/player.cs.
06/05/2007 (6:38 am)
Your function to spawn should look more like?function NPCDataBlock::SpawnNPC(%Name, %Script, %Portrait, %startQuestion, %location)
{
.....
}Your datablock doesnt look right to me. That looks like the animation cs file in the shapes folder...not the actual datablock. Are you doing a tutorial/resource or just making something up? Edit: the datablock you wanna look at is in server/scripts/player.cs.
#15
My new NPC.cs
Now I'm getting the console error
Validation required for shape: GameOne_Starter.FPS/data/shapes/player/player.dts
My echos are there correctly, but no NPC is spawned. Am I required to place a SpawnShere for each NPC? Or can they be spawned by code at any location in the world?
I've also tried it this way
But I get a parse error again.
Any other suggestions?
Thanks!!!
Tony
06/05/2007 (3:52 pm)
OK, I have made a couple changes and I have new results. I'm still not spawning an NPC, but I must be getting closer.My new NPC.cs
Quote:
datablock PlayerData(NPCDataBlock : PlayerBody)
{
//shapeFile = "~/data/shapes/player/player.dts";
category = "NPC";
aiPlayer = true;
};
//------
//Spawning an NPC - start script
//------
function SpawnNPC(%Name, %Script, %Portrait, %startQuestion, %location)
{
%bot = new AIPlayer()
{
dataBlock = %Script;
};
// %bot.setShapeName(%name);
// %bot.setTransform(%location);
echo ("function SpawnNPC has been LOADED");
}
//------
//Spawning an NPC - end script
//------
SpawnNPC("Frank", NPCDataBlock, 0, 0, "373 375 222");
echo ("function SpawnNPC has been CALLED");
Now I'm getting the console error
Validation required for shape: GameOne_Starter.FPS/data/shapes/player/player.dts
My echos are there correctly, but no NPC is spawned. Am I required to place a SpawnShere for each NPC? Or can they be spawned by code at any location in the world?
I've also tried it this way
Quote:
datablock PlayerData(NPCDataBlock : PlayerBody)
{
shapeFile = "~/data/shapes/player/player.dts";
category = "NPC";
aiPlayer = true;
};
datablock StaticShapeData(NPCSpawner)
{
shapeFile = "~/data/shapes/markers/octahedron.dts";
category = "NPCSpawner";
dataBlock = NPCDataBlock;
};
function NPCSpawner::spawn(%name,%spawnPoint)
{
// Create the demo player object
%player = new AiPlayer()
{
dataBlock = NPCDataBlock;
path = "";
name = "Frank";
spawnPoint = "373 375 222";
};
But I get a parse error again.
Any other suggestions?
Thanks!!!
Tony
#16
Try Changing " %bot = new AIPlayer()" to " %bot = new AIPlayer(testBot)" and then in your console while the game is running, type "testBot.getPosition()". If this returns anything at all, your AI is at the location. If it says something like, unable to find object "", then the bot failed to spawn.
Its really weird, everything looks fine.
06/06/2007 (5:47 am)
Hmm, I see "Validation required for shape:" in my console all the time. Maybe this is bad, but its never affected my game before.Try Changing " %bot = new AIPlayer()" to " %bot = new AIPlayer(testBot)" and then in your console while the game is running, type "testBot.getPosition()". If this returns anything at all, your AI is at the location. If it says something like, unable to find object "", then the bot failed to spawn.
Its really weird, everything looks fine.
#17
06/06/2007 (12:21 pm)
Or tryfunction SpawnNPC(%Name, %db, %Portrait, %startQuestion, %pos)
{
%bot = new AIPlayer(%Name)
{
dataBlock = %db;
%bot.setTransform(%pos);
};
}%pos = "373 375 222"; %name = "Frank"; SpawnNPC(%name, NPCDataBlock, 0, 0, %pos);
#18
This works!!!!
This creates an Orc at coordinates 373 375 222, and named Jim.
Since I have the RPGDialog resource working, I can approach Jim, press Q and have a conversation!
Yay!
Thanks again to everyone who helped. I'm still not 100% sure how it works, but it does. And I should be able to place hardcoded NPC's throughout my world now.
Please help with the line-by-line comments explaining why it works. Thanks!
Tony
06/06/2007 (4:21 pm)
Thanks guys!This works!!!!
Quote:
datablock PlayerData(NPCDataBlock : PlayerBody) //--this inherits (copies) the PlayerBody info as NPCDataBlock
{ //--this begins the datablock
category = "NPC"; //--I don't think this is needed
aiPlayer = true; //--I think this uses aiPlayer.cs info somehow
}; //this ends the datablock
function NPC::spawn(%Name, %Script, %Portrait, %startQuestion, %location) //--I don't know what the :: means
{ //--this starts the function
%bot = new NPC() { //--I don't know what this does
datablock = NPCDataBlock; //--this tells the function to use the NPCDataBlock for the NPC that spawns
}; //--this ends the middle part of this function
} //--this ends the function
SpawnNPC("Jim", NPCDataBlock, 0, 0, "373 375 222"); //--this spawns an NPC with the name Jim, using the NPCDataBlock for its script, portrait and start question are not used, location is listed.
This creates an Orc at coordinates 373 375 222, and named Jim.
Since I have the RPGDialog resource working, I can approach Jim, press Q and have a conversation!
Yay!
Thanks again to everyone who helped. I'm still not 100% sure how it works, but it does. And I should be able to place hardcoded NPC's throughout my world now.
Please help with the line-by-line comments explaining why it works. Thanks!
Tony
#19
note: your code makes no sense to me, obviously your using some other code not mentioned here cause I'm confused. Your calling "SpawnNPC" which isn't listed here. And your adding your "spawn" function to the NPC namespace which also is not listed here "NPC::spawn". So in this case your NPC::spawn function is not even being called. Search for "SpawnNPC" and thats the function that is called.
06/07/2007 (8:38 am)
Quote:
datablock PlayerData(NPCDataBlock : PlayerBody)
{
category = "NPC"; // Identifies which category in the Mission Editor objects based on this datablock will show up in
aiPlayer = true; // this datablock is an aiplayer (used to check if this player is a bot or not, since it is it will return true.)
};
function NPC::spawn(%Name, %Script, %Portrait, %startQuestion, %location) // :: is the scope operator and tells what namespace the function belongs to. In this case the 'spawn' function belongs to the 'NPC' namespace.
{
%bot = new NPC() { // creates a new NPC object which is defined elsewhere in your code...
datablock = NPCDataBlock; //--this tells the function to use the NPCDataBlock for the NPC object above
}; //--this ends the 'create a new NPC' function
} //--this ends the NPC::spawn function
SpawnNPC("Jim", NPCDataBlock, 0, 0, "373 375 222"); //--this spawns an NPC with the name Jim, using the NPCDataBlock for its script, portrait and start question are not used, location is listed.
note: your code makes no sense to me, obviously your using some other code not mentioned here cause I'm confused. Your calling "SpawnNPC" which isn't listed here. And your adding your "spawn" function to the NPC namespace which also is not listed here "NPC::spawn". So in this case your NPC::spawn function is not even being called. Search for "SpawnNPC" and thats the function that is called.
#20
OK, let me start again. I guess I thought that I already mentioned that I have the RPGDialog resource installed. It looks like that is where I'm being confusing. I must be pulling something like a datablock or a function from my RPGDialog.cs file.
So, this isn't stand-alone code. It depends on the RPGDialog resource. I apologize. I guess this isn't going to be much help to others.
Thanks again for all the help. It has really been good for me. I still need to learn more about scope operators, namespaces, and inheritance among other things.
Tony
06/07/2007 (2:51 pm)
@mb - Yes, I'm really sorry. OK, let me start again. I guess I thought that I already mentioned that I have the RPGDialog resource installed. It looks like that is where I'm being confusing. I must be pulling something like a datablock or a function from my RPGDialog.cs file.
So, this isn't stand-alone code. It depends on the RPGDialog resource. I apologize. I guess this isn't going to be much help to others.
Thanks again for all the help. It has really been good for me. I still need to learn more about scope operators, namespaces, and inheritance among other things.
Tony
Torque Owner Infinitum3D
Any help. Please!
I found this, but an explanation in simple English would be better tdn.garagegames.com/wiki/TorqueScript_Quick_Reference&needLogin=1
Thanks!
Tony