Data structure for simple animations in games - c#

I'm trying to create a system in which numbers are changed according to the time elapsed since last update, their target/destination value, etc.
In an earlier project, I created an abstract class for animation data which contained Tick(), IsDone(), etc, which were then implemented for each facet of the game object I wanted to animate, such as position and opacity. The animation data were held by the game object, with the Tick() function being called by the game object's Tick() function, which is called by the engine's logic update loop using a list of all game objects.
However, I now have more things I want to animate, and was looking in to doing so with as few classes as possible, and definitely not with N+1 classes.
I looked in to saving references to the variable being animated, using the fact that all variables I wanted to animate were floats. However, it appears C# pointers are analogs to the C pointers, and so I cannot save pointers to the values being animated, which appears to be impossible for the CLR in the first place.
The only alternative I could think of is to use reflection to record the argument being passed, then use reflection again to find the value to alter each tick. But then using dozens of reflection calls 60 times a second did not appeal to me.
Since animating objects is a near universal feature in games, I was wondering if there were established practices.

Related

Why don't you need to use GetComponent on Transform?

So I've noticed that in order to use properties of SpriteRenderer, you need to get the component first but the same isn't done for Transform.position. Isn't Transform another component like SpriteRenderer?
Yes, Transform is just another component of the system. The same is about RectTransform, which is used in the unity UI system (canvas).
Several years ago as far as I know, transform property was just syntactic sugar over GetComponent<Transform>() code, as any GameObject has this property and it is accessed quite often. Also, because of the underlying GetComponent call, which is not completely free there was a recommendation for a long time to cache this property if you need to access it often (assign it to a class member and use this member instead of this.transform).
Right now, as far as I know, caching has no sense in most cases, as all components are stored in the array internally, and Transform always has an index 0, so accessing transform is just an access to the first element of the array. Anyway, if someone has really tons of calls to it, it can be useful to profile this, but I don't think it will be an issue in most cases.
But some dinosaurs like me still cache it out of habit :D

Is it possible to get the value of a class attribute from a running application?

There is a simulator. In this simulator we have to pass a corridor. In the corridor there is a door and a puzzle, the solution of which opens this door. As soon as we solve the puzzle, the value of the boolean attribute of the class (something like isOpen) changes to true
This corridor needs to be traversed several times. The corridor itself doesn't change, but the puzzle is random each time.
So, I decided to create a macro application that reaches the puzzle and waits until I solve it
And since the simulation has the boolean variable I need, I was wondering: can I get it, in order to then create a delay in the macro until it is true?
The main problem here is that the two programs are not connected in any way.
I also want to note that I have an understanding that all variables lose their names after compilation, and that variable values subsequently occupy a random place in memory
Also, I have experience with programs like CheatEngine, which is to find the address of a value by its value
But I may just not know all the details, thinking that it's impossible, even if in reality there are ways to do it.
For this reason, I would appreciate it if you could explain to me how this can be done, or, if it is not possible, explain why.
Also, I wouldn't mind a response like "Read this "
I understand that you want to inspect one or more properties of an instance of an object at runtime and this can be achieved by using the so-called Reflection.
The latter provides functionalities that allow you to examine objects at runtime, get their Type, read their properties and invoke their methods. It should be used carefully.
Using reflection you can do
// retrieves the value of the property "NameOfProperty" for the instance of object myobj
bool myFlag = myobj.GetType().GetProperty("NameOfProperty").GetValue(myobj, null);

Architecture philosophy - should objects store information about the world?

I'm making a tetris-like game.
There are two classes in the game: simulator and block (i.e. a tetris block)
Right now, block only holds information about itself that is independent of any other parts of the game. So it's information like "shape", "color", "rotation" etc. (Notably excluding position)
simulator on the other hand, contains an data structure of block's and is responsible for their behaviours (falling, etc.)
Currently, to track the position of each block, simulator actually has an array called array_block_position, which is an array containing sub-arrays in the form of [block-instance, position], e.g. [block#3, (10,12)]
Now, I have this dilemma:
I am tempted to add a "position" variable inside the block class itself instead of tracking their positions in simulator, so simulator can have an array of visible_blocks that is simply an array of block instances rather than an array of arrays. (Visible is emphasized because there may be blocks that are not a part of the playing field, such as the block that is about to be deployed but not yet)
I am uncertain whether I should add "position" as a variable to the block class itself because of some vague memory of learning that this being a bad idea in college, but I no longer remember why it's supposed to be bad, if at all.
I want to know, in general, whether (and when) it's acceptable to store information about an object's relationship with the external world inside the object itself.
(My instincts tells me "no", because a variable like "position" is not meaningful to an object like block without some reference to the external world, but my reasoning sees no immediate problem with something like this in practice)

Where to do the collision detection

this is a question I've had for a while but never bothered asking.
Imagine I have 2 objects. 1 static, and the other movable (by player).
Where would the collision logic exist for that?
To elaborate; in my state I have a list of Components. These can be NPCs, static objects, the player, and just about everything being
private List<Component> _gameComponents;
If I were to do the check in the Player class, surely the player would need to know of the _gameComponents list, and then I'll do the check as I update so I can change the velocity accordingly.
Is there a standard I'm missing?
EDIT 1: I have just read that I should have a step between update and render for checking all collision. I'll give that a try, and see what happens.
Let every component that can be physically processed inherit from a custom interface like ICollidable.
Store every ICollidable in a List<T> that is separate from your components-list. And every ICollidable that is not static in even a third collection.
Have a third class that just processes the whole physics-part of your game and iterates through the non-statics-list. This way you only update what can be moved and you can compare it to every object that is in the list that contains every ICollidable.
Make sure you only check collision for objectes that are within the range of a moving object. It makes no sense to check a collision for an object that won't collide.
I always check for collisions before update, try what works best for you.

Unity 3D - Transforming beetween worlds/planes?

Let's say I want to replicate Planeshifting from Legacy of Kain: Soul Reaver in Unity.
There are 2 realms: Spectral realm and Material realm.
The Spectral realm is based on the Material realm, only has the geometry distorted and certain objects fade out/become non-interactive.
In Soul Reaver, it is used as means to go to areas where you normally wouldn't be able to in Material (distorting geometry), to use other powers (such as going through grates).
My question is: Is it even possible at all to implement this in Unity 3D? (I would need the Scene(level) or the objects to have 2 states somehow that I could switch beetween/distort to real-time.)
I would call this a rather advanced topic and there are multiple ways of accomplishing a at least similar effect.
But to answer your actual question straight away - Yes it is possible.
And here are some approaches i would take (i guess that would be your next question ;))
The easiest way is obviously having game object which have their collider and renderer disabled (or the whole object) when "changing
realms". But this for sure isn't the best-looking way of doing it,
even tho a lot of motion blur or other image effect could help.
(Depending on what shaders you use, animating the alpha value can
create a fading effect as well)
The more advanced way would be the actual manipulation of vertices (changing the object). There are quite a few tutorials on
how to change the geometry of object. Take a look at Mesh() in the
official documentation:
http://docs.unity3d.com/ScriptReference/Mesh.html
A class that allows creating or modifying meshes from scripts.
Another way (didn't try) thats rather easy would be using shape keys. I don't know which Software you use to create your
world/models but blender has this function which allows you to define
a base shape, then edit the verticies in blender and save it as a
second (or more) shapes. Unity can blend smoothly between those
shapes as being shown in this video:
https://www.youtube.com/watch?v=6vvNV1VeXhk
Yes it will be possible in Unity3D, but your question is quite general. You could try something like having 2 models per GameObject (perhaps as children or fields on the script) and disabling 1 of them depending on which realm the player is in. You could have 2 scenes for each level and switch between them, though that might be too slow. You could see if there are any plugins/assets that allow you to define 2 models and morph between them. There are probably a number of other routes you could take, but I can't really help more until you've chosen a path.

Categories

Resources