Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 337 338 [339] 340 341 ... 796

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

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5070 on: September 29, 2013, 02:42:52 pm »

So does that only work if the parameter used to call execute() is data?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5071 on: September 29, 2013, 04:09:39 pm »

You need to pass both the entire token list and the data map to execute().
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5072 on: September 29, 2013, 04:11:27 pm »

I know, I mean for the & thing to work does the thing it's referencing need to be a parameter used when calling that function? I don't know if I'm making sense.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5073 on: September 29, 2013, 04:20:30 pm »

The parameter is not optional, if you mean that. If not, give me a line of code and I'll tell you if it's valid or not.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5074 on: October 01, 2013, 07:59:30 am »

The addCommand function isn't working. When I call it using "new SmeltCommand()" as a second parameter, it says "'CommandController::addCommand' : cannot convert parameter 2 from 'SmeltCommand *' to 'Command *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

Is this because SmeltCommand is the name of the class, not the function?

Here's all of the program: http://pastebin.com/Gs2cA8hM

I also think I need a better loop to read through the inputs.
Logged

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5075 on: October 01, 2013, 08:01:56 am »

It's because you forgot to make SmeltCommand derive from Command.
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5076 on: October 01, 2013, 08:13:12 am »

Here's how you should start your SmeltCommand class:
Code: [Select]
class SmeltCommand: public Command {
Also, you should probably change this line in the Command class:
Code: [Select]
void execute(vector<string> & tokens, map<string, int> & data) = 0;
to
Code: [Select]
virtual void execute(vector<string> & tokens, map<string, int> & data) = 0;

Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5077 on: October 01, 2013, 11:52:54 am »

Code: [Select]
virtual void execute(vector<string> & tokens, map<string, int> & data) = 0;
What does that do?
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5078 on: October 01, 2013, 12:50:53 pm »

It's polymorphism in action. A method marked as virtual can be overridden by classes inheriting from that method. Basically, if you have a class (like ForgeCommand) that derives from another class (like Command), then you can store pointers to ForgeCommand objects in variables of type (Command *). Let's setup an example:
Code: [Select]
class Animal {
  public:
    string speak {
      return "This animal can't speak";
    }
};

class Cow: public Animal {
  public:
    string speak {
      return "mooo";
    }
};

class Bird: public Animal {
  public:
    string speak {
      return "tweet";
    }
};

Now let's do some stuff with those classes:
Code: [Select]
Cow cow;
Bird bird;
Animal * r_cow = &cow;
Animal * r_bird = &bird;
cow.speak(); // "mooo"
bird.speak(); // "tweet"
r_cow->speak(); // "This animal can't speak"
r_bird->speak(); // "This animal can't speak"

Now what do we see here? First of all, we can store a pointer to a Cow object in an (Animal *) variable, namely r_cow. But if we call r_cow->speak(), we see that Animal::speak() happens, because r_cow only knows that it has an Animal. Now let's change the Animal class a bit. More specifically, we'll change "string speak {" to "virtual string speak {". This is an important change, because r_cow->speak() now no longer means "Hey r_cow, use your speak() method on whatever you're currently holding", but "Hey r_cow, make whatever you're holding use its own speak() method". Let's see what happens when we run the above code again:

Code: [Select]
Cow cow;
Bird bird;
Animal * r_cow = &cow;
Animal * r_bird = &bird;
cow.speak(); // "mooo"
bird.speak(); // "tweet"
r_cow->speak(); // "mooo"
r_bird->speak(); // "tweet"

That's much better, right? Now we can call speak() on any pointer to an Animal, and the animal will call its own speak() method.
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5079 on: October 01, 2013, 01:05:46 pm »

Thank you very much, that was a good explanation.

I'm getting errors now when I try to compile the program and I have no idea what's causing them.
Spoiler (click to show/hide)

http://pastebin.com/BnYFkcKk
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5080 on: October 01, 2013, 01:39:48 pm »

Code: [Select]
class Fox: public Animal {
  public:
    string speak() {
      return "Unknown.";
    }
};

As for those errors, the first is due to the fact that static members of a class are only declared in the class. Add "map<string, Command*> CommandController::commands;" somewhere in the global scope (outside of any functions and classes) after the CommandController class. Right before Holdings would work.

The second is due to the compiler expecting a definition of execute in the base class. Make it a pure virtual function by adding " = 0" before the semicolon.
« Last Edit: October 01, 2013, 01:43:54 pm by Mego »
Logged

MrWillsauce

  • Bay Watcher
  • Has an ass that won't quit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5081 on: October 01, 2013, 09:24:01 pm »

Okay,thanks. I still need to fix that bug I mentioned earlier that Olemars said was caused by me trying to take more strings out of 'tokens' than there actually are. I don't really understand how that's the case or how to fix it.
Logged

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5082 on: October 02, 2013, 01:48:43 pm »


I...what...

Code: [Select]
    // GEN HEIGHTS
    for (int y=0; y < Height; y++) {
        for (int x=0; x < Width; x++) {
            // BluhBluh
            ImageData[ToNode(x, y)] = SDL_MapRGBA(TempSurface->format, pHeight, pHeight, pHeight, 255);
        }
    }

    // CONVERT TO TEXTURE
    for (int y=0; y < Height; y++) {
        for (int x=0; x < Width; x++) {
            putpixel(TempSurface, x, y, ImageData[ToNode(x, y)]);
        }
    }
Causes the above image.

Problem is that when I put the putpixel() into the upper loop, it works fine. The result should be completely identical. I copy pasted the loops themselves, so they should be completely identical, but somehow they arent.

EDIT: Wait, what? It shouldn't even be working at all. ToNode is the wrong function. How the hell does it work when I put putpixel() into the upper loop...
EDIT2: That fixed it. I have no idea why it worked earlier. Maybe the compiler did some shenanigans for efficiency and fixed a bug in the process?
« Last Edit: October 02, 2013, 02:01:13 pm by cerapa »
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #5083 on: October 02, 2013, 08:02:11 pm »

I've had a similar result with Allegro before. In my case it was because put_pixel() used the pointer data for colors.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #5084 on: October 03, 2013, 02:41:58 pm »

Spoiler (click to show/hide)
Causes the above image.

Problem is that when I put the putpixel() into the upper loop, it works fine. The result should be completely identical. I copy pasted the loops themselves, so they should be completely identical, but somehow they arent.

EDIT: Wait, what? It shouldn't even be working at all. ToNode is the wrong function. How the hell does it work when I put putpixel() into the upper loop...
EDIT2: That fixed it. I have no idea why it worked earlier. Maybe the compiler did some shenanigans for efficiency and fixed a bug in the process?

If the loop originally looked like this,
Code: [Select]
    // GEN HEIGHTS
    for (int y=0; y < Height; y++) {
        for (int x=0; x < Width; x++) {
            // BluhBluh
            ImageData[ToNode(x, y)] = SDL_MapRGBA(TempSurface->format, pHeight, pHeight, pHeight, 255);
            putpixel(TempSurface, x, y, ImageData[ToNode(x, y)]);
        }
    }

it is equivalent (from the compiler's perspective) to this,
Code: [Select]
    // GEN HEIGHTS
    for (int y=0; y < Height; y++) {
        for (int x=0; x < Width; x++) {
            // BluhBluh
            uint32 color = SDL_MapRGBA(TempSurface->format, pHeight, pHeight, pHeight, 255);
            ImageData[ToNode(x, y)] = color;
            putpixel(TempSurface, x, y, color );
        }
    }
So that would always* pass the correct value to putpixel, while the contents of ImageData will get all jumbled depending on what ToNode actually returns. When you split it into two loops, putpixel got the jumbled ImageData passed to it instead.

Why would you want to split it into two loops though?


Spoiler: * (click to show/hide)
Logged
Pages: 1 ... 337 338 [339] 340 341 ... 796