I'm building a Unity game where you have feelers, and by clicking the color given at that moment, you can get or lose a score, time or you get a game over screen.
My problem is - I have a lot gameObjects and each one calculates scores alone, (eache one have his own score int) and I need one code detects all scores number one, so I can see the scores one point (about how to see it, this is less important).
The name of the number that I want to find is called (how creative): score.
And yes, this is a public float.
I am most glad if I could write this document duration (unless you have no choice anyway ...)
sorry about any grammar mistakes, I do my best.
Thanks from advance
Roei Meiri.
:)
I can't image this would be very efficient, you would probably get better performance if you keep you own list of all the scoring objects(add to the list when creating the scoring object).
That being said...(uncompiled example).
Assuming you want to find of all objects with a particular component, like
class ScoringObject:Monobehavior
then you can find them all and add up the scores like so...
float totalscore=0;
ScoringObject[] listOfScores= Object.FindObjectsOfType(typeof(ScoringObject)) as ScoringObject[];
foreach (ScoringObject scorObject in listOfScores) {
totalscore+=listOfScores.score;}
http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
Related
I am a beginner in Unity and I'm trying to make something pretty simple. However, I don't know where to start. I have a number of 'knots' in the scene, that the player will be able to select. They will then be able to connect two knots. I managed to find all knots in a range of 40 from the player's position and put them in an array. The next step would be to 'navigate' through the knots. I want the player to use the left analogue stick for that, but I don't know how to go about it. When they hold the stick at 120°, the knot closest to that position would be able to be selected by pressing another button. I looked at some UI navigation scripts, but this situation is pretty different as it's a two dimensional space. Does anyone have any suggestion on how to go about this? Thank you.
screenshot
It sounds to me like you're going to have to do some trigonometry to be able to find the nearest "knot." Each of these--at least if you're programming well--should each be an object whose x and y location variables can be accessed from your script.
You could iterate through the list of collected objects and compare them to your current location and store the object in a temporary variable if it is a shorter distance than the last object in the array.
Here's some pseudocode that should help illustrate my point:
for item in array {
Object closestKnotDist = Null
Float currentKnotDist = math.sqrt((player.x-item.x)**2 + (player.y-item.y)**2)
if currentKnotDist < closestKnotDist {
closestKnotDist = currentKnotDist
}
}
Thus, closestKnotDist will be the closest object to the player. Manipulate it how you will.
Not sure if this answered your question, but feel free to add some more explanation if not!
My question is not so much code-oriented, it's more theoretical.
I'm currently working on an application for a sporting event. The goal is to be able to track competitors on a map while they are moving along a predetermined route.
Currently I have already been able to map the route and I'm able to place markers on the different locations using GMap.NET.
However I have two big challenges that I don't know how to tackle.
1 Calculating the distance and the (estimated) time until the competitors reach the finish.
So for every competitor that has a tracker with them, I would like to map him/her on the map and calculate the distance to the finish. In theory that should be easy. Every competitor will always be in between two waypoints and when I get the position of the tracker I could calculate the distance to the next waypoint and from there on I add every distance between the next waypoints and I have the total remaining distance to the finish.
But that's just theory, I have no clue how I could implement this.
Is there a way to know in between which two waypoints the competitor is?
And what should I do if, for example, there is a part of the route where the competitor goes up to a turningpoint at the end of the road and then comes back on the same road but just on the other side. How would I know if the runner is going up to the turningpoint or if he/she is on the way back from the turningpoint?
2 Working with loops inside the route
This is an even more complicated task. In the route there are two sections that the competitors have to do twice. They are large loops, not small ones. In order to get a correct calculation of the distance, I would need to find a way to know if the competitors are in the first loop or the second.
I was thinking I could probably use a quite similar approach as the issue above, i.e. to specify two waypoints in which I register the time that they have passed there.
If they pass again I could compare that time with the saved time and if there is enough time in between, I could conclude that they are in the second loop of that section.
But again, that's theory. The question is how would I do this in reality? And how do I calculate the distance properly? Do I specify two indexes of waypoints where I calculate the distance in between those indexes twice?
I would love to hear some of your insights on this.
Thanks,
I'm making a 2D top down survival game, maximum sprite count for the top level is 100 sprites.
When I use a random to generate their vector positions occasionally i'll get some overlap between sprites.
So to combat this i'm going to store some pre-defined positions.
The question
So my question is what would be the best way of storing these. Initially i was going to store them in an array, however i'm thinking that storing them in a text file and reading them in at the start of the game would be a better solution.
I'm a beginner so if anyone could give any advice on this it would be much appreciated.
Many thanks
Yes store them in a CSV (comma separated values) text file, or if you want, use a database, although I would recommend the former. Database storage sounds like overkill in your situation. On start-up you would load the values into an array. If you don't the game will lag every time it gets a value. You just need the text file for persistent storage and then the array for usage.
Hope this clears your issue up for you!
Why don't you just check if the position of the sprites overlap? If the sprites do not overlap often this should not add too much calculations and grands more randomness then a fixed position template.
What you do in the class for the entities with the sprite is add a public Rectangle, you probably already use a rectangle to draw them to the screen. Making this public allows you to do something like this in the class that generates the entities.
if (addSprite)
{
Entity newEntity = new Entity(random position);
foreach (Entity e in entityList)
{
if (newEntity.rectangle(Intersects(newEntity.rectangle))
{
//give a new position to newEntity and run this loop again
}
else
{
entityList.add(newEntity);
}
}
I was going to to write a game (it's called "Qwirkle" if you ever heard of it) in which a 2-dimensional game field stores the position of stones which the players have put into it. The first player puts a stone anywhere and other players can connect to it from any side (left / right / top and bottom). The game field itself is not restricted to a fixed size which would ruin the game idea. However, the number of stones is limited to a value the player can define at start.
Because of the game logic I need to for-loop through the stones with an index. However, since the players can add stones from any side, I'd need a list which is expandable into any direction (e.g. into negative and positive index direction).
Performance is not unimportant since I need to check several stones in one turn.
The best thing would be to access a stone like _stones[-3,5] to access the one at position -3, 5, of course.
I thought a stack which can be pushed and popped from any side (like PushBack / PushFront) would be useful for this, but I'm not quite sure how to realize that in C#.
Are there pre-implemented lists / stacks like the one I'm thinking of, or is my approach completely weird?
The data structure you want is an immutable quadtree. If the board is mostly empty then using an immutable quadree enables you to represent boards that are essentially unlimited in size; a one-trillion-by-one-trillion cell board takes only a few bytes more memory than a 32-by-32 cell board. Immutable quadtrees can easily be indexed in the manner you describe, and computing a new quadtree given an old quadtree and an edit is straightforward.
I've written immutable quadtree algorithms several times over the years and I have been meaning for a long time to do a series of blog articles on them, but I never have. When I do I'll come back and update this answer.
In the meanwhile, this Dr. Dobbs article on Gosper's Algorithm is the one I used to learn how immutable quadtrees work.
http://www.drdobbs.com/jvm/an-algorithm-for-compressing-space-and-t/184406478
What you want is a double ended queue (known as a deque, pronounced "deck"). There is no implementation in the .NET BCL (unfortunately) but there are 3rd party implementations (see Google).
Dictionary could be an option, instead of thinking about indexes of a list, you could think of integer keys of a Dictionary. No matter what's the dimension.
Dictionary<int,Dictionary<int,Stone>> stones = new Dictionary<int,Dictionary<int,Stone>>();
// do some initialisation for the base field size ...
// access it this way
Stone s = stones[-1][-5];
Only issue is when you want to add a 2nd dimension which can get resource consuming (iterating over all 1st dimensions).
I think you will learn more from implementing a custom data structure containing, say, two ArrayLists. One going forward and one going backward. Of course, in your implementation they will both go the same way but your implementation could have the data structure return a negation of the positive index plus one so that you don't get an index of -0 for the first element in the backward ArrayList.
Maybe I've misunderstood the problem, but that's how I would go about implementing a two-way expandable data structure.
Good luck!
You could do something like:
_stones[-(i),i] and/or _stones[i,-(i)]
Just a suggestion.
I started work on a dungeon crawler in C# and I've already coded the level generation.
However, I've run into a problem. My level map is stored in a 32x32 multidimensional array, and each tile is stored as a string. All the tiles except for the following (all of these names are the variable names that represent that tile) (mongroveplant, tree, hjalaplant, vnosplant, barraplant, weedplant, naroplant, deathweedplant, venustrap, strangulator, statue, emptiness and stonewall) cannot be walked over.
These tiles (which can be walked over), which constitute a much longer list, are found here: Walkable Tiles. In each entry in the 32x32 multidimensional array, every entry is a string.
How do I create a pathfinding algorithm that avoids all the tiles listed above, but can go through all the tiles listed in the link? I am trying to go from the "start" tile to the "exitlevel" tile.
The first thing I would remove is the notion of string. Parsing string isn't quick in term of a video game. What you want, is to have flags for each tiles (bitfields). In the end, you will love flags because you can combine them!
[Flags]
public enum TileDescription
{
Walkable,
Trap,
Altar,
Door
}
They can also be stored at a int, which take far less space. Speed and space, two amazing notions.
As for the path-finding algo, there's plenty of them out-there. But basically, you have a start point, a end point, and you must find the quickest way between both. The idea is to check the nearest "nodes" and see if you get closer or not of your goal. Each time, you repeat the check with the new node. If you get trapped, you rewind to the nodes that still had available paths.
You have some nice basic algo :
http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
http://en.wikipedia.org/wiki/A*_search_algorithm
However, long range pathfinding is ALWAYS extremely costly. You will have to limit the pathfinding to a specific range around the origin. Parsing a whole 32x32 maze could take a lot of time to find the quickest route. In most case, when you are beyond a specific range, you move your NPC up to the closest point, then repeat the pathfinding when it reaches it, or while reaching it. The trick to pathfinding is to do it over many frames and never to try to process it all at once.