Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 54 55 [56] 57 58 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887305 times)

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #825 on: January 23, 2012, 10:53:12 pm »

Error: expected return type after 'public'

Joking aside, this is great for anyone stumbling their way through programming. The difference between an expert programmer and a novice is that the expert knows stumbles are good.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #826 on: January 23, 2012, 10:56:50 pm »

Why does OpenGL have to have no good tutorials for it... Things keep breaking.
Ah well, might as well take a break and get a bit of work done on some other work. The Camera class needs checking.

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #827 on: January 23, 2012, 11:00:35 pm »

Otherwise, things get a little trickier. The idea behind it all was that a creature doesn't have to know what its AI is doing, instead its AI figures out what it should be doing, but you can find it with this.

Code: [Select]
foreach(AIType ai in AIStack)
{
if(ai is FleeAI)
{
ai.Update()
}
}

Yeah, I want to be able to discretely add Subclass objects to the Superclass List<> (i.e. in no set order), find Subclass objects of a specific type, and then call Methods that only that Subclass possesses.  I realize this is kinda complicated and there might be an easier way to do it, but I hope you can see what I'm getting at.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #828 on: January 23, 2012, 11:05:52 pm »

Ah, I think I get what you mean. Not 100% sure you are looking at this the right way though.
Got an example of what you want so far?

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #829 on: January 23, 2012, 11:09:38 pm »

Okay, think of it this way.

I've an Object (think an NPC) for "Mutant", and Mutants have a randomly filled List<> of "Bodypart" objects.  Bodypart is a Superclass, with a variety of different Subclasses with different unique methods.  I want to be able to search through a Mutant's Bodypart list, and fire off methods unique to any given particular type (with the assurance that the List will never contain more than one of that type).

Any idea how I should go about this?
« Last Edit: January 23, 2012, 11:22:03 pm by Aqizzar »
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #830 on: January 23, 2012, 11:35:21 pm »

Ah, so you are making Elona? Well why didn't you say! Umm, it could be done with exceptions, but I'm pretty sure this is faster.

Spoiler (click to show/hide)

That was a fun little test!

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #831 on: January 24, 2012, 12:05:50 am »

Ok, while we are here, time for exceptions.
Trust me, if you were crying over how much easier your life would be now if you had known about enums from the start, might as well grab a gun now.

Let's make a simple program to take an array of people and print their names.

Spoiler: BAM!!! (click to show/hide)
Run that, see how it works, enjoy!
But wait, we forgot somebody! Emily wants to be in our array, so let's add her.

Spoiler (click to show/hide)

Try running that, what happens? Well things break. If you are working in visual studio then "people[3] = new Person("Emily");" will be highlighted and you will be getting some sort of message saying "IndexOutOfRangeException was unhandled", well what the fuck does that mean? Is the computer sexist? No, just logical, a different kind of discrimination!
We all know that a Person[3] array can only hold 3 people, starting at 0, that means 2 is the highest value it should go to. When you try to go beyond that, it 'throws' an exception.
An exception is what happens when something goes wrong, or breaks the rules in some way, and if you don't handle them then they crash your program. Luckily for us, when an exception is thrown, it can be 'caught' before it escapes the scope of your program all together and fucks some shit up. Let's catch an exception!

Spoiler (click to show/hide)

Hey look, it runs again! When we tried to add Emily, it broke, but then we caught the exception and things kept going.
Now the first thing to notice is the try{} block. Every catch starts with a try. Basically your catch block will only catch exceptions that happen inside it's try block, so for example.

Spoiler (click to show/hide)
Because the exception happens outside the try block, it does not get caught, and the program crashes. We can also put multiple things inside the try block.

Spoiler (click to show/hide)
Once again, run this, and you will find it working as you would expect by now.
Now, Brian is a pushy little SOB and wants to be added to the array first. He dosn't care what position he ends up as in the array, as long as he is the first in. Let's shut this baby up.

Spoiler (click to show/hide)
Once again, the program works the exact same way, no change, and all is fine in the world.
Now Emily decides she wants to go first in the array! As if she hasn't caused enough problems, seriously, she is getting annoying. But hey, her name hasn't shown up in our program yet, so we owe her this much. Move her up.

Spoiler (click to show/hide)
Now run that and... OH FUCK YOU EMILY! All you do is cause issues! What the fuck now?
Ok, so this time we are getting a null pointer exception. Those show up when you are trying to access something with no data in it yet. Why is there no data? We added Adam, Brian and Charlie, right? Well while the program is still running, hover your mouse over people, and click the little plus sign. This will show you what people contains (By the way, this works for all types, not just arrays, it is fucking awesome), like so.
Spoiler (click to show/hide)
Hey look, all our elements are null! Adam, Brian and Charlie never made it into the array? This is because once an exception is thrown, it breaks out of the try block, and into the catch block, and then goes from there, any lines of code that are inside the try block after the exception are ignored. This is why you can't just surround your entire program in a try catch block, as soon as something goes wrong, it will smash a sledge hammer through your walls of code, and it will be just as good as crashing for real.

Now, let's put Emily back in her place, where the damage she did was minor. But also, what if we want something to happen only if an exception was thrown? Well then we can put that code inside the catch block!
Spoiler (click to show/hide)
Try that with and without Emily. When you try to add the fourth element to the array, an exception is thrown, caught, and then the catch function is called. Otherwise it skips over the catch block and continues on.

Continues in part 2....

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #832 on: January 24, 2012, 12:34:05 am »

Spent today learning how to do spritesheets in SDL, came up with a nice little snippet of code which I've saved and now can use for any other program I ever need sprites for, so it was a day well spent.
all thats left before the first beta of my game is for me to write algorithms for 9 or trajectories,
I think if I knew more c++ i'd be able to come up with an easier way of doing this, but w/e.  spent a few hours today thinking of how to do it and had a eureka moment and thought of a way of doing it using a series of sine waves.  wrote it all down.  Gonna mess around with it tomorrow
Basically I need 9 arcs that  start from (x,y) 10,353, 90, 353, and 170, 353 and end at 530,353, 450,353, and 370, 353.
that all take the same amount of time to complete.
so instead of wracking my brain doing the math I'm going to spend tonight working on the sprites.
made a 3d ball model and textured it / shined a light on it and took a video of it spinning, and am converting it to a 14 frame animation. having to edit them frame by frame and is time consuming.   I'm not the best artist, after I'm done with the beta I'll look around to see if anyone wants to do some graphical mods for it.
Pic of current version in spoiler
Spoiler (click to show/hide)

edit was due to the /img tag being in the wrong place and causing the paragraph after the picture in the spoiler to not show up.
« Last Edit: January 24, 2012, 01:02:41 am by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #833 on: January 24, 2012, 12:56:47 am »

Part 2: Its a thing?
Here is the thing, exceptions are a type of object, just like Person. You can manipulate them and pass them around. So let's get out hands on a tasty exception.

Spoiler (click to show/hide)

After out catch block, we have a type, and then a name. This is similar to the parameters of a method, we can now use access and manipulate the exception that was thrown! In this case, it has a message for us, a message that you can print out.
Oh look, Emily seems upset, I think using her as the example butt monkey has wet her mood and she has stormed off. Well good, with her gone we don't need to try and put her in the array, so remove that.
In the meantime, Adam and Charlie have got into a debate. Adam thinks you can divide by zero, while Charlie says you can not! Well lets settle this on the field of code!

Spoiler (click to show/hide)
Now, if we can divide by 0 then it should print that out first, otherwise an exception will come up before the print line, and it will print nothing, not even an empty line, there will be no change to the program.
Try running that and... Oh great, it crashed the program! Why would it crash when we have it inside a try catch block?
Well that is because when we started, we weren't catching any specific type of exception, but when we added (IndexOutOfRangeException ex), we made out try catch block work specifically for that type of exception. It will not catch divide by zero exceptions.
But, we can fix it! We can add another catch block, and we don't even need another try block this time! In fact, let's try to make our program catch all sorts of exceptions, like so!

Spoiler (click to show/hide)

There, all types covered! But wait, the compiler is trying to tell us something... Red lines? Under my types? It is more likely than you think! The problem is that the second two types are both subtypes of exception, so when an exception is thrown, no matter what type, it would get caught at 'Exception' and not get a chance to go down to 'DivideByZeroException'. Because of this, we have to put the most generic type last, like so.

Spoiler (click to show/hide)

And oh look, turns out we can't divide by zero, suck shit Adam!

Continues in part 3

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #834 on: January 24, 2012, 01:00:56 am »

sorry for posting between your things, was just unlucky timing.
and you should put the just in spoilers, instead of inside code tags inside spoiler.
if it's in code tags I cant read it without copy/pasting
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #835 on: January 24, 2012, 01:30:01 am »

Part 3: An exception to call our own!
If exceptions are things, does that mean we can make them? Fuck yea it does!

Let's clean up all that crap we had before and start again with something a little new. Don't delete your person class, just what is inside the main.
Spoiler (click to show/hide)
Don't worry that there is no try catch stuff yet. We might have made a new exception, but we didn't throw it yet. They don't get deadly until you start to throw them around.
SO LET'S THROW THIS FUCKER!!!

Spoiler (click to show/hide)
As you can see, it crashed your program, as all non-caught exceptions do. Now, while the others are away, I would like to introduce my new friend 'Fo', 'Fo' is a stupid name and has no right to be a thing. In fact, all names must be at least three letters long, as a rule. But in our program you could easily make a new person called 'fo', or any other dumb ass name. Let's fix that!
Back in the person class

Spoiler (click to show/hide)
Now let's see Fo try to exist!
Spoiler (click to show/hide)
Bam, crashed. Fo, you suck so bad we can't even make you a person.
Now throwing random exceptions is one thing, but wouldn't it be nice to have our own type of exception is throw? Well we can! By making a new class that extends Exception. for it. Observe!
Spoiler (click to show/hide)
Now let's upgrade our person class to throw this new sort of exception.
Spoiler (click to show/hide)
And finally, our main
Spoiler (click to show/hide)
And then what if we want to print their name after we make them? Let's try it.
Spoiler (click to show/hide)
Crap, the compiler is yelling at us again. What now?
Turns out that try catch blocks work like ifs and loops, anything you declare inside them isn't accessible outside the try catch block.
And finally, we come to finally. a try catch block can have one more competent, and that is a 'finally' block. It is pretty useless in our example, but what you need to know is that a finally block will run after the try catch block, and has access to anything you declare inside the try catch. It is used to 'clean up' after your try part, in that it can be used to close file streams and connections. Not must utility for you yet, but someday...

Any questions?

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #836 on: January 24, 2012, 05:00:30 am »

sorry for posting between your things, was just unlucky timing.
And then you did it again! ;)

Your project looks cool, and feasible! I'd just start with left-right, you can always add the upwards motion later, I guess...
Your arcs are parabolas (y=ax^2+bx+c). I think you can easily solve those if you pretend that y =0 for start and end (protip: a is negative)... I think?
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))

Xegeth

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #837 on: January 24, 2012, 06:24:23 am »

Aqizzar, I don't know if you've solved your problem with MeleeAttack, but I've had a look at your code and I think I've found the problem. RemoveMob is broken because "mob.mobX == yPos" should be "mob.mobY == yPos". The reason they aren't losing hitpoints is because you don't reduce them if the enemy should be killed by an attack.

If you have similar problems in the future, where some code isn't working the way you expect, one thing you can do is use a debugger. This fix took literally minutes because I just had to step through the code until something broke.
Logged

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #838 on: January 24, 2012, 06:50:25 am »

I'll be slogging through Max's latest tutorials soon, but I actually already knew about Exception catching.  Me and errors are good friends after all, even if my project is not currently using any try/catches.

Aqizzar, I don't know if you've solved your problem with MeleeAttack, but I've had a look at your code and I think I've found the problem. RemoveMob is broken because "mob.mobX == yPos" should be "mob.mobY == yPos". The reason they aren't losing hitpoints is because you don't reduce them if the enemy should be killed by an attack.

Y'know what the really weird thing is?  I had absolutely no idea that typo was there, and it had zero impact on the functionality.  When all the "attack" function did was remove a target, it always removed the right target.  What the Heck man.

But even so, and thanks, that's not the issue.  The issue is, MeleeAttack does print the currentHP of the attacker and target on the screen as a debugging function, and you can clearly see player-on-npc attacks doing nothing while npc-on-player attacks work fine for reducing currentHP.  It has to be a class problem.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Xegeth

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #839 on: January 24, 2012, 07:46:07 am »

Unless your code has changed a lot from the version you put online, it looks like it's working like it should other to RemoveMob not working.

Code: [Select]
if (DAM > BLK) //5 > 3 so this is true
if (target.currentHP > DAM) //3 < 5, so the next line is skipped
target.currentHP -= (byte)DAM;
else //Now doing this section. currentHP is still 3
if (target is MobObject)
RemoveMob(target.mobX, target.mobY); //Removing the mob. This would only work if mobX == mobY
//If this does work, hitpoints will print as 3 and the target will be removed from the game
//If not, hitpoints will remain at 3 and the enemy will remain in the game

If you set target.MaxHP to 20, you should get it working up until currentHP is 5. If you want it to print what's going on a bit better, you could use
Code: [Select]
if (target is MobObject)
{
target.currentHP = 0;
RemoveMob(target.mobX, target.mobY); //Removing the mob. This would only work if mobX == mobY
}
Logged
Pages: 1 ... 54 55 [56] 57 58 ... 796