Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


ChaosEngine

Member Since 09 Jun 2000
Offline Last Active Dec 12 2014 12:46 PM

#5195103 Multiple Inheritance or lots of pointers?

Posted by ChaosEngine on 27 November 2014 - 10:24 PM

Ask yourself a simple question when deciding to use inheritance: is a dog an animal, or does a dog have a tail?

 

So in your case, an entity is not a mesh, it's not physics data, etc. But it does have those things.

 

In general, this is what's know as "favouring composition over inheritance".

 

edit: damnit, ninja'd




#5194352 Building A Game Engine + Game

Posted by ChaosEngine on 23 November 2014 - 09:19 PM

Now, if you're talking about using C++ effectively/efficiently, then I'd partially agree.  For me, learning C++ wasn't so hard, but learning to use it properly was a bigger challenge, one that I'm still learning as the language evolves.

Well, I wasn't talking about writing "hello, world".

Learning to use it == learning to use it properly.
The very fact that you feel you have to differentiate between the two is symptomatic of the issues with c++.

Maybe you were some kind of c++ savant, but I've seen hundreds of beginner and junior programmers make mistakes in C++ that just don't happen in other languages. It is demonstrably difficult to learn for most people. If it wasn't, we wouldn't see those kinda of threads.


#5193162 Building A Game Engine + Game

Posted by ChaosEngine on 16 November 2014 - 06:53 PM

 

 


Its undefined because you haven't assigned a value to it. In reality it will be some value that was left in the stack from a previous local variable in some other function call.

While the question is simple, the correct answer is quite a lot more complicated than that (although more information would be required to accurately answer a specific "version" of the question).

The short answer is "It depends on where the line of code is written", followed by a number of different scenarioes with different rules & logic for the corresponding value.

 

 

However, if you are going by the language specification then you are to assume that gibberish is stored there.

We are writing C++ code, not coding a compiler.

 

 

 

I'm not trying to be mean, but this is textbook dunning kruger. You don't even realise what you don't know. Don't feel bad, C++ is a difficult language and most people don't understand all the corner cases.

 

I suggest you watch this video. If you still think C++ isn't hard to learn, you're either a genius or you haven't got experience to realise you're wrong.




#5193137 Building A Game Engine + Game

Posted by ChaosEngine on 16 November 2014 - 03:15 PM

C++ isn't hard to learn. Thats just a lie people use to sell new programming languages.

 
In that case, please answer the following simple question:
 
given the following code

int x;

what is value of x?

 

Maybe a few in this world are, like 2%-3% of the population.


eh? There are probably 20-30 million people (about 0.004% of the population) who actually write code in the whole world! 

Of those, there are probably around 2-3 million C++ programmers and of those, maybe 2-3% of them are experts, if that.

 

I would actually guess that the number of expert c++ programmers in the world is > 10000.




#5192348 Building A Game Engine + Game

Posted by ChaosEngine on 11 November 2014 - 08:25 PM

Before you even contemplate writing a game or an engine, you need to learn to program.

 

There are plenty of languages that are good for beginners; personally I'd recommend python or C#.

 


Call me old fashioned, but only wimps have to rely on an engine (and tbh, I believe that many devs in this day and age with such easy access to middleware engines lack the talent/skill to do it themselves, nor are they interested in learning to do so)

 

Yes, everyone should reinvent the wheel.... rolleyes.gif

 

Honestly, when was the last time you saw someone telling a chef they should build their own kitchen?




#5189575 function that returns class template

Posted by ChaosEngine on 27 October 2014 - 08:06 PM

Don't use underscores as a suffix prefix. note to self: don't post without coffee.

  • Reserved in any scope, including for use as implementation macros:
    • identifiers beginning with an underscore and an uppercase letter
    • identifiers containing adjacent underscores (or "double underscore")
  • Reserved in the global namespaces:
    • identifiers beginning with an underscore



#5188729 C++ header inclusion question

Posted by ChaosEngine on 23 October 2014 - 06:18 AM


Care to elaborate how this is a bad thing? I never ever had an error directly related to forward declaration, and having depencies spread is really a good thing - now if I have to include "X.h" into "Y.h" I can be asured that Y only has the barely minimum depencies that X relies on in its declaration, mostly due to templates/typedefs. Instead of having Y.h now suddenly contain every single include that might be there in the include-chain, which gives you nothing but increased compile times. Even with #pragma once, having all header-files included in other header-files will still lead to recompilation of half your project if you make a change to the header of certain classes.

 

Essentially DRY.

 

If you have a class (or more likely templated typedef) that you commonly forward declare, it's a good idea to put the forward declaration in a separate header file (AwesomeClass_fwd.h) and include that. 

 

As far as SeraphLances advice is concerned, I would say that including headers in headers is common enough to not be considered a deviation.

 

It's like saying "when driving, it's safest to stay below 20mph". It's certainly true, but not really practical. You're going to break that rule as soon as you want to do anything non-trivial. 




#5187032 Problem with List of inherited objects

Posted by ChaosEngine on 14 October 2014 - 03:48 PM

First of all the word is "archetype". Pedantic, I know, but code is communication and concepts matter.

 

As Sandman said, inheritance is probably not what you want here. Anytime you have a collection of Base and you find yourself wanting to access Derived.Member when iterating, that is a code smell. Either refactor the interface to Base, or rethink your structure.

 

If you must have this structure, consider inverting the responsibility, i.e.

public class Archetype
{
    public int SomeProperty {get;set;}
    public int AnotherProperty {get;set;}

    public virtual void Populate(Entity e)
    {
        e.Foo = SomeProperty;
        e.Bar = AnotherProperty;
    }
}

public class EnemyArchetype : Archetype
{
    public string EnemyProperty {get;set;}

    public override void Populate(Entity e)
    {
        base.Populate(e)
        e.Baz = EnemyProperty;
    }
}

 but I'd still recommend Sandmans  approach




#5186543 where are the 4 addresses of an integer

Posted by ChaosEngine on 12 October 2014 - 02:35 PM

Do your own homework


#5178209 Critique my approach to writing RAII wrappers

Posted by ChaosEngine on 04 September 2014 - 09:52 PM

 

RAII does not mean "allocate resources".

Whaaaat?
 
"Resource allocation is instantiation" does not mean resource allocation is instantiation?  What kind of wacky newspeak are we dealing with here?
 
Perhaps the simple answer is that if you can not control the lifetime of a resource, you can not use RAII to wrap it.  It's just not the appropriate tool for the job.

 

RAII is actually a total misnomer. The reason it's used has almost nothing to do with resource allocation (or indeed initialisation or instantiation) and everything to do with resource cleanup.

 

I remember reading in the C++ FAQ that a more accurate acronym would be DIRR (Destruction Is Resource Reclamation), but we're kinda stuck with RAII now.




#5176542 Windows Programming

Posted by ChaosEngine on 27 August 2014 - 05:18 PM

If your tool is Windows only, you might consider using C# and winforms (or possibly even WPF). It's a much nicer language than C++ to build UIs in and you can still interface with your C++ engine code easily enough using C++/CLI.




#5176539 List of generic objects

Posted by ChaosEngine on 27 August 2014 - 05:06 PM

Okay, I really like this technique better than generics.

 
eh??? Pretty sure that code still uses generics.
 
 

public class Unit
{
    public readonly Type m_type;
    
    public List<Creature> critters = new List<Creature>(); // Generics!
    
    Func<Unit,Creature> m_creationDelegate; // also generic

    public Unit(Type type, Func<Unit,Creature> creationDelegate) // even MOAR generics
    {
        m_type = type;
        m_creationDelegate = creationDelegate;
    }

    public void Spawn(int count)
    {
        for (int i=0; i<count; ++i)
            critters.Add(m_creationDelegate(this)); // invokes the delegate which returns a Creature.
    }
}

 
I'm not sure you've grasped what generics are in C#.

Don't take this the wrong way, but TBH, this whole concept is a bit messy. 

Why do you need to know the Type of the creature? Be very wary of code that uses reflection. It's significantly slower, harder to debug and harder to navigate (for a start, visual studio won't tell all the places the code is used, etc)

 

Are you going to do some kind of 

(if unit.m_type == typeof(Goblin)) // do goblin stuff

 That's a huge code smell.
 
 
 

In my actual game though, the player wizard is an object which derives from the creature class. The player wizard can do all sorts of things which a standard creature doesn't do (like channel mana and cast spells) and has a whole bunch of attributes which no other creature needs to worry about (age, ghost state, etc). So, it would make sense for the wizard to be a derived class from Creature.

Actually it doesn't. It would be much better to use composition instead of inheritance here. You would probably be better off looking at an entity system




#5172992 what is a template from c++ intent?

Posted by ChaosEngine on 11 August 2014 - 08:27 PM

 

std::vector or std::unique_ptr

standard vectors just poses pointers. Right?  Nothing further and they do not mess with a template stuff.
 

In the case of templates the compiler is checking types while it is doing the code generation.

It realy does not, and if it does..... nope, c++ cannot turn to a hell of a brain language can it?
 
Frob, did you ever fall for tamplate usage, or sticked to conforming compiler runtime code? You sure did Frob. Template= utopia!!! IT DOES

 

None of what you have said makes any sense.
 
At a basic level it's not rocket science.
Let's say you have a simple function

int Add(int a, int b) { return a + b; }

Now in non template C++ if you wanted to use that function to add floats or doubles, you'd need to write

float Add(float a, float b) { return a + b; }
double Add(double a, double b) { return a + b; }

ugh... repetition.

With a template you'd write

template <typename T>
T Add(T a, T b) { return a + b; }

when you want to use it, you would write

int x = Add<int>(1, 2);
double x = Add<double>(1.0, 2.0);

and the compiler would generate two functions for you.

int Add_int(int a, int b) { return a + b; }
double Add_double(double a, double b) { return a + b; }

These functions are only generated when you use them.

But let's say you had a class Bar

class Bar
{
};

if you tried to use your Add function with a Bar, the compiler would first generate a new add function

Bar Add_Bar(Bar a, Bar b) { return a + b; }

and then it would issue a compiler error (probably "no operator + found that accepts Bar and Bar")

On the other hand, if you tried to use an std::string with Add

std::string x = Add<std::string>("Hello, ", "World!");

// generated code
// std::string Add_string(std::string a, std::string b) { return a + b; }

this would work fine because std::string defines a + operator.

Now this is a very simple explanation, and leaves out all kinds of complexities like argument matching, SFINAE, specialisation, etc, but that's the basics of it.




#5172196 Thread-safe weak pointers

Posted by ChaosEngine on 07 August 2014 - 10:53 PM

Thanks for the honest words, and you're propably right with it too. I just remember reading something like "Write your code so that it is hard to use it incorrectly", which I liked as an approach. But no one would propably do something like delete somePtr.Get() without malicious intent, and then he would only harm himself

 
You're probably referring to Effective C++ (3rd ed) Item 18 "Make interfaces easy to use correctly and hard to use incorrectly"?

 

It's good advice, and I'd argue you're already following it. For example, if you had an implicit T* conversion operator in your class, that would allow people to easily write 

delete someSmartPtr;

and it would compile fine. But any decent programmer would look at 

delete someSmartPtr.get();

and get worried.

 

The important word there is "hard", not impossible. If you make the developer think about something they possibly shouldn't be doing instead of doing it accidentally, that's good, but if you stop an expert from doing something he needs to do, he'll just get annoyed.




#5163728 Can someone help me write a program?

Posted by ChaosEngine on 29 June 2014 - 05:30 PM

You wouldn't need to be very advanced to do this.

 

But no-one here will write it for you. You need to put in the effort.

 

Let's start with a few basics. What are you going to run this on (windows, linux, mac, etc)? Do you want a command line program or a GUI? How are you going to store the users letter/colour choices?

 

Once you decided what you want, break the problem down into steps. At each step, google will be your friend.

As a starter, the first thing you need to do is read your text file. Google "how to read a text file in your-language-choice-here".

 

This might sound a little bit callous, but learning how to break a problem down into manageable chunks and then research solutions is pretty the core skill of programming.

 

If you're looking for advice on languages, etc, I would recommend Python as a good place to start.






PARTNERS