Assistance with Playing Card Game.

Hello all,

For days now I have been trying to find the answer and keep running into dead ends so now im gonna try getting an answer here :smile:. Right now im trying to create a card game. I am able to create my deck of 52. Im able to lay 52 cards on a table. Once one of the cards are selected it will destroy and i have a message to pop up (using the print function). Now my problem is that when I select a card. I would like a GUI text or a window to pop up and give directions based on that card. My script has the switch statement set up so my cases go from 0 to 52. and each case i have print (…). but i would like to have a GUI pop up when that number comes up. i have tried adding var test=true; into the case and then in the OnGUI function i have if(test) then display gui but it doesnt work. any assistance out there would be great.
Or even a full tutorial out there that is a card game like solitare.

Flashnburn,

Glad you decided to come to the forums for help… thats what we are here for!

Sadly I do not know of any tutorials specific to card games off the top of my head. My best suggestion would be to post a sample of the code which is not working correctly so we can offer specific pointers.

To create an effective menu structure you would want to use some form of state machine. As far as a basic overview of doing popup dialogs, you would do something like this:

  • Layout your dialog in the OnGUI of a script that will always be in your scene. Make sure the popup dialog looks the way you expect it to.
  • Create a bool variable for storing the visibility state of the dialog.
  • Wrap the code for drawing your dialog with an if (myBoolVar) { } so that it only is drawn when this value is true.
  • Toggle the bool value when you want to show/hide this dialog.

Thanks for the fast response. I think I understand what you are saying. An example like: have a game object hiddin and then in that point of script enable the renderer of it ? or even move the object to the specific location…

Here is a sample:

private var selectCard : int;

function OnMouseDown () {

selectCard = Random.Range(0, Script.cardDeck.length);
Script.cardDeck.RemoveAt(selectCard);
Destroy(gameObject);

print("you have selected: " + selectCard);

switch (selectCard) {

case 0:
break;

case 1:
break;

case 2:
break;
(And so on…

in the cases i would like for a box and some 3D Text to show up so i tired putting the instantiate command in the cases with a prefab of the box and also tried to move the 3d text but it does nothing during the cases… but if i put print(ā€œMessageā€); then the message is printed when the value is equal to that corresponding number.

Flashnburn,

You don’t really need the game object to be ā€œhiddenā€. You could simply make a PopupGuiScript and attach it to a blank game object. Doing this would mean that there is nothing for the editor to render until you tell it to draw something in the OnGUI method.

I would strongly recommend reading through this article and practicing some of the techniques it teaches:
http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

Yea I have tried a bunch of things to make the switch statement goto the OnGUI function but its not working… I have tried to put var example = false and case 1: example=true; then if example then display the GUI.

var testGUI = false;

switch

case 1:
testGUI=true;

function OnGUI () {

if (testGUI) {
GUI.Button (Rect (10, 10, 150, 100), ā€œI am a buttonā€);
}
}

Flashnburn,

The code you posted does not make sense, you should be toggling that variable when your action is performed.

So if you have your OnMouseDown() method working correctly on your card you would do something like this:

function OnMouseDown() {
    // guiObject would be a reference to your GUI component.
    guiObject.testGUI = true;
}

The above code would be attached to your card so that when you click it you let the guiObject know that it should show your dialog. You would most likely call a method in which you could pass additional information to your guiObject. This is just a workflow example.

You would want to get a reference to your component via the editor or dynamically in the Start method.

After this info im really thinking it would be alot easier to use the gui buttons and then just add custom gui textures to the buttons to do what i want and then just call each one under the function OnGUI instead of having my other objects trying to access these gui’s :smile: