Oh, the joys of converting Starter.FPS to C# (Sample Code)
by Vince Gee · 02/09/2012 (7:16 am) · 29 comments
Hey guys,
I've been busy toiling away at converting the Start.FPS to CSharp. I always try to measure the complexity of what I'm doing by the number of "WTF"'s per minute. Let me tell you I was hitting a WTF per second there for a while, but now, once again things are moving along.
So, if your curious what the cSharp code will look like, here is the Cheetah.cs file converted :)
I've been busy toiling away at converting the Start.FPS to CSharp. I always try to measure the complexity of what I'm doing by the number of "WTF"'s per minute. Let me tell you I was hitting a WTF per second there for a while, but now, once again things are moving along.
So, if your curious what the cSharp code will look like, here is the Cheetah.cs file converted :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WinterLeaf.Classes;
namespace DNT_FPS_Demo_Game_Dll
{
public partial class Main : WinterLeaf.Classes.TorqueScriptTemplate
{
[Torque_Decorations.Torque_Call_Back("", "", "CheetahCar", "onAdd", "(%this, %obj)", false, 2, 2, 2800, false, Torque_Decorations.CallParentType.Begin)]
public string CheetahCar_onAdd(string thisobj, string obj)
{
WheeledVehicle.setWheelTire(obj, 0, "CheetahCarTire");
WheeledVehicle.setWheelTire(obj, 1, "CheetahCarTire");
WheeledVehicle.setWheelTire(obj, 2, "CheetahCarTireRear");
WheeledVehicle.setWheelTire(obj, 3, "CheetahCarTireRear");
// Setup the car with some tires & springs
for (int i = WheeledVehicle.getWheelCount(obj) - 1; i >= 0; i--)
{
WheeledVehicle.setWheelPowered(obj, i, true);
WheeledVehicle.setWheelSpring(obj, i, "CheetahCarSpring");
}
// Steer with the front tires
WheeledVehicle.setWheelSteering(obj, 0, 1);
WheeledVehicle.setWheelSteering(obj, 1, 1);
// Add tail lights
Torque_Class_Helper tc = new Torque_Class_Helper("PointLight", "");
tc.Props.Add("radius", "1");
tc.Props.Add("isEnabled", "0");
tc.Props.Add("color", @"""1 0 0.141176 1""");
tc.Props.Add("brightness", "2");
tc.Props.Add("castShadows", "1");
tc.Props.Add("priority", "1");
tc.Props.Add("animate", "0");
tc.Props.Add("animationPeriod", "1");
tc.Props.Add("animationPhase", "1");
tc.Props.Add("flareScale", "1");
tc.Props.Add("attenuationRatio", @"""0 1 1""");
tc.Props.Add("shadowType", @"""DualParaboloidSinglePass""");
tc.Props.Add("texSize", "512");
tc.Props.Add("overDarkFactor", @"""2000 1000 500 100""");
tc.Props.Add("shadowDistance", "400");
tc.Props.Add("shadowSoftness", "0.15");
tc.Props.Add("numSplits", "1");
tc.Props.Add("logWeight", "0.91");
tc.Props.Add("fadeStartDistance", "0");
tc.Props.Add("lastSplitTerrainOnly", "0");
tc.Props.Add("representedInLightmap", "0");
tc.Props.Add("shadowDarkenColor", @"""0 0 0 -1""");
tc.Props.Add("includeLightmappedGeometryInShadow", "0");
tc.Props.Add("rotation", @"""1 0 0 0 """);
tc.Props.Add("canSave", "1");
tc.Props.Add("canSaveDynamicFields", "1");
tc.Props.Add("splitFadeDistances", @"""10 20 30 40""");
string rightbrakelight = tc.Create(m_ts).ToString();
Con.SetVar(obj + ".rightBrakeLight", rightbrakelight);
string leftbrakelight = tc.Create(m_ts).ToString();
Con.SetVar(obj + ".leftBrakeLight", leftbrakelight);
// Mount a ShapeBaseImageData
ShapeBase.mountImage(obj, "TurretImage", Con.GetVarInt(thisobj + ".turretSlot"), true, "");
// Mount the brake lights
Vehicle.mountObject(obj, rightbrakelight, Con.GetVarInt(thisobj + ".rightBrakeSlot"));
Vehicle.mountObject(obj, leftbrakelight, Con.GetVarInt(thisobj + ".leftBrakeSlot"));
return "";
}
[Torque_Decorations.Torque_Call_Back("", "", "CheetahCar", "onRemove", "(%this, %obj)", false, 2, 2, 2800, false, Torque_Decorations.CallParentType.Begin)]
public string CheetahCar_onRemove(string thisob, string obj)
{
if (Con.isObject(Con.GetVarString(obj + ".rightBrakeLight")))
Con.Eval(obj + ".rightBrakeLight.delete();");
if (Con.isObject(Con.GetVarString(obj + ".leftBrakeLight")))
Con.Eval(obj + ".leftBrakeLight.delete();");
if (Con.isObject(Con.GetVarString(obj + ".turret")))
Con.Eval(obj + ".turret.delete();");
return "";
}
[Torque_Decorations.Torque_Call_Back("", "", "", "serverCmdtoggleBrakeLights", "(%client)", false, 1, 1, 2800, false)]
public string CheetahCar_serverCmdtoggleBrakeLights(string client)
{
string player = Con.GetVarString(client + ".player");
//Remember to pay attention to what type of object your looking at.
string car = ShapeBase.getControlObject(player).ToString();
if (Con.GetClassName(car) == "WheeledVehicle")
{
if (Con.GetVarInt(car + ".rightBrakeLight.isEnabled") == 1)
{
Con.Eval(car + ".rightBrakeLight.setLightEnabled(0);");
Con.Eval(car + ".leftBrakeLight.setLightEnabled(0);");
}
else
{
Con.Eval(car + ".rightBrakeLight.setLightEnabled(1);");
Con.Eval(car + ".leftBrakeLight.setLightEnabled(1);");
}
}
return "";
}
// Callback invoked when an input move trigger state changes when the CheetahCar
// is the control object
[Torque_Decorations.Torque_Call_Back("", "", "CheetahCar", "onTrigger", "(%%this, %obj, %index, %state)", false, 4, 4, 2800, false)]
public string CheetahCar_onTrigger(string thisobj, string obj, string index, string state)
{
// Pass trigger states on to TurretImage (to fire weapon)
switch (int.Parse(index))
{
case 0:
ShapeBase.setImageTrigger(obj, Con.GetVarInt(thisobj + ".turretSlot"), (state == "1" ? true : false));
break;
case 1:
ShapeBase.setImageAltTrigger(obj, Con.GetVarInt(thisobj + ".turretSlot"), (state == "1" ? true : false));
break;
}
return "";
}
[Torque_Decorations.Torque_Call_Back("", "", "CheetahCar", "onMount", "(%this, %obj, %slot)", false, 3, 3, 2800, false)]
public string CheetahCar_onMount(string thisobj, string obj, string slot)
{
// Load the gun
ShapeBase.setImageAmmo(obj, int.Parse(slot), true);
return "";
}
}
}About the author
www.winterleafentertainment.com
#22
02/14/2012 (12:07 am)
Alright, I'm just very curious as to wear the performance savings you experienced are actually coming from.
#24
02/14/2012 (11:36 am)
Vince - sure, that would be helpful. I guess I'm more curious which systems are underperforming in the existing setup, but at the very least that would give me an opportunity to do some profiling.
#25
Shoot me an email at vgee at winterleafentertainment.com, I'll set ya up with the right people.
02/15/2012 (3:30 am)
@Thomas,Shoot me an email at vgee at winterleafentertainment.com, I'll set ya up with the right people.
#26
02/21/2012 (1:09 am)
If I read correctly, I can't wait for a C# version of the engine to hit the market. I'm just saying.
#27
It's not really the engine that is in cSharp, but more of the fact of replacing 99 percent of all torquescript with csharp.
02/21/2012 (2:48 am)
@Philip,It's not really the engine that is in cSharp, but more of the fact of replacing 99 percent of all torquescript with csharp.
#28
02/22/2012 (5:57 am)
What you're doing would probably be more feasible anyways lol. When I first read it though, I was thinking "Torque + Xbox Dev. Club thingy?!"
#29
02/24/2012 (7:05 am)
Running a optimized engine like torque rewritten to csharp would be like trying to build a Lamborghini out of a Yugo parts.... 
Torque Owner Vince Gee
WinterLeaf Entertainment
I'm right now working through the player script, (working backwards through scriptexec) when I plan to get performance ratings after I finish through all the server scripts. I'm already working around the clock on this thing, don't have any spare time to run metrics atm.