General Coding Help

Started by G_G, March 09, 2011, 06:00:39 pm

Previous topic - Next topic

G_G

This topic is for general help with C++ coding specifically for ARC. This is so we don't spam other topics and get them off topic. If you disagree, delete.

Alright just so I know I'm doing it right, heres Color.cpp so far.
namespace zer0
{
namespace RGSS
{
Color::Color()
{
this->red = 255;
this->green = 255;
this->blue = 255;
this->alpha = 255;
}
}
}


I've also committed an updated Color.h and Color.cpp

Blizzard

March 09, 2011, 06:09:31 pm #1 Last Edit: March 09, 2011, 06:10:54 pm by Blizzard
I have updated your code.

Advice in between: It is good practice to include as few headers in other headers as possible. This reduces recompilation time when you change something. Adding headers in source files is fine.

One trick that is also often used are forward declarations to avoid header inclusion. e.g. if you have a class pointer of a certain type , you don't need to include the header to that type. You can use forward declarations and include the header in the source file only.

Code: B.h

class B
{
public:
    int c = 0;
};


Code: A.h

class B; // forward declaration of class B
class A
{
public:
   B* aPointer;
   A();
    ~A();
};


Code: A.cpp

#include "A.h"
#include "B.h"
A::A()
{
   this->aPointer = new B();
}
A::~A()
{
    delete this->aPointer;
}
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

March 09, 2011, 06:13:49 pm #2 Last Edit: March 09, 2011, 06:16:15 pm by game_guy
Alright thanks Blizzard. ^_^

Ugh! I can't update or commit the Color.cpp. Says its in conflict. :(

EDIT: Got it. Updated Color.cpp again. Getting the hang of this C++ stuff. Can't wait to get into the graphics drawing :3

ForeverZer0

I have a really stupid question:

I'm doing the Rect class at the moment, and I was wondering if I was doing this wrong.
For instance, I have the function "setX". Should the function simply change the "x" property I created, or should it simply be a function that will call the "setPosition" method from the parent class?

I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

G_G

I'm positive it just changes the X property. In fact, I'm just as stumped as you are now that you bring it up. O_O

ForeverZer0

The Rectangle class is already included, simply creating classes with properties doesn't seem neccessary.
I imagine I should be kind of creating an intermediate class that uses the RGSS arguments and methods of the Rect class, but "translates" them into the actual C++ Rectangle class that is being used.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Ryex

just create a class with methods like

void set_x(int x)
{
    this->x = x;
}
int get_x(int x)
{
    return this->x
}

and then we wrap them in ruby.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

ForeverZer0

Okay, I had did that, then I started second-guessing myself and over-complicating stuff. Thanks.  ;)
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

Quote from: ForeverZer0 on March 09, 2011, 11:31:27 pm
I imagine I should be kind of creating an intermediate class that uses the RGSS arguments and methods of the Rect class, but "translates" them into the actual C++ Rectangle class that is being used.


Exactly. Over time the RGSS classes will be remove and instead a Ruby script (the RGSS Compatibility Script) will provide this functionality and translate them into the Zer0 classes. But for now these classes will be in C++.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

How can I make a variable named with "?" like you can in Ruby?

ex.
bool disposed?() {}
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Ryex

you can't, names can only contain A-Z, a-z, 0-9, and _ and they can't start with a number
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Blizzard

Try it and see if it compiles. xD But as Ryex said, you can't. ? is an operator.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

It's no biggie, I ended up just dropping the "?".  I was just curious. I kinda like the convention of naming boolean returning methods with a "?" at the end. 
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

March 13, 2011, 11:31:47 am #13 Last Edit: March 13, 2011, 11:44:26 am by Blizzard
I have noticed that some of you have troubles with pointers.

In Ruby every object is actually a pointer to an object. That means the object is only one and there are many references to that object. In C++ it's different. You can have reference variables and pointer variables and you can have value variables. Color, Rect and Tone should always be value variables because they should work like integral types so that you don't have to worry about destroying them and clearing memory.

If you have value variables in a class header, you have to include the header to that type or you will get compilation errors. In that case a forward declaration becomes obsolete. Forward declarations should only be used for pointer variables instead of including headers.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

G_G

Oh okay. So I was right when I was supposed to include the Color header. Oh well.

Blizzard

I just noticed something else I forgot to mention earlier. Default values for arguments are only necessary in headers. In source files you don't add them.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

Okay, I have a couple stupid questions:

  • As for pointers, I read a few things to see what I could learn. Correct me if I'm wrong, but I and I can basically add a * at the end of the type declaration in the argument to tell C++ that this is the location of this object, not the object itself. I realize that there will be differences calling the function, etc, etc, but I noticed I messed that up in some of the files I made.


  • #include  VS.  forward declaration. How does one know for sure when to use which?
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Blizzard

March 13, 2011, 03:53:03 pm #17 Last Edit: March 13, 2011, 05:11:53 pm by Blizzard
1. Yes, you pretty much got it. When you use * after the type name, it means that this variable's actual value will be literally the memory address of the object. You can also use ** and *** and even more, but double pointers are very rarely needed, not to talk about triple pointers or more.

2. When you have a type as pointer, you can use a forward declaration. When you have it as normal value type (without any *), you have to include a header. You also have to include a header of the superclass if you subclass it. In source files (.cpp) you have to include headers either way.

EDIT: Here is a useful list of similarities and differences between Ruby and C/C++. http://www.ruby-lang.org/en/documentation/ruby-from-other-languages/to-ruby-from-c-and-c-
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

ForeverZer0

Okay, really stupid question here. I noticed that we have been using the hclamp method to keep values in within range, such as values for color and tone.
Why are we keeping the opacity of things in a -255.0 to 255 range? I have been just following what others have seemed to do, but correct me if I'm wrong, but isn't the proper range from 0.0 to 255.0? I have never heard of negative alpha values before.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Ryex

it true. no negative alpha. I'm not even sure what this would imply.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />