Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 2 3 [4] 5 6 ... 91

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

Chandos

  • Bay Watcher
  • bork bork bork!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #45 on: May 03, 2011, 11:12:19 pm »

C# question here regarding runtime creation of objects: say I want to have a button in my game that says "Create Ball", which should do just that when clicked. I know Ball x = new Ball() would work just fine if it was a single ball. But how do I handle things when I don't know how many balls the player would create, beforehand? I suppose object arrays would help, but even then I would have to set the dimensions of the array before runtime, correct?

And when I do that like:
Ball[] x = new Ball[10];
x[0] = new Ball();
the question is what are the effects of each of the above two lines on memory allocation? Is there a way to dynamically declare an object instance at runtime as needed?
Logged

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #46 on: May 03, 2011, 11:12:38 pm »

Eh, it's actually an assignment. Any improvements would get me beaten by my crazy professor.
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #47 on: May 03, 2011, 11:16:42 pm »

C# question here regarding runtime creation of objects: say I want to have a button in my game that says "Create Ball", which should do just that when clicked. I know Ball x = new Ball() would work just fine if it was a single ball. But how do I handle things when I don't know how many balls the player would create, beforehand? I suppose object arrays would help, but even then I would have to set the dimensions of the array before runtime, correct?

And when I do that like:
Ball[] x = new Ball[10];
x[0] = new Ball();
the question is what are the effects of each of the above two lines on memory allocation? Is there a way to dynamically declare an object instance at runtime as needed?

What you need is a class called a List. There are multiple types of lists, but the List alone will work. Unlike the array, it can hold a dynamic number of things. Code looks a little like this.

List<Ball> ballList = new List<Ball>; //This will declair your list!
ballList.add(new Ball()); // This will add a ball to the list.
ballList[3].bounce(); //This will get the third item in the list, and make the ball bounce. It works just like an array in that way due to sexy indexers.


EDIT: I find it some what hard to believe I am the most professional programmer here. I don't even make games, just business applications! Where are all the pros that spend more time figuring out path finding from mathematical equations then drawing class diagrams?

Chandos

  • Bay Watcher
  • bork bork bork!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #48 on: May 03, 2011, 11:24:28 pm »

C# question here regarding runtime creation of objects: say I want to have a button in my game that says "Create Ball", which should do just that when clicked. I know Ball x = new Ball() would work just fine if it was a single ball. But how do I handle things when I don't know how many balls the player would create, beforehand? I suppose object arrays would help, but even then I would have to set the dimensions of the array before runtime, correct?

And when I do that like:
Ball[] x = new Ball[10];
x[0] = new Ball();
the question is what are the effects of each of the above two lines on memory allocation? Is there a way to dynamically declare an object instance at runtime as needed?

What you need is a class called a List. There are multiple types of lists, but the List alone will work. Unlike the array, it can hold a dynamic number of things. Code looks a little like this.

List<Ball> ballList = new List<Ball>; //This will declair your list!
ballList.add(new Ball()); // This will add a ball to the list.
ballList[3].bounce(); //This will get the third item in the list, and make the ball bounce. It works just like an array in that way due to sexy indexers.

Awesome. Thanks! And when you mean the third, you mean the fourth right? It starts from 0 like the regular indexer?
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #49 on: May 03, 2011, 11:28:18 pm »

Yes, your right. Sorry, not thinking. I'm no good at remembering how things work when I don't have a compiler in front of me, but when I do things just come naturally. Here is some working code to show exactly how it works.

Code: [Select]
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> intlist = new List<int>();
            intlist.Add(3);
            intlist.Add(2);
            intlist.Add(5);
            Console.WriteLine(intlist[1]);
            Console.ReadKey();
        }
    }
}

Output is 2.

devek

  • Bay Watcher
  • [KILL_EVERYTHING]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #50 on: May 03, 2011, 11:43:27 pm »

I wonder if there's a compiler that will actually utilize my 6 core processor...
Visual Studio, if you can hunt down the correct option in the settings labyrinth. gcc/make should also be able to. Note that multithreaded compiling is meaningful only for projects with multiple source files.

As someone with a lot of experience programming, I would advise avoiding Visual Studio while you are learning. I'm not being a fanboy or anti-ms, it is just that you might inadvertently pick up on something windows specific which would be a very bad habit. Once you have a firm grasp of the language you are free to use whatever you want, but you'll probably still avoid Visual Studio because it lacks many advanced features of the language that all the other compilers support.

On the subject of multithreading, it isn't that big of a deal because the usual bottleneck when compiling is your hard drive. A solid state drive is best. Clang is pretty amazing when it comes to compiling time/multi-threaded compiles, but unless you have a Mac with XCode you may find it difficult to set up yourself since you are just starting out. For now just stick with something that "just works". That being said the moment someone releases an IDE for other platforms that support Clang, it will be the best for beginners due to its informative error messages and code completion. Clang is the future so keep an eye out on it.

For now, when picking an IDE that "just works", it depends on what you are doing. For college work, Eclipse is probably the best. If you are writing UI apps, QT Creator is excellent. I also have a lot of friends who swear by netbeans, but I don't know.

For indentation and such, just use whatever is the most clear to you. When you work on a professional project there will usually be a style guide, http://www.freebsd.org/cgi/man.cgi?query=style&sektion=9 is an example of a professional style guide while other projects will have informal standards. If there was a standard it would be ANSI http://en.wikipedia.org/wiki/Indent_style#Allman_style TBH I don't give a damn about my style unless I am working with other programmers.
Logged
"Why do people rebuild things that they know are going to be destroyed? Why do people cling to life when they know they can't live forever?"

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #51 on: May 04, 2011, 03:33:28 am »

So... Anybody here know how to do a side scroller with destructible terrain? The idea of the game is that you have to get through each level using the level as a weapon to crush and burn enemies and such, but the problem is with how each atom of stuff interacts with each other. So take for example, I have an atom of water above an atom of stone, and I remove the atom of stone. What is the best way for the water to know it is meant to fall? Should I be doing grounded checks for EVERY SINGLE PIXEL ON SCREEN every frame? I can;t be sure, but if I had to guess I would say that would be fps killing. I could, of coarse, make my map out of a rectangle of nodes, similar to a 2d doubly linked list, and each atom class extends from this node, but without an array of atoms to check against while moving a larger object, such as our hero, things will get tricks. I could, of corse, have an array of nodes that not only link to each other, but fit into an array, so when they speak to each other and decide they have to move or change, they tell the class holding the array of nodes to update them, but something about this screams bad code.

I can think of several ways to do this, but none of them seem to be correct, and this is one of those things that unlike AI or difficulty curve, if I don't get this right the first time, it will be a mess of fixing things later. What is the 'right' way?

Depends a bit on the level of complexity you're after. If terrain is always stationary (ie not fluid) but can be destroyed, and water cannot be destroyed but should be fluid, then the best is probably a multi-pass grid simulation. An extra problem for you is that you probably don't want to run the simulation on the entire map all the time, just what's interesting presently.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #52 on: May 04, 2011, 03:51:02 am »

Well the problem extends to blocks interaction with other blocks, not just blocking them from falling. So if you pour water over magma, you form a cap of obsidian to walk over. As such, an atom will need to know what is around it, and how to react to those other atoms.

Shades

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #53 on: May 04, 2011, 04:07:21 am »

Well the problem extends to blocks interaction with other blocks, not just blocking them from falling. So if you pour water over magma, you form a cap of obsidian to walk over. As such, an atom will need to know what is around it, and how to react to those other atoms.

If you don't have many types of interaction you could use a mask like layer, one for the solid terrain level and one each for the various liquids. Anything that would change a layer, an explosion of stone for example, you check any objects around it and make them update their layers as well and repeat the surround check.

Liquids such as water could then use the solid terrain mask to work out where they need to flow and if they touch a pixel on the magma mask you replace both with a stone object.

This means you tend to have ripples of updates, which depending how deep you process each frame might be noticeable but will be a much lower load then checking every pixel or object. It's also a task that is 'easy' to multithread well.

Mixing destructible terrains with physics is one of the more processor intensive things you can do, which is why 3d physic engines and hardware exist.
Logged
Its like playing god with sentient legos. - They Got Leader
[Dwarf Fortress] plays like a dizzyingly complex hybrid of Dungeon Keeper and The Sims, if all your little people were manic-depressive alcoholics. - tv tropes
You don't use science to show that you're right, you use science to become right. - xkcd

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #54 on: May 04, 2011, 04:37:21 am »

I used a trigger system in a tree-like structure: Nothing moves of it's own accord. So whenever some voxel/pixel is changed (eg: a piece of terrain destroyed) it adds all it's neighbours (which it gets through its parent) to a global "to be checked" map. Each of those, if changed, adds all its neighbours to the "to be checked" list. Careful here that you don't get an infinite loop from this.

There's a lot of ways to do this, but most if not all are pretty CPU intensive...
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))

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #55 on: May 04, 2011, 04:46:42 am »

Or GPU intensive ^^
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #56 on: May 04, 2011, 04:56:33 am »

Hmm, so after some reading I think I can lower c/gpu use by instead of making each block a single pixel in size, I make them notably large in size, and then split into smaller and smaller as needed, so checks are only done for a few very large blocks until you blow something up. This solves one problem, and forces me out of using linked nodes.

Going to have some fun interactions between my atoms blocks and an array.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #57 on: May 04, 2011, 05:25:14 am »

So... Anybody here know how to do a side scroller with destructible terrain? The idea of the game is that you have to get through each level using the level as a weapon to crush and burn enemies and such, but the problem is with how each atom of stuff interacts with each other. So take for example, I have an atom of water above an atom of stone, and I remove the atom of stone. What is the best way for the water to know it is meant to fall? Should I be doing grounded checks for EVERY SINGLE PIXEL ON SCREEN every frame? I can;t be sure, but if I had to guess I would say that would be fps killing. I could, of coarse, make my map out of a rectangle of nodes, similar to a 2d doubly linked list, and each atom class extends from this node, but without an array of atoms to check against while moving a larger object, such as our hero, things will get tricks. I could, of corse, have an array of nodes that not only link to each other, but fit into an array, so when they speak to each other and decide they have to move or change, they tell the class holding the array of nodes to update them, but something about this screams bad code.

I can think of several ways to do this, but none of them seem to be correct, and this is one of those things that unlike AI or difficulty curve, if I don't get this right the first time, it will be a mess of fixing things later. What is the 'right' way?
Have you considered using a Quad tree yet? Large swaths of ground can be one large node that only needs updating if something happens in a neighboring node, while you can still have small-scale detail.
Edit: seems you already figured that out. (also quad trees work better as a true tree then as a structure on top of an array, due to their inhomogeneous nature. Access is in O(log n) so you don't realy have to worry about overhead)
« Last Edit: May 04, 2011, 05:27:57 am by Virex »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #58 on: May 04, 2011, 05:39:07 am »

Still have some problems involved with moving my guy through such an environment, but starting to wrap my head around it. This will be... interesting?

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #59 on: May 04, 2011, 06:36:34 am »

Does anybody know a Perl compiler and how to install it? 'cause I tried to get the lurker tracker to run and failed even getting that far.
I mostly have used Perl2exe (edit: On a machine already with a Perl interpreter, when I want to create something I can take to a machine without one), albeit that it's supposed to be paid for (at least under Windows) if used for anything beyond a trial period[1] and it's a Windows executable only, even if you're running essentially a solely DOS-session application through it.

(alsoedit: It's not so much a compiler as packaging both script, the Perl interpreter and (hopefully) any required module libraries you need into a super-sized executable.  As such, it's very inefficient compared with something properly compiled.  Just thought I'd point that out while I was editing the above... :) )


[1] The last thing I used it for was to streamline the backup of Windows 7 data without running into the whole SymLink recursion.  The whole "...\application data\application data\application data\application data\..." thing that really messes up all most attempts to copy just a single version of every data and usually hits the above recursive situation as well.  I have been able to use a "dir /al /s > file" and xcopy /s /e /h /exception:file" combo, usually, but this time it hit some kind of memory limit.  Anyway, a quick perl app and Perl2exe gave me the workable solution.  If I need it again I can always remake it (even better than before!) so I don't have to break the conditions of use.
« Last Edit: May 04, 2011, 06:46:07 am by Starver »
Logged
Pages: 1 2 3 [4] 5 6 ... 91