Game Development Community

Microbes / RepellerComponent.cs Error

by Jason Walters · in Torque X 2D · 12/16/2008 (7:49 pm) · 3 replies

After I complete the 'RepellerComponent.cs' file, I receive the below error on build. Would someone please point out why I'm receiving this error?

Error
Quote:Cannot implicitly convert type 'GarageGames.Torque.Core.TorqueBase' to 'GarageGames.Torque.T2D.T2DSceneGraph'. An explicit conversion exists (are you missing a cast?)

RepellerComponent.cs
Quote:using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using GarageGames.Torque.Core;
using GarageGames.Torque.Util;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T2D;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.MathUtil;

namespace StarterGame2D
{
[TorqueXmlSchemaType]
public class RepellerComponent : TorqueComponent, ITickObject
{
//======================================================
#region Static methods, fields, constructors
#endregion

//======================================================
#region Constructors
#endregion

//======================================================
#region Public properties, operators, constants, and enums

public T2DSceneObject SceneObject
{
get { return Owner as T2DSceneObject; }
}

[TorqueXmlSchemaType(DefaultValue = "6")]
public float Radius
{
get { return _radius; }
set { _radius = value; }
}

[TorqueXmlSchemaType(DefaultValue = "10")]
public float Strength
{
get { return _strength; }
set { _strength = value; }
}

#endregion

//======================================================
#region Public methods

public virtual void ProcessTick(Move move, float dt)
{
// todo: perform processing for component here

// Keep track of which objects were nearby last tick.
_oldNearbyObjects.Clear();
_oldNearbyObjects.AddRange(_nearbyObjects);

// Use the FindObjects method to get all objects in the scenegraph in a certain radius.
_nearbyObjects.Clear();
T2DSceneGraph mySceneGraph = TorqueObjectDatabase.Instance.FindObject("DefaultSceneGraph");
mySceneGraph.FindObjects(SceneObject.Position, _radius, TorqueObjectType.AllObjects, (uint)0xFFFFFFFF, _nearbyObjects);

Thank you.

#1
12/16/2008 (7:50 pm)
Here's the rest of the code:
Quote:
// Now tell the objects that are within our repulsion radius that we're repelling them.
foreach (ISceneContainerObject nearbyObject in _nearbyObjects)
{
// can't repel yourself!
if (nearbyObject == Owner)
{
continue;
}
if (nearbyObject is T2DSceneObject)
{
T2DSceneObject nObj = nearbyObject as T2DSceneObject;
MicrobeAIComponent objAI = nObj.Components.FindComponent();
if (objAI != null)
{
// tell the object who we are, and how hard we're pushing.
objAI.AddPush(this, _strength);
}
}
}

// Remember how we kept track of which objects we were pushing last tick?
// Well, if we're not going to push them anymore, we need to tell them.
_noLongerPushingObjects = _oldNearbyObjects.FindAll(NotInNewList);

foreach (ISceneContainerObject oldObject in _noLongerPushingObjects)
{
if (oldObject is T2DSceneObject)
{
T2DSceneObject oObj = oldObject as T2DSceneObject;
MicrobeAIComponent objAI = oObj.Components.FindComponent();
if (objAI != null)
{
// it's an objects we can affect...
objAI.RemovePush(this);
}
}
}

}

public virtual void InterpolateTick(float k)
{
// todo: interpolate between ticks as needed here
}

public override void CopyTo(TorqueComponent obj)
{
base.CopyTo(obj);
RepellerComponent obj2 = (RepellerComponent)obj;

obj2.Strength = Strength;
obj2.Radius = Radius;
}

#endregion

//======================================================
#region Private, protected, internal methods

protected override bool _OnRegister(TorqueObject owner)
{
if (!base._OnRegister(owner) || !(owner is T2DSceneObject))
return false;

// todo: perform initialization for the component

// todo: look up interfaces exposed by other components
// E.g.,
// _theirInterface = Owner.Components.GetInterface>("float", "their interface name");

// tell the process list to notifiy us with ProcessTick and InterpolateTick events
ProcessList.Instance.AddTickCallback(Owner, this);

return true;
}

protected override void _OnUnregister()
{
// todo: perform de-initialization for the component

base._OnUnregister();
}

protected override void _RegisterInterfaces(TorqueObject owner)
{
base._RegisterInterfaces(owner);

// todo: register interfaces to be accessed by other components
// E.g.,
// Owner.RegisterCachedInterface("float", "interface name", this, _ourInterface);
}

private bool NotInNewList(ISceneContainerObject obj)
{
return !_nearbyObjects.Contains(obj);
}

#endregion

//======================================================
#region Private, protected, internal fields
protected float _radius;
protected float _strength;
protected List _nearbyObjects = new List();
protected List _oldNearbyObjects = new List();
protected List _noLongerPushingObjects;
#endregion
}
}
#2
12/17/2008 (5:50 am)
I havent seen that project, but it is saying basically that there is a casting issue. I would change this:

T2DSceneGraph mySceneGraph = TorqueObjectDatabase.Instance.FindObject("DefaultSceneGraph");
to

T2DSceneGraph mySceneGraph = TorqueObjectDatabase.Instance.FindObject<T2DSceneGraph>("DefaultSceneGraph");
#3
12/17/2008 (6:40 am)
Thanks David that did the trick!!