Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 43 44 [45] 46 47 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100882 times)

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #660 on: June 24, 2012, 01:59:41 pm »

Nevermind, I am stupid. I had to use
Code: [Select]
mweapons[0] = new melee("Copper sword", 1, 6, 50);for it to work.

Can someone explain it?

I thought that with
Code: [Select]
melee[] mweapons = new melee[3];it would create an array already with values. Also it's obvious that without "new" it's a syntax error. Please tell me why or where to find out about it :).

P.S. My guess would be that's because the class is not static, is it correct? So I need to create the object with "new". I still need to wrap my head around this static/nonstatic thing, it's very new to me.
If you can show me some basic articles explaining it, it would be nice. I can understand the MSDN explanation, but it doesn't help me to use it correctly :D.
« Last Edit: June 24, 2012, 02:01:43 pm by Deon »
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #661 on: June 24, 2012, 02:17:11 pm »

Code: [Select]
melee[] mweapons = new melee[3];
Creates an array of melee objects, but it does not initialize them (after all, it only creates the array, not the object themsevles). You have to do that by hand using
Code: [Select]
for(int i = 0; i < mweapons.length; i++)
{
    mweapons[i] = new melee()
}
[\code]
Alternatively, you could use this:
[code]
melee[] mweapons = new melee[] {new melee(); new melee(); new melee()};
[\code]
« Last Edit: June 24, 2012, 02:18:46 pm by Virex »
Logged

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #662 on: June 24, 2012, 02:21:29 pm »

Try it like this?

Okay, I think I get how that works. The only problem is that in this function:

Code: [Select]
function doTransit(xOne,xTwo,xThree,xFour){
        var xTwoMove = 0;
        var xThreeMove = 0;
        var xFourMove = 0;
        var i = 0;
        if (xOne > 0) { xOne = xOne - .05; }
       
        if (xOne = 0.5) { xTwoMove = 1;}
        if (xTwoMove = 1) {xTwo = xTwo - .05;}
       
        if (xTwo = 0.5) { xThreeMove = 1;}
        if (xThreeMove = 1) {xThree = xThree - .05;}
       
        if (xThree = 0.5) { xFourMove = 1;}
        if (xFourMove = 1) {xFour = xFour - .05;}
       
        if (xFour = 0) {i = 1;}
        panelTransition (xOne, xTwo, xThree, xFour) ;
        if(i == 0){
            setInterval ("doTransit(xOne, xTwo, xThree, xFour)", 5);
        }
}

Firefox keeps yelling at me about the use of setInterval, saying 'xOne is not defined'. Is there a reason I'm missing as to why it might do that?
Logged

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #663 on: June 24, 2012, 02:52:39 pm »

Thank you Virex, you explained it really well :).

Now I seem to have another problem... Most of my problems probably come from me not understanding the Object-Oriented Programming, but I try to learn :). I wanted to move the display code from the main.cs for it to exist in a separate file and not to crowd the project, but now I do something wrong again.

main.cs: http://pastebin.com/SXsvNmwc
shopinterface.cs: http://pastebin.com/dEFN9ZcV
(those are not the only files, but the other ones should not be related I think)

The error is that "the non-static field, method or property "ShopInterface.Listitems(melee[], ranged[], armor[])" requires a reference" to the object.

It comes from the
Code: [Select]
ShopInterface.ListItems(mweapons, rweapons, armors);
Please tell me what do I do wrong because I cannot understand the reason of the error. It's the last question for today, I swear :).
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #664 on: June 24, 2012, 04:10:47 pm »

Well your problem is quite literally what the error says. You're calling ShopInterface.Listitems(melee[], ranged[], armor[]), but ShopInterface(melee[], ranged[], armor[] ) is not a static method. This means that it can only be called on an existing object, but right now you're calling it on the whole ShopInterface class (note the difference between objects or instances and classes). To fix the problem, you either need to make it static, which would tell the compiler that Listitems is a part of the general ShopInterface class and not of the instances of ShopInterface, or you need to make an instance of ShopInterface and call the method on that.
Logged

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #665 on: June 24, 2012, 04:15:03 pm »

Thanks, I will try to figure it out myself now :).

By the way, it's all to figure out a future "item list" system and mostly to experiment with arrays and classes.

Meanwhile, I've learned a better way to store the info in arrays.

I've made classes for item types like:
Code: [Select]
class Weapons
{
    //Constructor
    public Weapons(int Num) { data = new weapon[Num]; }

    public weapon GetElem(int i) { return data[i]; }
    public void SetElem(int i, weapon val) { data[i] = val; }
    public weapon[] GetArray() { return data; }

    private weapon[] data;
}

And then since I have  public weapon(weapon m) I can just use stuff like:
Code: [Select]
weaponlist.SetElem(0, itm_copper_sword);
which allows me to simply declare stuff like
Code: [Select]
weapon itm_copper_sword = new weapon("Copper sword", 1, 6, 50); which is easy for the eye.



I am sure by the end of the next week I will see how bad it is and find out something else :).
Thank you very much for your help.
« Last Edit: June 24, 2012, 04:17:23 pm by Deon »
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #666 on: June 24, 2012, 04:20:01 pm »

Firefox keeps yelling at me about the use of setInterval, saying 'xOne is not defined'. Is there a reason I'm missing as to why it might do that?
Oh yeah, that's because it's a function reference, not a string, sorry, didn't test that code :)
Two options.
Switch solution:
case Pretty: setInterval(function(){doTransit(xOne, xTwo, xThree, xFour)}, 5);
case Ugly: setInterval("doTransit("+xOne+", "+xTwo+", "+xThree+", "+xFour+")", 5);
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #667 on: June 24, 2012, 04:21:14 pm »

You know, you can use getters and setters on class variables:
Code: [Select]
private string fName;
public string Name
{
    get { return this.fName }
    set { this.fName = value }
}
and this will allow you to drop any code in the get and set function while still being able to treat it like a variable, so someclass.Name = 3 would call the setter with value = 3.
Logged

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #668 on: June 24, 2012, 04:32:49 pm »

Of course I did not know that... So you can replace the "set variable" and the "get variable" with any code? So while calling for something like Weapon.DisplayName="CopperSword" I could run a whole code showing it on the screen? Wow...

As I said I've just started studying C#, and it's the functionality which is not yet known to me. I feel great urge to learn more to find out more awesome stuff I can make :).

P.S. Do I set it within class and does it apply to any "string" variable (in your case)?
« Last Edit: June 24, 2012, 04:37:36 pm by Deon »
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #669 on: June 24, 2012, 05:09:42 pm »

Okay, here's the last question which will greatly help me to understand how to organize and reference classes and variables within my program.

Here's the cleaned up version of the program:

main.cs: http://pastebin.com/Qmu2H94V
itemlistdef.cs: http://pastebin.com/dAx2wqkv
itemtypedef.cs: http://pastebin.com/5xHSpNre

Can someone as kind as Virex help me with moving the "calling the lists" from the main class to a separate class so it could be called from any other class?

Namely the
Code: [Select]
            Console.WriteLine("--[WEAPONS]-------------[$]----[Dmg]---------------");

            i = 1;
            foreach (weapon element in weaponlist.GetArray())
            {
                Console.WriteLine("{0}. {1}\t\t{2}\t{3}-{4}", i, element.name, element.price, element.mindamage, element.maxdamage);
                i++;
            }
            Console.WriteLine("--[ARMORS]--------------[$]----[AC]----[DV]--------");

           
            i = 1;
            foreach (armor element in armorlist.GetArray())
            {
                Console.WriteLine("{0}. {1}\t\t{2}\t{3}\t{4}", i, element.name, element.price, element.protection, element.dodge);
                i++;
            }

            Console.ReadKey();
part.

I've tried to do it in different ways, but I seem to be unable to reference "weaponlist" and "armorlist" correctly. I just want to be able to call this whole code as a separate function whenever I want.

Don't worry, I am finally going to bed. But if you can give me an answer or a hint, it would be most appreciated. Now I better take a nap, I cannot think when it's that late (2 AM :P).
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #670 on: June 24, 2012, 07:31:28 pm »

Firefox keeps yelling at me about the use of setInterval, saying 'xOne is not defined'. Is there a reason I'm missing as to why it might do that?
Oh yeah, that's because it's a function reference, not a string, sorry, didn't test that code :)
Two options.
Switch solution:
case Pretty: setInterval(function(){doTransit(xOne, xTwo, xThree, xFour)}, 5);
case Ugly: setInterval("doTransit("+xOne+", "+xTwo+", "+xThree+", "+xFour+")", 5);

Thank you!
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #671 on: June 25, 2012, 05:37:45 am »

snip



You can copy the code almost verbatim. Put the following in your Itemlist class
Code: [Select]
public static void print_items(Weapons weaponlist, Armors armorlist)
{
Console.WriteLine("--[WEAPONS]-------------[$]----[Dmg]---------------");

            int i = 1;
            foreach (weapon element in weaponlist.GetArray())
            {
                Console.WriteLine("{0}. {1}\t\t{2}\t{3}-{4}", i, element.name, element.price, element.mindamage, element.maxdamage);
                i++;
            }
            Console.WriteLine("--[ARMORS]--------------[$]----[AC]----[DV]--------");

           
            i = 1;
            foreach (armor element in armorlist.GetArray())
            {
                Console.WriteLine("{0}. {1}\t\t{2}\t{3}\t{4}", i, element.name, element.price, element.protection, element.dodge);
                i++;
            }

            Console.ReadKey();
 }
and call it from the main function using
Code: [Select]
Itemlist.print_items(weaponlist, armorlist);
Since print_items is static, we can call it on the Itemlist class. I think you tried implementing a nonstatic variant, which does not work, because Itemlist is not an object, it's a class. That means only static functions can be called on it, for nonstatic functions, you need to instantiate an Itemlist object using the new operator and call the function on that. Instances are real objects that have their own variables while classes are recepies for making these Instances.

There are some other nonideomatic parts of your code, so I also took the liberty to rewrite it in the way I'd write it. Take a look and see if it makes sense to you.
main.cs: http://pastebin.com/Jj34G0wb
itemlistdef.cs: http://pastebin.com/CDj2zWhf (for some reason the alignment of the comments is out of whack here)
itemtypedef.cs: http://pastebin.com/V0RgHHaG
« Last Edit: June 25, 2012, 06:10:30 am by Virex »
Logged

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #672 on: June 25, 2012, 10:53:12 am »

Thank you very much! My problem was that I did not use a static method, that's the error I keep making.

P.S. What is "using System.Collections;" in the main.cs for?
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #673 on: June 25, 2012, 11:31:30 am »

Erm, that's a leftover from an older version in which I used ArrayLists for the weapons and armor. I figured it wasn't necessary for the example since you already know how many different types of items there are.
Logged

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #674 on: June 25, 2012, 11:38:36 am »

So if I want to make a dynamic array, should I check this Collections class?
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository
Pages: 1 ... 43 44 [45] 46 47 ... 91