Tuesday, February 2, 2016

How to get the previous value from string array c#(Unity)



I have this example data



Data: P ,B ,B ,T ,P



This must be something like this


enter image description here


But what is happening to me is this


enter image description here


here's my code


string[,] table = new string[104, 6];

string newPreviousValue = "placeholder";
int xIndex = -1;
int yIndex = 0;
if (result.Equals(newPreviousValue) && yIndex < table.GetLength(1) - 1)
{
yIndex += 1;
table[xIndex, yIndex] = result;
}

else

{
xIndex += 1;
yIndex = 0;
table[xIndex, yIndex] = result;
}

So you won't get confuse the result variable is the data generator. What could be missing on my condition?


I tried doing something like this


if (result.Equals(newPreviousValue) && result.Equals(newPreviousValueTie) && yIndex < table.GetLength(1) - 1) but what's happening here is they're all in y axis falling in line .




opengl - How should I do 3D games through Java on a mac?




I have been self-teaching myself Java on the mac mostly because the language is cross-platform. Recently, I have been only able to develop 2D games using the Graphics2D class. Now, I want to learn how to make 3D games in Java. I used to model and animate stuff in 3D, so my knowledge of 3-Dimensional stuff is okay.


I have spent the last 3 hours using google to look up ways of making 3D games in java. Apparently the best one to use is OpenGL, so i looked up a tutorial on it and i cannot find a tutorial that shows how to (if there is a way) install JOGL on the Mac platform.


Should i continue to use Java?


How can i make 3D games using Java?


What is the best way to make 3D games on a mac?



Answer



I would recommend using LWJGL. It's an OpenGL library for Java that works on Mac, Linux, and Windows.


You can make 3D games in Java just as easily as you would in C or C++ using LWJGL. Although some may complain that Java is slow, modern Java is fast enough for game purposes.


An example of a successful game created using LWJGL: Minecraft. Another: Revenge of the Titans (cannot post link.)



If you're looking for more game-development tutorials, there are many available on the LWJGL wiki / website / forums. Also, you can follow many C or C++ OpenGL tutorials as the OpenGL functions work almost exactly the same on Java.


unity - Sprite disappears after adding a material to it


I have background which is FirsLayer, order in layer 0 and platform over it which is FirstLayer, order in layer 1.


enter image description here


Once I add a material to the platform, the layer doesn't seem to change but it gets rendered behind the background.


The material that I add:


enter image description here


1)Why do I get such results?


2) How can I add the material to the platform and still keep it above the background?


P.S. For further clarification if somebody didn't understand, I expected the sprite to stay on its original position(i.e. not to go behind the background when a material is being added to it)




unity - How to make an Assert.IsNull test pass when the value is reported as ?


I've just started using unity tests with the built in editor tests. After making my test and finding out that it constantly failed I debugged the code and found that a static object was set to a new game object when running the test while it is set to null when playing in the editor.


Is there a way to force unity to treat the test the in the same way that it treats scripts at run time?


Edit


After changing some code around i get another strange error: Editor Tests window showing error


does anyone know why "null" isn't "null". The null objects are stored in an array, but that shouldn't make a difference right?



Edit Edit (Edit 2?)


Here is the code that has the issue


public class StatCollector : MonoBehaviour {

public static StatCollector running;

//ensure that there is only one object running at the same time
public void Awake () {
if(running == null)
{

DontDestroyOnLoad(gameObject);
running = this;
}
else if(running != this)
{
DestroyImmediate(gameObject);
}
}
}


and here is the test:


[Test]
public void SingleTest()
{
//create gameobjects
GameObject[] collect = new GameObject[2];
collect[0] = new GameObject("one");
collect[1] = new GameObject("two");
collect[0].AddComponent();


//see if gameobjects were created correctly
if (collect[0] == null || collect[1] == null) Assert.Fail("failed to create game objects");

//test what happens when the second stat collector is added to the scene
collect[1].AddComponent();
collect[0].GetComponent().Awake();
collect[1].GetComponent().Awake();
Assert.IsNull(collect[1]);
}


I'm assuming that there is no way to keep Awake private which is why it is public (just so that the test case has access to it)



Answer



First some background...


There are two flavours of "null" in Unity:




  • Actual null




  • "Pseudo-Null" - this is a non-null Unity object (usually a GameObject or MonoBehaviour) that has been removed from the scene using Destroy or unloading a scene.



    (In-editor, this is also used for reference fields in an Inspector that have been left blank)


    The reason this is called pseudo-null is that for an object in this state, (destroyedObject == null) evaluates to true using an overloaded operator.




Using DestroyImmediate() should put an object into this state immediately, while using Destroy() will do so at the end of the current frame's updates but before rendering, which can often help avoid order-of-execution bugs when multiple scripts are referencing an object.


The thing is that a "pseudo-null" reference isn't actually null, which you can confirm by calling:


System.Object.ReferenceEquals(destroyedObject, null); // returns false

...or by trying to access any of the object's members - you won't get a null reference exception, but a Unity MissingReferenceException (which is handy, because the debugging tools can tell you more about which object got destroyed while a script was still trying to use it - something we don't get from real nulls).


This psuedo-null stub will remain until all references to it are cleared/go out of scope, using normal C# garbage collection rules. Read more about the reasons this exists in this Unity blog post.





How this applies to your test


My guess is that Assert.IsNull() at the end of your test is internally checking for real null instead of comparing with the overloaded operator.


(Especially if you're using Microsoft.VisualStudio.TestTools.UnitTesting.Assert rather than Unity's version - I don't see any using statements in your code so I'm guessing)


Since the destroyed object is only pseudo-null, this check comes back false, tripping the assertion.


We can ensure it uses the overloaded operator to also detect pseudo-null by rephrasing the assert at the end of your test as:


Assert.IsTrue(collect[1] == null);

We know this == will return true for a Destroy()ed object, whether it's recently-destroyed or long-dead, so this phrasing reliably checks for what we want.


architecture - Keyboard input system handling


Note: I have to poll, rather than do callbacks because of API limitations (SFML). I also apologize for the lack of a 'decent' title.


I think I have two questions here; how to register the input I'm receiving, and what to do with it.



Handling Input


I'm talking about after the fact you've registered that the 'A' key has been pressed, for example, and how to do it from there.


I've seen an array of the whole keyboard, something like:


bool keyboard[256]; //And each input loop check the state of every key on the keyboard


But this seems inefficient. Not only are you coupling the key 'A' to 'player moving left', for example, but it checks every key, 30-60 times a second.


I then tried another system which just looked for keys it wanted.


std::map< unsigned char, Key> keyMap; //Key stores the keycode, and whether it's been pressed. Then, I declare a load of const unsigned char called 'Quit' or 'PlayerLeft'.
input->BindKey(Keys::PlayerLeft, KeyCode::A); //so now you can check if PlayerLeft, rather than if A.


However, the problem with this is I cannot now type a name, for example, without having to bind every single key.


Then, I have the second problem, which I cannot really think of a good solution for:



Sending Input


I now know that the A key has been pressed or that playerLeft is true. But how do I go from here?


I thought about just checking if(input->IsKeyDown(Key::PlayerLeft) { player.MoveLeft(); }
This couples the input greatly to the entities, and I find it rather messy. I'd prefer the player to handle its own movement when it gets updated. I thought some kind of event system could work, but I do not know how to go with it. (I heard signals and slots was good for this kind of work, but it's apparently very slow and I cannot see how it'd fit).


Thanks.



Answer



What I'd do is use the observer pattern and have an input class that maintains a list of callbacks or input handling objects. Other objects can register themselves with the input system to be notified when certain things happen. There are different types of callbacks you could register, based on the type of input events observers would like to be notified about. This could be as low level as 'key x is down/up' or more game-specific like 'a jump/shooting/movement event happened'.


Generally game objects have an input handling component that is responsible for registering itself with the input class and providing methods to handle the events it is interested in. For movement, I have an input mover component that has a move(x, y) method. It registers itself with the input system and gets notified on movement events (where x and y would be -1 and 0 to signify moving left), the mover component then changes its parent game object coordinates based on movement direction and object velocity.


I don't know if this is a good solution but it works for me so far. In particular it allows me to provide different kinds of input system that map to the same logical game events so that gamepad button x and keyboard space both generate a jump event for example (it's a bit more complicated than that, allowing the user to reassign key mappings).


Monday, February 1, 2016

Statements used as questions in casual speech


I see people say statement as question while speaking by raising their voice at the of it to mean it's a question.


For example consider followings.





  1. You want to stay here? ==> Is used to mean "Do you want to stay here?"

  2. You guys spoke to anyone? ==> Is used to mean "Did you guys speak to anyone?"



Are these really used by native speaker in casual speech? And can I write them in the same way while writing them as dialogue by putting them between quotes? Are there any special grammar rules for these kind of questions?



Answer



Yes, they're really used by native speakers in casual speech.


Yes, you can write them that way in dialogue.


No, there are no special rules. Just write a sentence that is grammatically a statement, then write a question mark at the end instead of a period to signal the rising intonation. (That's what you've done in your examples.)


word usage - "He is in death" or "He is dead" or "He died"? Are they all correct?





  • "He is in death"




  • "He is dead"





  • "He died"





Are they all correct?


I am feeling that "He is in death" and "He died" are not correct.




Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...