NOTE: When I say efficiency, I mean in getting things done, not performance.
Really, it’s just a coding style difference between the two.
One line of code.
It may not seem like much, but I think this is incredibly important.
To compare, here’s the C version:
Yes, the C version is only 1 line longer, but a better parallel would be this:
The import line is implied by Java and C#. I’d say the C version makes more sense, since there is nothing implied. Nothing done for you without asking.
Languages like Java and C# sometimes get around this initial verbosity with the tools. Your IDE, once you go File->New Project will generate your basic structure for you. And that’s fine, but it’s not really more efficient enforcing a structure/hierarchy when it comes to something simple.
This is a bit of a subtle thing, but knowing and dealing with the type isn’t really that important if everything is a number or string anyway. And again, ignoring performance, being able to set a number actually equal to null is nice because it doesn’t waste numerical space (well, the space is already wasted due to everything being a reference). That doesn’t matter though.
Nested Arrays and Objects (Tables)
This is what matters. Because all types are implied based on the values you hardcode, that makes hardcoding a sophisticated structure extremely simple. You’re not wasting time building classes, you’re building exactly what you want, and changing it exactly when you need to. Added a new property? No problem, it’s already allowed, and you can just go implement it.
I didn’t have to go make a class for the ArtFile Objects, nor did I have to write all permutations of constructors to take only the arguments I wanted. I just hardcoded what I want. In my code that uses the ArtFile Objects, I check if certain properties exist, and if they do do something, otherwise use what I’ve decided is the default.
To use the data it’s as simple as:
This saves so much time. No fuddling around types, just fill in what you want and you’re done.
That kinda makes sense, and we have to do it this way due to how inheritance sort-of works, but it could be cleaner.
For example, in Squirrel:
I think this is a nicer syntax.
Generic Owner-less Functions
This can be created at the global scope, and given to any object.
And thus called:
It can also be cleanly applied to any object, without adding it to the object.
Where the first argument to call is what to use as ‘this’.
Only BG1 is awesome, so why waste time adding a short global function only used once?
No Function/Variable Prototypes or Externals Required
One of the first things you learn about Object Oriented Programming is that the Global Scope is bad. Yes, the Global Scope is bad… a bad place to put local and member variables, but it’s an awesomely efficient way to access things. In C/C++, global variables resolve down to a single pointer reference to where the data is in memory, rather than a pointer reference/offset for every level deep it is. Nobody really cares about the few cycles lost resolving an address anymore, but it’s still worth knowing that globals are fast, and back in the day that was sometimes why you used them.
That said, care must be taken when dealing with objects in the Global Scope. You have to be sure you choose names that don’t conflict with other things in the Global Scope, such as standard libraries. If you can do that, you can end up creating simpler code too.
I just told the player to set his “Sit on the Couch” state and animation on the same line of code I defined where the couch is in the world, and what artwork to use.
That is the magic of Global Scope and Anonymous Functions in action.
Need to add a state or property? Add it when needed!
You *could* create a structure to store states, or edit the base class to include a state and default state.
… or you could just add it as needed.
For efficiency, you probably want to populate your data with states, missing properties, etc during some initialization stage, but the above demonstrates the point.
TODO: Add More
I’m going to add more examples as I think of them. I blog for me as much as anyone that reads them.
Problem 1: No Operator Overloading
That said, Squirrel to the rescue.
C and C++ are fundamental, like a better Assembly. You can write C/C++ code and know the assembly it will generate. If you don’t care about the generated code at that level, then that’s fine. I just don’t think Java and C# do enough to provide a core language that’s faster to work in.
That said, I haven’t made a serious project with Squirrel… yet.
I’ve done some simple experiments with Chrome’s V8. It works, and I like it. But when I’m writing games, I really use vector math a lot… A LOT… HOLY!