Need help with program design for mounting objects in a linked fashion
by Randy Lutcavich · in Torque X 2D · 09/01/2009 (4:24 pm) · 4 replies
Do you remember the old cell phone game Snake? You controlled the direction of a constantly moving dot and collected more dots that chained on to the end of the previously collected dot. Eventually you had a large snake like chain of dots that you had to maneuver in a way that avoided contacting one end of the chain with the other.
I have a similar element in my game that I need to program. I want to control a yellow dot that collects blue, green, and red dots.
Currently, when the yellow dot collides with any color dot, it is added to the end of the yellow dot. This is perfect for the very first collected dot but any dot after that will still mount to the yellow dot instead of the last dot in the chain.
My question is how can I mount the collided object to the last dot in the chain? I want it so that the yellow dot is the one that always triggers the collision but when I do this, the variable for theirObject in the OnCollision method will always link to the yellow dot. I need a variable for the last collected dot but my C# is pretty bad so I am having trouble just declaring a variable and then assigning it to the last collected dot and then calling it again later on the next collision. What's the best way to do this in code?
I have a similar element in my game that I need to program. I want to control a yellow dot that collects blue, green, and red dots.
Currently, when the yellow dot collides with any color dot, it is added to the end of the yellow dot. This is perfect for the very first collected dot but any dot after that will still mount to the yellow dot instead of the last dot in the chain.
My question is how can I mount the collided object to the last dot in the chain? I want it so that the yellow dot is the one that always triggers the collision but when I do this, the variable for theirObject in the OnCollision method will always link to the yellow dot. I need a variable for the last collected dot but my C# is pretty bad so I am having trouble just declaring a variable and then assigning it to the last collected dot and then calling it again later on the next collision. What's the best way to do this in code?
About the author
Recent Threads
#2
Each collision, mount the new dot on the object referenced by this "tail" property, then update the tail to refer to the new object.
To remove, you could either put a reference on each object to the previous one so that the tail property could be backed up, but I believe there is already a built-in way to access the object something is currently mounted on.
I think this ought to do the trick.
09/01/2009 (7:19 pm)
One thing that could be pretty simple is to keep a reference to a dot object in the yellow "head" dot, perhaps called "tail". It will represent the last dot on the chain; initially, it will point to the head.Each collision, mount the new dot on the object referenced by this "tail" property, then update the tail to refer to the new object.
To remove, you could either put a reference on each object to the previous one so that the tail property could be backed up, but I believe there is already a built-in way to access the object something is currently mounted on.
I think this ought to do the trick.
#3
If anything, I think the updating of mounted objects might be done recursively, in which case there is a limit by the language or processor for how deep recursion can go.
09/01/2009 (7:22 pm)
Now that I think about it, I'm wondering if there might a limit to how many things can be mounted in a daisy chain like this, but i really don't know.If anything, I think the updating of mounted objects might be done recursively, in which case there is a limit by the language or processor for how deep recursion can go.
#4
09/01/2009 (11:15 pm)
Is there any special way you recommend I reference the "head" and "tail"? I believe I am already attempting this with the code I posted above with my reference to the "last" dot. But it just doesn't seem to work that way.
Torque Owner Randy Lutcavich
if (theirObject.Name == "greenDot") { theirObject.CollisionsEnabled = false; theirObject.Mount(ourObject, "LinkPoint1", true); Game.Instance._greenDot.Name = "last"; } if(theirObject.Name == "redDot") { theirObject.CollisionsEnabled = false; Game.Instance._greenDot.CollisionsEnabled = true; theirObject.Mount(TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("greenDot"), "LinkPoint1", true); }It doesn't recognize collision with the redDot after the collision with the greenDot though.
This isn't how I want it to be in the end though cause I want the color dots to be random. But if I could get this working I would be better off.