Page 1 of 1

Learning with the TMW code

Posted: 24 Oct 2008, 12:14
by Ces
Well, I only know the basics of coding in a couple of languages, so I figured to try to delve some more into this. By working with the TMW code. Here are some questions, which you’re free to answer. ;) At the moment I’m working with the 0.0 client.

src/gui/item_amount.cpp:

Code: Select all

amount = static_cast<int>(mItemAmountSlide->getValue());
src/gui/sell.cpp

Code: Select all

mAmountItems = (int) mSlider->getValue();
Questions
  • Variable or class member, which is preferred? Or, is there a reason to use the one in one place but not in the other? (amount vs mAmountItems)
  • How to use one datatype as another? (static cast vs “whatever it is named”)
In general, the item_amount.cpp file looks quite different in style than the other GUI source files I have been inside. The changes I have done work in any case, the above questions are just a curiosity to me.

EDIT (12.57 UTC)
I think I figured out the first question, it seems to be all about scope. I tested to add a function (private class method) and suddenly I believe I realised some things...

EDIT (13.26 UTC)
New question:
• When I tried to make changes in scope and do things in a more advanced way, TMW segmentsfaults. :lol: Is there a way to check where this happens, without adding a lot of write-to-output-snippets in the code?

Re: Learning with the TMW code

Posted: 24 Oct 2008, 13:28
by Crush
(int)stuff and static_cast<int>(stuff) do the same - converting a numeric variable of any type to an integer. Which one to use is mostly a question of personal preference.

In a project like TMW where a lot of people worked on the code over a long period of time you will frequently see different coding styles in different files.

Regarding members vs. variables: a local variable in a function is only visible inside the function and when you leave the function and call it again the variable is reset. A member variable, on the other hand, retains its state between calls and is visible inside the whole object (and even from outside the object when it is declared public, but declaring class members as public is bad style in C++).

Re: Learning with the TMW code

Posted: 24 Oct 2008, 13:38
by Crush
When I tried to make changes in scope and do things in a more advanced way, TMW segmentsfaults. Is there a way to check where this happens, without adding a lot of write-to-output-snippets in the code?
Yes, there are programs called Debuggers for this purpose. Which one can be used and how it is used depends on the platform and development environment you are working with.

Re: Learning with the TMW code

Posted: 24 Oct 2008, 17:57
by trapdoor
The first is a c++ style cast, and the second is a c style cast.

C++ is preferred, as with C it can be dangerous as you can overwrite the bounds of memory.

Read http://www.parashift.com/c++-faq-lite/c ... #faq-27.11 for more information on typecasting.

--
trapdoor

Re: Learning with the TMW code

Posted: 25 Oct 2008, 23:52
by Ces
Thank you for the answers! Using gdb as debugger helped me understand where the problem was, but I still need to learn how to use it to understand the details. Anyway, I solved what I wanted, and that’s enough for now.

Re: Learning with the TMW code

Posted: 25 Oct 2008, 23:56
by Crush
When you have any more questions feel free to ask.

Re: Learning with the TMW code

Posted: 26 Oct 2008, 00:05
by Ces
*smiles devilishly* :twisted: That’s why I gave this thread a general topic, so I could come back here with more questions. While I doubt I will be of any help in coding new features, I might be able to help with fixing minor issues, tweaking the gui or porting existing stuff between trunk and branch(es).

Fate, thank you again for the marvelous lesson on binary and bitwise thinking you gave me a few days back, it made me want to know more. ;)