Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by perdamcivitatem · May 02, 2018 at 11:15 PM · buttonlistbuttonslists

How can I check value stored in a list when pressing button with the same name?

I've assigned how much a person would like to do x activity and have it in a list. Using buttons you can ask the npc what it wants to do from given activities. How would you make it so each button would check the value the npc has for that activity?

This is not from the activities, but its done the same way: gamingTypes.Add (new Gaming ("Board Games", Random.Range(-5, 5)));

How can I make so if the button that says "Running", checks the script for the value the character has in running?

EIT: I have some activities like the ones below. As a start, I'm making it so it generates how much a npc likes those activities, and you can ask the npc if it wants to do X activity. I'm doing it using buttons, with all the options in the screen, and I wanted to try to make it so, if the player clicks on Running, it would look for "Running", on the list, and get the value. How should I go about this? Sorry if this is too basic, had been a while since I programmed the last time:

     activity.Add (new Activities ("Fishing", Random.Range(-5, 5)));
     activity.Add (new Activities ("Running", Random.Range(-5, 5)));
     activity.Add (new Activities ("Hunting", Random.Range(-5, 5)));
Comment
Add comment · Show 5
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 bobisgod234 · May 02, 2018 at 11:53 PM 0
Share

Something like this perhaps:

 int value = ga$$anonymous$$gTypes.Single(x => x.name == button.name).value;

Since you have provided no code, I can only guess as to what you have called your variables.

avatar image perdamcivitatem bobisgod234 · May 03, 2018 at 02:08 AM 0
Share

Sorry, thanks anyway. Edited the question explaining properly and adding some code

avatar image toddisarockstar · May 02, 2018 at 11:54 PM 0
Share

please post your code with your list so we can help

avatar image perdamcivitatem toddisarockstar · May 03, 2018 at 02:09 AM 0
Share

Sorry, I'm stupid. Edited the question. Hope it helps!

avatar image RobAnthem · May 03, 2018 at 06:02 AM 0
Share

This is simple, yet the answer is not simple. You need to create the logic for this system to work, it's not some miraculously built-in thing just because you read it on a board game tutorial.

What I tend to do for displaying dialog is creating a class that is a dialog entry template, a dialog controller, and instantiate the dialogs as needed. So lets say your dialog entries can contain an activity, that would be included in the template class, then define it during instantiation. $$anonymous$$eep the activities separate. Like this. (this is a basic example mockup, don't expect it to work with copy and paste)

 public class DialogEntry : $$anonymous$$onoBehaviour
 {
     public Text text;
     public Activity activity;
     public void OnDialogSelected()
     {
         DialogController.Instance.RegisterSelection(this);
     }
 }


and

 public class DialogController : $$anonymous$$onoBehaviour
 {
     public static DialogController Instance
     {
         get
         {
             if (instance == null)
             {
                 instance = FindObjectOfType<DialogController>();
             }
             return instance;
         }
         private set
         {
             instance = value;
         }
     }
     private static DiaogController instance;
     public DialogEntry template;
     public RectTransform dialogAnchor;
     public void Initialize(Dialog dialog)
     {
         foreach (DialogActivityPair pair in dialog)
         {
             DialogEntry entry = Instantiate(template, dialogAnchor);
             entry.text.text = pair.text;
             entry.activity = pair.activity;
         }
     }
     public void RegisterSelection(DialogEntry entry)
     {
         if (entry.activity == someExpectedActivity)
         {
             //Do something based on expected activity.
         }
     }
 }

2 Replies

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

Answer by Pinkuboxu · May 03, 2018 at 01:52 PM

@tormentoarmagedoom Raises a good idea but didn't really supply an implementation. Also, I think a Dictionary would simplify this greatly.

A very simple version but this is the code you would put on the NPC:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Activity : MonoBehaviour {
 
     Dictionary<string, int> activities =
         new Dictionary<string, int>();
 
     void Start ()
     {
         activities.Add("Fishing", Random.Range(-5, 5));
         activities.Add("Running", Random.Range(-5, 5));
         activities.Add("Hunting", Random.Range(-5, 5));
     }
 
     public void GetActivity(string activity)
     {
         print("My " + activity + " is " + activities[activity]);        
     }
 }



Of course you would probably want to change the class name to something else like ActivityPool and have a method that helped add activities to the NPC rather than just pop them in at start... among other things.


Here is how to set up a Button for use with this.

[1]: /storage/temp/116139-dictexamp1.png


dictexamp1.png (120.4 kB)
Comment
Add comment · Show 3 · 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 perdamcivitatem · May 03, 2018 at 08:00 PM 0
Share

You are right! Just checked unity's video on dictionaries, and, if I understood correctly, it allows me to access the information much easier and faster! Gonna keep learning about both rn, and will reply to the code and all that later.

I have a question tho, just so I know I'm getting this properly. Could I have NPC's stats, likes... stored in their own dictionaries... I'm guessing I'll also be able to edit the values... $$anonymous$$an, thanks a lot. I can kinda see it now. With lists it was kind of a mess thinking about accessing the information stored, but this way seems way better.

Btw, I'm guessing dictionaries are also better when storing a lot of information. Am I right assu$$anonymous$$g this?

Edit: I'm just wondering, should I give the random value as I was doing it (when adding it to the list/dictionary), or using For to go through the dictionary adding the value? I'm gonna have a lot of stats, so I don't know what'd be the best way

Gonna learn properly about it. Thanks a lot!

avatar image Pinkuboxu perdamcivitatem · May 05, 2018 at 05:30 AM 0
Share

Yes, Lists are kinda/sorta just a bit of arbitrary arrays, and dictionaries are more of "Hashtables" which ins$$anonymous$$d of an integer index to grab the data... it can technically be any other type of data as a key to access another type of data. I think I'm ok in saying that the sky is the limit, kinda. You can use your own class/struct as a key or value, I think. You could have any data, or for Unity sake GameObject, point to another bit of data. Just remember $$anonymous$$ISS principle and don't get so bogged with stuff that you lose interest. As amazing as all of the new data types that exist, I feel it sometimes detracts from what we started with in low level coding. I'm not saying you have to learn any assembly to have a good time, but some core CSci is probably good medicine. There's always a really easy and simple way to do something, even before all of the candy we get in C#, there's just so many ways of doing things now that it's kind of $$anonymous$$d boggling. Good luck and I look forward to hearing what you come up with.

avatar image Pinkuboxu perdamcivitatem · May 05, 2018 at 04:31 PM 0
Share

As for the other part, adding stats for NPCs or what not, I'd look into scriptable objects if you need a way to add persistant data that you input by hand, however if you need something more procedural/runtime generated the method you are doing is fine but I'd wrap it in a class or struct so it's more flexable and re-usable.

avatar image
2

Answer by tormentoarmagedoom · May 03, 2018 at 11:20 AM

Good day.

Ypu should check the event trigger system. It allows you to execute a method with parameters, like the button name.

Bye

Comment
Add comment · 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

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

105 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 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 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 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 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

Store instantiated object in List 2 Answers

Getting the element that has the same element number on another list ? 2 Answers

Remembering list of camera positions is not working 0 Answers

Calling functions via buttons when cooldown finished 1 Answer

Show and hide more buttons after clicking different button 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