Author Topic: Arrays  (Read 4617 times)

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Arrays
« on: 2008-08-17, 11:46:11 AM »
i thought i could manage some things with arrays.
for example:
there is a new custom code document called "personal arrays". it holds three empty arrays, named "Marco", "Daniel" and "Joshua". There are three NPCs named the same way. If the player talks to them the value "tree" will be pushed into the array of that NPC. The arrays are used to store global values for various uses.

a friend of mine wrote arrays in PHP, i wonder what the correct C# syntax would be.
Code: [Select]
$marco = array("tree","topic1");
$daniel = array();

what of these components do i need in the head of the custom code? (using System; using System.ComponentModel; etc.)

and would it look like this?
[code]namespace CustomObjects
{

public class Talking_Array
   {

      public static void PushIntoArray(??? string my_value, string my_array)     // i don
« Last Edit: 2008-08-17, 11:51:40 AM by Tanja »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Arrays
« Reply #1 on: 2008-08-18, 05:41:04 AM »
I don't have time to go into details at the moment, so let me just post my guess at the essence of the answer:
Code: [Select]
System.Collections.ArrayList my_list = new ArrayList();
my_list.Add("new string");

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Arrays
« Reply #2 on: 2008-08-18, 07:00:00 AM »
Now that I have a little more time:
This would be the C# translation of the PHP code:
Code: [Select]
string[] marco = new string[] {"tree", "topic1"};
string[] daniel = new string[];

However, if you want to be able to add to the list, you should use this:
Code: [Select]
List<string> marco = new List<string>(new string[] {"tree", "topic1"});
List<string> daniel = new List<string>();

Then you can add and to them like this:
Code: [Select]
daniel.Add("coconut");

In either case, you can access the elements like this:
Code: [Select]
string first = marco[0];

But if you just need to be able to track whether the user has covered a particular topic or not, I recommend not storing strings.  All you need is one bit of information.  So you could put it in an array of bits.

Code: [Select]
      // Enums must be defined outside the scope of a function (but within a class)
      enum marco_topics
      {
         tree,
         topic1,
         topic2,
         topic3,
         topic4
         count
      }
      // Create new bit array with element count equal to enum value count, all defaulted to false
      static System.Collections.BitArray marco = new System.Collections.BitArray(marco_topics.count, false);
      // Remember that tree has been discussed
      marco[marco_topics.tree] = true;
      // Check if tree was discussed
      if (marco[marco_topics.tree])
      {
         // trees are covered
      }

I just remembered that SGDK 2.0 is based on .NET 1.1, and "generics" (like List<string>) are only available in .NET 2.0.  But if you want to use a BitArray, it doesn't matter (if you don't I suggest using an ArrayList as demonstrated above).

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Arrays
« Reply #3 on: 2008-08-19, 05:48:23 AM »
thank you very much for your answer. i

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Arrays
« Reply #4 on: 2008-08-19, 08:12:24 PM »
this is the version with only one error.
Code: [Select]
      enum marco_topics
      {
         tree,
         topic1,
         topic2,
         topic3,
         topic4,    // it gave errors till i inserted the comma

Good, yes, the comma is required there, and I left it out.

Code: [Select]
         count    // is there supposed to be a number? like 4 for the five topics?
      }

No, it will automatically number starting from tree = 0 up to count = 5 (there are 5 topics excluding "count").

Code: [Select]
      // Create new bit array with element count equal to enum value count, all defaulted to false
      static System.Collections.BitArray marco = new System.Collections.BitArray(marco_topics.count, false);

         marco[marco_topics.tree] = true;  // this lines gave three errors, so i wrote the next line ...
This code must be inserted in a function. It can't be directly in the class.  It's just a line of sample code.

Code: [Select]
         marco[] = true;   // => (Invalid token '=' in class, struct, or interface member declaration)
This is invalid.  You can't set a variable designed to contain an array (such as BitArray) to contain a single value (such as true).  You also can't put code like this directly in the class.  It must be in a function.

Code: [Select]
// here i would like to make a function so i can set topics true with rules.
   public static void UnlockTopic(string npcname, string topic)
     { 
       // can i make it something like this?
       npcname[npcname + string _topics + .topic] = true;  // -> marco[marco_topics.tree] = true;
      }
}
}

Once again, I don't see why you need to deal with strings.  Think about how you will be calling this function.  Are you really going to have to separate the npc name from the topic, or are they both going to be hard-coded anyway?  If they're going to both be hard-coded, the simplest thing would be this:
Code: [Select]
      enum npc_topics
      {
         marco_tree,
         marco_topic1,
         marco_topic2,
         marco_topic3,
         marco_topic4,
         polo_topic1,
         polo_topic2,
         polo_topic3,
         einsteins_ghost_wormhole,
         einsteins_ghost_time_travel,
         count
      }

      static System.Collections.BitArray availTopics = new System.Collections.BitArray(npc_topics.count, false);

      public static void UnlockTopic(npc_topics topic)
      { 
         availTopics[(int)topic] = true;
      }

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Arrays
« Reply #5 on: 2008-08-20, 02:49:12 AM »
Quote from: bluemonkmn
Good, yes, the comma is required there, and I left it out.

sorry, i didn
« Last Edit: 2008-08-20, 03:10:55 AM by Tanja »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Arrays
« Reply #6 on: 2008-08-20, 05:23:42 AM »

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Arrays
« Reply #7 on: 2008-08-20, 05:41:51 AM »
 :-[
talking-demo\Talking.cs(23,56) : error CS1502: The best overloaded method match for 'System.Collections.BitArray.BitArray(int, bool)' has some invalid arguments
talking-demo\Talking.cs(23,88) : error CS1503: Argument '1': cannot convert from 'CustomObjects.Talking.npc_topics' to 'int'


they appear where i made a //:
static System.Collections.BitArray availTopics = //new System.Collections.BitArray(//npc_topics.count, false);

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Arrays
« Reply #8 on: 2008-08-20, 07:54:45 AM »
Since C# is strongly-typed, you have to specifically tell the compiler that npc_topics.count is an integer:

static System.Collections.BitArray availTopics = new System.Collections.BitArray((int)npc_topics.count, false);
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Arrays
« Reply #9 on: 2008-08-20, 11:25:27 AM »
thanks! i got the whole thing working now!

i changed the function so i can set it true or false like i want.
      public static void UnlockTopic(topics topic, bool lala)
      { 
         availTopics[(int)topic] = lala;
      }


i did this mainly because i wanted to make if rules that ask wether a topic is set true or false. but i see, an if-rule cannot access the custom code-objects. why and what can i do about that?

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Arrays
« Reply #10 on: 2008-08-20, 12:25:20 PM »
You can just create a function that returns a bool, which should then be picked up by the IDE as a function suitable for an if statement:

public static bool isTopicUnlocked(topics topic) {
    return availTopics[(int)topic];
}


You could access it directly like you wanted to, but the IDE only lists functions in its dropdown, as it doesn't know how to convert a BitArray to a boolean.  If you wanted to, you could just type "CustomObjects.ClassName.availTopics[(int)topic]" in the if statement, where ClassName is the name of the class, and topic is one of the items in the enum.  You would also have to make the availTopics BitArray public:

public static System.Collections.BitArray availTopics...

but as you can probably tell, it's easier to just make a function to do it for you.
« Last Edit: 2008-08-20, 12:33:11 PM by durnurd »
Edward Dassmesser