Game Development Community

Making Level 2

by Shiraz · in Torque X 2D · 04/02/2007 (12:10 pm) · 12 replies

I've created the first level of my game and for level 2 all I want to do is change the tilemap. When I do a load the new tilemap gets placed on top of the old one and it's a big mess. I don't want to have to unload the first level because I'm using all the sprites still. So what do I do?

#1
04/02/2007 (2:20 pm)
You can set the original sprites from level 1 to persistant, and then unload the level and load the new level, which will persist all the sprites, but not the level itself (IIRC).

www.linkedin.com/img/webpromo/btn_viewmy_160x25.gif

www.mmogamedev.info/images/imgdc_ad1.gif
#2
04/02/2007 (2:34 pm)
Oh THAT'S what persistent is for! (Hmmm, kinda self-explanatory now that I think about it. Doh.) Thanks!
#3
04/02/2007 (2:47 pm)
This looks kind of relevent. I ran into a similar problem, heres how I solved it...

Basically, the new object steals the scene camera, and returns it when it goes away. All you have to do is clone the the object, and delete it when it leaves make sure its in a different part of the map then any of your other stuff.

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 StarterGame
{
    [TorqueXmlSchemaType]
    public class GetAndReturnCameraComponent : 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; }
        }

        #endregion

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

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

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

        public override void CopyTo(TorqueComponent obj)
        {
            base.CopyTo(obj);
        }

        #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
            T2DSceneCamera _myCamera = (T2DSceneCamera)TorqueObjectDatabase.Instance.FindObject("yourCamera");
            _myCamera.Mount(SceneObject, String.Empty, true);
            // todo: look up interfaces exposed by other components
            // E.g., 
            // _theirInterface = Owner.Components.GetInterface<ValueInterface<float>>("float", "their interface name");            

            return true;
        }

        protected override void _OnUnregister()
        {
            // todo: perform de-initialization for the component
            T2DSceneObject _oldMount = (T2DSceneObject)TorqueObjectDatabase.Instance.FindObject("OldObject");
            T2DSceneCamera _myCamera = (T2DSceneCamera)TorqueObjectDatabase.Instance.FindObject("yourCamera");
            _myCamera.Mount(_oldMount, String.Empty, true);
            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);
        }


        #endregion

        //======================================================
        #region Private, protected, internal fields
        #endregion
    }
}
#4
04/02/2007 (6:48 pm)
K, so I made the objects on level 1 persistent and then tried to compile and got this message

TorqueObject.CopyTo(TorqueObject obj) needs the following lines of code:

TorqueObject obj2 = (TorqueObject)obj;
obj2.IsPersistent = IsPersistent;

when I execute the following:

_playerNorth = (T2DAnimatedSprite)(TorqueObjectDatabase.Instance.FindObject("playerNorth") as T2DAnimatedSprite).Clone();

Why can't it ever be simple?
#5
04/03/2007 (12:03 am)
K, I've done a workaround. I load a level with the scene data then I load a level with the tilemap then when I'm ready I UnloadLastScene and load a new one. The only problem is I have to recreate any clones I created with each new level load. Is there a way to make a persistent clone?
#6
04/16/2007 (11:21 am)
Funny how these things weird and obscure until it hits you in the face :) I got the same problem by moving all my templates to a separate .txscene (and making them persistent) from the actual level.

Apparently cloning a persistent object gets set non-persistent (probably by desing). But this causes an exception in the cloning because the .IsPersistent-properties of the clone and original are different. You can work around this by setting persistancy off before cloning like this:

template.IsPersistent = false;
T2DSceneObject clone = template.Clone() as T2DSceneObject;
template.IsPersistent = true;

It's not very pretty, but I guess solves the thing..
#7
04/16/2007 (12:33 pm)
That makes the clones persistent? Thanks! I'll try it out. It's a lot prettier than the workaround code I've had to write.
#8
04/16/2007 (12:39 pm)
I don't see how that would make the clone persistant as you aren't turning on persistance on the clone at all.

Why wouldn't you simply leave persistance on and when you clone turn the persistance on for the clone?

template.IsPersistent = true;
T2DSceneObject clone = template.Clone() as T2DSceneObject;
clone.IsPersistent = true;


www.linkedin.com/img/webpromo/btn_viewmy_160x25.gif
#9
04/17/2007 (1:20 am)
Ok, maybe I wasn't very clear about my assumptions:

- I have a bunch of templates in one .txscene that I want to be both .IsPersistent and .IsTemplate, i.e. to be available regardless of which map is loaded.

- From these I clone my game objects that I don't want to be either .IsPersistent (game objects would get scrapped and re-created if a new level is loaded, which differs from Shiraz's case) or .IsTemplate (obviously).

- If I don't have the two lines manipulating .IsPersistent, the template.Clone() will cause an exception. This is because (my guess) .Clone() is designed to switch .IsPersistent always false, but TorqueObject.Clone() has an assert that always "clone.Property == template.Property" (people with source could check).

- I don't think Jonathon's suggestion would work, because after cloning .IsPersistent will always be false regardless of template property. Otherwise there wouldn't have been a problem in the first place (both template and clone would have ".IsPersistent == true").

So in a way, TX behaves as I would like it to do, but has an internal conflict with checking that properties get cloned correctly.

Matias
#10
04/17/2007 (4:11 am)
If you have them in a seperate file why do you need to label them as persistant in the first place? Couldnt you just never Unload that file?
#11
04/17/2007 (10:26 am)
After reading my own post once, I see what you mean. I thought I would, but I guess I don't, better try :)
#12
04/18/2007 (1:13 am)
I tried making the clones persistent but they still get unloaded with the current level. :-( Bug?