0

I'll give you an example of what I'm trying to accomplish.

Using twine2 (where I started) I could programmatically set and manipulate variables by directly referencing other variables as such (I'll explain the syntax a bit for anyone unfamiliar with twine).

Initialize and set my variables

  // data map is equivalent to dict
$Inv = (dm: potion, 0, bomb, 0, gold, 0);
  // array
$PossibleRewards =(a: potion, bomb, gold);
  // string
$Reward "";

Then

set $Reward to (shuffled... $PossibleReward's 1st);
set $Inv's $Reward to it +1;

So what it does is simply shuffle the PossibleRewards array, choses the first entry then sets the Reward string to what was chosen, then automatically increases the Inv data maps appropriate entry by one.

Of course you could skip the string and just go

set $Inv's (shuffled... $PossibleReward's 1st to ot +1

I know it can be done, but need some help with the syntax, any input is appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LoTechFo
  • 5
  • 2
  • I'm not asking how to create or initialize variables, I'm asking how to do the conversions that were automatic in another language. Did you even read or put any consideration into the question or did you just want to pull a 'scoff the newb' – LoTechFo Feb 19 '19 at 03:14
  • Nor sure what you're upset about. You've shown no c# code, so it looks like you've not tried yet. If you post the code you have it would be easier to see where you're stuck and provide help. – Rufus L Feb 19 '19 at 04:05
  • @RufusL, reading your comment the 1st time guess I just took the whole comment as its first statement, upon rereading it through a second time I see what you were saying, turns out I was the one jumping to conclusions, and I apologize for that – LoTechFo Feb 19 '19 at 17:37
  • No worries, thank you! – Rufus L Feb 19 '19 at 17:52

2 Answers2

0

Here is a post covering shuffling an array in C#: Best way to randomize an array with .NET. There are several options in the answers.

Assuming you have a Dictionary<string, int> for your inventory (already populated with all possible rewards), you can simply assign it it as inventory[possibleRewards[0]] += 1 or inventory[possibleRewards[0]]++

I would not, however, shuffle the array each time. I would instead generate a random number and use that to select the index in the array, e.g.

var rand = new Random(); // do this once, e.g. in startup
// initialize possibleRewards array 

// in your reward method
var index = rand.Next(numberOfRewards);
inventory[possibleRewards[index]] += 1;

If Twine2 is your only exposure to programming so far, I'd suggest an introductory C# tutorial would be time well invested.

Colin Young
  • 3,018
  • 1
  • 22
  • 46
  • Yea I am currently taking a unity and a c# course. And thanks fo suggestion, I'll give it a shot when I make it home – LoTechFo Feb 19 '19 at 14:26
  • As a long-time C# developer and recently picking up Unity, my biased opinion would be to learn C# first, then Unity. Unity has their own way of doing things and having a good grounding in C# helps orient yourself. And welcome to StackOverflow. – Colin Young Feb 19 '19 at 14:43
  • I've noticed a couple of inconsisenies, so I think you are right, I'll take the advice and focus more on gaining a good foundation in c# 1st. – LoTechFo Feb 19 '19 at 17:39
0

If PossibleRewards is just a duplicate of the Dictionary.Keys values, then you may not need it. You can use the Random class to grab a random item in the dictionary using the dictionary's ElementAt method (which returns a KeyValuePair), set the Reward to the Value of that element, and then can increment the Value of that element in the dictionary:

Random rnd = new Random(); // Note: you typically only need one instance of Random in a class

// Create a dictionary of rewards and quantity
Dictionary<string, int> inventory = new Dictionary<string, int> 
    {{"potion", 0}, {"bomb", 0}, {"gold", 0}};

// Grab the Key of a random item in the dictionary for the reward
string reward = inventory.ElementAt(rnd.Next(inventory.Count)).Key;

// Increment the value at that Key
inventory[reward]++;

Otherwise, if the list is needed (if Rewards is a shared list between instances of different classes, for example), then an individual inventory dictionary can be initialized based on the list. The easiest way is to use the System.Linq extension method, ToDictionary:

private static readonly Random Random = new Random();
private static readonly List<string> Rewards = new List<string> {"potion", "bomb", "gold"};

private static void Main()
{
    // Initialize an inventory dictionary based on the shared list of rewards
    var inventory = Rewards.ToDictionary(key => key, value => 0);

    // Grab the Key of a random item in the dictionary for the reward
    var reward = inventory.ElementAt(Random.Next(inventory.Count)).Key;

    // Increment the value at that Key
    inventory[reward]++;
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • A little late but I figured I should come back and set a selected answer in case it becomes helpful for others. I've accepted this answer over the one above (which also works), because 1) you provided a solution including the array as requested as well as a second more efficient solution. 2) you explained the code in very effective way, as a new come too much detail can be overwhelming, but too little isnt teaching me anything, yea I ouls simply copy and paste the code, but then how does really help me going forward? Ourexplaination was simple enough to not overwhelm and detail enough that I u – LoTechFo Mar 05 '19 at 16:57