Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Scobbo · Apr 04, 2014 at 10:01 AM · valuescardconstant

Keeping card variables together

I have a card game where each card has four values (type, value, cost, price) they each change but each can't be dynamically assigned because each card is different. Some example cards are |Attack, 4, 7, 10| in which the card is an attack card, that deals 4 damage, costs 7 action points to cast and costs 10 dollars to buy. |Block, 6, 15, 50| is a card that blocks 6 points of damage, costs 15 ap to play and costs 50 dollars to buy. What is the best way to keep these values constant and together. I have been looking at a few ways but I'm not sure.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wijesijp · Apr 04, 2014 at 10:12 AM 0
Share

Are these cards prefabs?

avatar image Scobbo · Apr 04, 2014 at 10:24 AM 0
Share

No, the information will be applied to a prefab when it is processed. I was thinking, when drawing a card, it will randomly select a card from the library array and put it in the player's hand. But with four bits of data (or 3 in the game, doesn't need price when playing) it makes it harder especially because the cards can't be dynamic like a deck of playing cards which I once used a 2d array of bools because both red and black card had every number assigned to it but a poison card and an attack card of the same value will have different costs. I hope that makes sense.

avatar image fendorio · Apr 04, 2014 at 10:25 AM 1
Share

Use a Class/Object?

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fendorio · Apr 04, 2014 at 10:31 AM

Create an enum to store the type

 public enum CardType
 {
    attacking,
    blocking,
    whatever_else
 }

Create a class to hold each cards values.

 public class Card
 {
     public CardType type;
     public int card_value, card_apCost, card_costToBuy;

     Public Card(CardType type, int value, int apCost, int costToBuy)
     {
          card_type = type;
          card_value = value;
          card_pCost = apCost;
          card_costToBuy = costToBuy;
     }         
 }

Then create a Script Component for each card and give it an instance of the Card Class to store it's values?

Unsure if you wanted JavaScript, it shouldn't be too hard to translate.

Note that that data structure is pretty unsafe - unsure if you're concerned about that.

For instance you could add some encapsulation to protect the values by using getters/setters - like so:

   public int card_Value { get; private set; } 

Now you can only set the card_Value inside the class, so to set it from outside you'd need to add an accessor method - like so:

  public void Set_Card_Value(int value)
  {
       //Add some validation logic if required - such as 
       if(value < 0)
         {
             value = 1;
         }
       else
         {
             card_Value = value;
         }
   }

Also note that if you do not add an accessor method and use the { get; private set; } getter/setter as stated above, the value is going to be immutable (a constant value).

Going on from your comment:

Create a gameObject to use as a prefab, then attach a script component to it. Let's just say it's called ScriptOne for simplicity.

  public class ScriptOne : MonoBehavior
  {

       public Card the_instance_I_Was_Talking_About;

       void Awake()
       {
            the_instance_I_Was_Talking_About = new Class(CardType.attacking,
                                                         10,
                                                         5,
                                                         0);

       }
 }

How you want to assign the values in awake i'm unsure, i'd need to know more about your game.. But yeah, now accessing that, is as simple as calling .GetComponent().

Something like this - assuming you assigned the object with a tag in this instance.

  void SomeFunctionElseWhere()
  {
  
       int x = GameObject.FindGameObjectWithTag("WhateverTagYouUsed")
                         .GetComponent(ScriptOne)().cardValue;

  }

Note that the 'ScriptOne' in the above .GetComponent should be enclosed in those triangles (Chevrons? lol), but they don't show up in the answer so I used brackets instead.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scobbo · Apr 04, 2014 at 10:40 AM 0
Share

Thanks man! I'll give it a go, and no I use C#.

avatar image fendorio · Apr 04, 2014 at 10:42 AM 1
Share

No worries! Just @me in a comment if you run into any trouble, i'll be poking around for most of the afternoon :P

avatar image Scobbo · Apr 04, 2014 at 10:44 AM 0
Share

One thing I'm not sure about is what you mean by give the instance of the class to the script. Sorry for the trouble, I'm still learning and this seems to be a difficult one. @fendorio

avatar image fendorio · Apr 04, 2014 at 11:00 AM 1
Share

Edited, hope that gives some clarification.

avatar image fendorio · Apr 04, 2014 at 11:28 AM 0
Share

@Scobbo please consider accepting the answer if it worked for you :P

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to smootlhy change values in amout of time? 3 Answers

Synchronizing multiple values 1 Answer

How do I create a Key and Values Dictionary array in C# 1 Answer

Inventory system. Set multiple values/layers to one variable. 0 Answers

Tree Structure: update root element (reference) for all classes 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges