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 /
avatar image
0
Question by ZacGarby · Aug 17, 2015 at 06:03 PM · c#randomstructgenerate

Generating a word out of an array, each has its own rarity.

I'm making a script to generate random weapons with prefixes and a suffix. I've made 2 structs, one called ModifierPrefix and another called ModifierSuffix. They each have:

  1. A name (string)

  2. A damage modifier (float)

  3. and a rarity (float between 1 and 10)

I just need a way to generate a random prefix and suffix based on it's rarity.

Here's the script:

 using UnityEngine;
 using System.Collections;
 
 public class Weapons : MonoBehaviour {
 
     [System.Serializable]
     public struct ModifierPrefix {
         public string modifier;
         public float damageChange;
         public float rarity;
     }
 
     [System.Serializable]
     public struct ModifierSuffix {
         public string modifier;
         public float damageChange;
         public float rarity;
     }
 
     [System.Serializable]
     public struct Weapon {
         public string name;
         public float damage;
         public ModifierPrefix[] prefixes;
         public ModifierSuffix suffix;
     }
 
     public Weapon[] weapons;
     public ModifierPrefix[] possiblePrefixes;
     public ModifierSuffix[] possibleSuffixes;
 
     // Generate a new weapon
 
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DeeCeptor · Aug 17, 2015 at 10:34 PM

A simple way to do it would be to create a large array of prefixes and have it filled up with prefixes based on their rarity, and then selecting a random prefix from this list.

From these suffixes: [name, rarity], [deadly, 1], [rare, 3], [common, 6]

you would generate a list looking like: (deadly, rare, rare, rare, common, common, common, common, common, common)

The rarity dictates how many copies of each prefix you put into this array. Now this array represents your chances of getting each prefix.

Now use Mathf.Range(0, listSize - 1) to select a prefix from it.

Rinse and repeat for suffixes and damage.

Comment
Add comment · Show 2 · 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 ZacGarby · Aug 20, 2015 at 09:28 PM 0
Share

Good idea! I'll try it now :)

avatar image ZacGarby · Aug 20, 2015 at 09:36 PM 0
Share

I'm just doing it, but i just realised - what if i wanted a decimal number for the rarity? EDIT: Also, how do i append something to an array?

avatar image
0

Answer by cjdev · Aug 18, 2015 at 10:13 PM

Personally I would change the Arrays to Lists and use LINQ to sort through them by rarity. You could do something like this:

 using System.Collections.Generic;
 using System.Linq;
 
 public List<ModifierPrefix> possiblePrefixes;
 
 //Gets a collection of modifiers that have a rarity > 7.0
 IEnumerable<ModifierPrefix> sorted = possiblePrefixes.Where(x => x.rarity > 7.0f);

 //Picks a modifier from a random location in the collection
 ModifierPrefix randomPrefix = sorted.ElementAt(Random.Range(0, sorted.Count()));
Comment
Add comment · Show 1 · 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 ZacGarby · Aug 20, 2015 at 09:30 PM 0
Share

I'm relatively new to C#, so i don't really understand what LINQ is...

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

27 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

Related Questions

How to instantiate prefabs at random postions in 2d 1 Answer

Multiple Cars not working 1 Answer

Generate new blocks to land on 1 Answer

Distribute terrain in zones 3 Answers

How can I randomly create a number and then delete it to stop repeating 3 Answers


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