Learning with the TMW code

Talk about anything, including games and servers not affiliated with The Mana World.
Post Reply
User avatar
Ces
Novice
Novice
Posts: 231
Joined: 19 Mar 2008, 22:46
Location: The hemisphere

Learning with the TMW code

Post 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?
User avatar
Crush
TMW Adviser
TMW Adviser
Posts: 8046
Joined: 25 Aug 2005, 16:08
Location: Germany

Re: Learning with the TMW code

Post 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++).
  • former Manasource Programmer
  • former TMW Pixel artist
  • NOT a game master

Please do not send me any inquiries regarding player accounts on TMW.


You might have heard a certain rumor about me. This rumor is completely false. You might also have heard the other rumor about me. This rumor is 100% accurate.
User avatar
Crush
TMW Adviser
TMW Adviser
Posts: 8046
Joined: 25 Aug 2005, 16:08
Location: Germany

Re: Learning with the TMW code

Post 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.
  • former Manasource Programmer
  • former TMW Pixel artist
  • NOT a game master

Please do not send me any inquiries regarding player accounts on TMW.


You might have heard a certain rumor about me. This rumor is completely false. You might also have heard the other rumor about me. This rumor is 100% accurate.
User avatar
trapdoor
Novice
Novice
Posts: 216
Joined: 18 Feb 2007, 12:36

Re: Learning with the TMW code

Post 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
User avatar
Ces
Novice
Novice
Posts: 231
Joined: 19 Mar 2008, 22:46
Location: The hemisphere

Re: Learning with the TMW code

Post 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.
User avatar
Crush
TMW Adviser
TMW Adviser
Posts: 8046
Joined: 25 Aug 2005, 16:08
Location: Germany

Re: Learning with the TMW code

Post by Crush »

When you have any more questions feel free to ask.
  • former Manasource Programmer
  • former TMW Pixel artist
  • NOT a game master

Please do not send me any inquiries regarding player accounts on TMW.


You might have heard a certain rumor about me. This rumor is completely false. You might also have heard the other rumor about me. This rumor is 100% accurate.
User avatar
Ces
Novice
Novice
Posts: 231
Joined: 19 Mar 2008, 22:46
Location: The hemisphere

Re: Learning with the TMW code

Post 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. ;)
Post Reply