Addiction to soft coding...

Started by
14 comments, last by coder84 17 years ago
Hi everybody! The title talks for itself, I'm obsessed with soft coding, I'm always trying to left the actual engine clear and take all the information from external sources. And I'm asking the following questions because a lot of people asked me why don't I just hardcore everything. Which makes me doubt, because I'm not sure. Is soft coding a good practice, or is it useless? Should I leave at least some of the game data inside the engine or should I leave it totally clear to make the code more flexible and versatile? BTW, I'm developing a MMO. I believe it depends a lot of what you are developing, with a Tetris I wouldn't do this, but... With a MMO I think it turns useful. For example, I'm using scripting to handle the creatures AI. Some would say, why don't you inherit from an parent class and add some specific behaviors... And I actually have no point there... I have all my maps, scripts, items descriptions, sprites, configurations as external files; that consumes more processing power and makes the game heavier but I make all that code reusable, don't I?
Advertisement
you might want to have 3 layers

at the bottom would be the engine, it would be basically be what you have now, as light as possible and no specific details of the game

in the middle you'd have game specific code that is still in the native language, you could put a lot of ai behaviors function here

at the top would be the scripting level, it could have ai and items but it would just be a few variables and function calls so most of the work would be done by the middle layer

Quote:Original post by coder84
Is soft coding a good practice, or is it useless?

that dosen't really have a answer, hard coding everything isn't a good idea but soft coding everything defeats the purpose of having a having a separate scripting system, for the time being just do what works for you,

just try to keep in mind what parts of the system will be preformance bottlenecks and what you can cache at startup
Hmm, I see your point. I agree in most of it, most of the object behaviors which, won't change should be written in the middle layer.

But doing that I'm not sure about how to handle, exceptional NPCs (for example inside a quest), which have to change its routines. As it written up inside the game, I couldn't change it. Instead, scripting I would. I should make a script (a copy of the original), and change what I want to change. With fixed code, I couldn't.

For example, if I want a Wolf not to attack, when its coded to attack the user, I couldn't change it because is fixed.

Probably I will use default creatures, with the code of the middle layer and make a script if I want to make one different.

Quote:that dosen't really have a answer, hard coding everything isn't a good idea but soft coding everything defeats the purpose of having a having a separate scripting system, for the time being just do what works for you,


I didn't understand that =/

Equally, there is something I didn't say yet... My scripting language is the same that the language I'm using to program. As C# has a code reader, it can read C# code from any file. Even if its extension is not ".cs". So is pretty easy to me to make the scripts, if thats what you trying to say.

Thanks for replying!!
Quote:Original post by coder84
Quote:that dosen't really have a answer, hard coding everything isn't a good idea but soft coding everything defeats the purpose of having a having a separate scripting system, for the time being just do what works for you,


I didn't understand that =/

i suppose that could have been clearer
basically i was trying to say that learning to use soft code is definitely a good thing but it doesn't mean soft code is the best solution 100% of the time (like any other technique)

Quote:Original post by coder84
Equally, there is something I didn't say yet... My scripting language is the same that the language I'm using to program. As C# has a code reader, it can read C# code from any file. Even if its extension is not ".cs".

that makes a bit of a difference, iv never used the C# reflection api but i would assume its performance is almost the same as statically linking C# classes, so it really doesn't matter then since as long as you avoid unnecessary loading and compiling your scripts should run at the same speed as your engine.
You are right.

Quote:i suppose that could have been clearer
basically i was trying to say that learning to use soft code is definitely a good thing but it doesn't mean soft code is the best solution 100% of the time (like any other technique)


Soft coding is useful when dealing with big projects (like MMOs). It keeps the code clearer and it improves versatility and flexibility. But it is totally useless to use it while making a Pacman or Tetris. Is necessary to know when to.

Thanks for helping!
For a quick hack, anything will do.

For any project which will need to be maintained, keeping your options open is always a good choice, as long as you stay realistic.

It's easy to get carried away and abstract too much. It's extremly easy to find a shiny new hammer, and then develop tetris toolkit that supports n-dimensional pieces using some numberical recipes library that represents piece topologies.

There's some rules though. One Java guideline states, that only numeric constants that should ever be hard-coded are values of 0 and 1. In Java, that's not such a bad practice, even if all you ever do declare static final (static const equivalent) variables.

But perhaps the most dangerous aspect that comes with hard-coding is overuse of static variables. They quickly add unwanted interdependancies which can no longer be removed without completely changing the design.
Quote:Original post by Kaze
Quote:Original post by coder84
Quote:that dosen't really have a answer, hard coding everything isn't a good idea but soft coding everything defeats the purpose of having a having a separate scripting system, for the time being just do what works for you,


I didn't understand that =/

i suppose that could have been clearer
basically i was trying to say that learning to use soft code is definitely a good thing but it doesn't mean soft code is the best solution 100% of the time (like any other technique)

Quote:Original post by coder84
Equally, there is something I didn't say yet... My scripting language is the same that the language I'm using to program. As C# has a code reader, it can read C# code from any file. Even if its extension is not ".cs".

that makes a bit of a difference, iv never used the C# reflection api but i would assume its performance is almost the same as statically linking C# classes, so it really doesn't matter then since as long as you avoid unnecessary loading and compiling your scripts should run at the same speed as your engine.


Reflection in C#(Aswell as other language that support reflection) is extremely slow (compared to normal C# code).
In general you should Never use reflection in any area where performance is a concern.

http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/#S2
(Some good information about whats expensive and what isn't, and how to avoid the worst)

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Quote:Original post by SimonForsman
Reflection in C#(Aswell as other language that support reflection) is extremely slow (compared to normal C# code).
In general you should Never use reflection in any area where performance is a concern.

http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/#S2
(Some good information about whats expensive and what isn't, and how to avoid the worst)

i no expert on reflection but once you have the class JIT'ed and the needed methods cached why would it still be slow?
(sorry if the answers in the link, i didn't really follow it)
Quote:Reflection in C#(Aswell as other language that support reflection) is extremely slow (compared to normal C# code).
In general you should Never use reflection in any area where performance is a concern.

http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/#S2
(Some good information about whats expensive and what isn't, and how to avoid the worst)


A MMO is an area where performance really concerns. And as C# reflection will be used every time an object appears (I was thinking on script every object), I must find another way.

I could use Lua instead of C# reflection. Or even better, define the classes inside the game, and take all the data of the instances from XML.

EXAMPLE:

class Monster{     int HP, XP, Attack;     string textureSet;     bool RunsIfWonded;     float Velocity;}


<Monster type="Wolf">
<HP>120</HP>
<XP>18</XP>
<AttackMin 10/><AttackMax 22/>
<RunsIfWonded>false<RunsIfWonded/>
<Velocity>0.25f</Velocity>
</Monster>

But doing that, I couldn't set its behavior. I would only set its properties.

What you suggest?
Original post by Kaze
Quote:Original post by SimonForsman
Reflection in C#(Aswell as other language that support reflection) is extremely slow (compared to normal C# code).
In general you should Never use reflection in any area where performance is a concern.

http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/#S2
(Some good information about whats expensive and what isn't, and how to avoid the worst)

i no expert on reflection but once you have the class JIT'ed and the needed methods cached why would it still be slow?
(sorry if the answers in the link, i didn't really follow it)[/quote


Quote:
source:http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/
Invocation mechanisms can be divided into three categories: early-bound, late-bound, and a hybrid of early- and late-bound invocation. Early-bound invocation mechanisms result from direct intermediate language (IL)-level instructions (call, callvirt, and calli), interface calls, and delegate calls. These early-bound call mechanisms are typically emitted statically by compilers at compile time, where the compiler is supplied with all of the required information about the target to be invoked. These early-bound cases are significantly faster than their late-bound and hybrid counterparts as they map down to a few x86 instructions emitted by the JIT or native code generation (NGEN) compilers. Aggressive optimizations can also be made by the JIT because the call site and method are unchanging and are well known to the runtime.

The late-bound cases are MethodBase.Invoke, DynamicMethod via Invoke, Type.InvokeMember, and late-bound delegate calls (calls on delegates via Delegate.DynamicInvoke). All of these methods come with significantly more negative performance implications than the early-bound cases. Even in the best case, they're typically an order of magnitude slower than the slowest early-bound case. Type.InvokeMember is the slowest of the late-bound invocation mechanisms because there are two functions that InvokeMember needs to perform to properly invoke a member. First, it must figure out the exact member it's supposed to invoke, as determined by the user's string input, and then perform checks to be sure the invocation is safe. MethodBase.Invoke, on the other hand, doesn't need to figure out which method it must call because the MethodBase already holds the identity of the method.

To further explore how reflection sets up and invokes a member, consider a typical usage of MethodBase.Invoke:
-snip-


Basically the reflection API will be slow unless you only use it to load plugin classes(MSIL) using a known interface. (Comparable to loading DLL:s in C or C++ i guess).

There are methods around this though but none that would allow the use of C# as a proper scripting language without paying the standard performance price.

converting C# code to native at runtime is simply too slow to work, normal C# programs are converted to optimized MSIL before distribution and the JIT compiler goes from optimized MSIL to optimized native. (takes a bit of time but as it can be done before the application starts it doesn't impact performance that much).

Quote:Original post by coder84
Quote:Reflection in C#(Aswell as other language that support reflection) is extremely slow (compared to normal C# code).
In general you should Never use reflection in any area where performance is a concern.

http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/#S2
(Some good information about whats expensive and what isn't, and how to avoid the worst)


A MMO is an area where performance really concerns. And as C# reflection will be used every time an object appears (I was thinking on script every object), I must find another way.

I could use Lua instead of C# reflection. Or even better, define the classes inside the game, and take all the data of the instances from XML.

EXAMPLE:

*** Source Snippet Removed ***

<Monster type="Wolf">
<HP>120</HP>
<XP>18</XP>
<AttackMin 10/><AttackMax 22/>
<RunsIfWonded>false<RunsIfWonded/>
<Velocity>0.25f</Velocity>
</Monster>

But doing that, I couldn't set its behavior. I would only set its properties.

What you suggest?


Lua would be just as slow, or even slower, its not a problem with C# specifically.

The question really is : would it be too slow ?

"Every time an object appears" doesn't sound too bad. (unless you have hundreds of thousands of objects appearing every frame)

Oh, and when i said Areas i meant parts of the code. Even an MMO have some areas of the code that will run thousands of times each second (or maybe even millions of times / second), those are the areas where you want to avoid reflection.

On an entity (Monster, NPC, etc) that is updated a few times / second or less it is hardly a big issue.

For an MMO your main concern will be bandwidth, not CPU time. (You might want to avoid heavy reflection use on the client though as you want the game to run smoothly on as old hardware as possible)

[Edited by - SimonForsman on April 8, 2007 5:42:30 PM]
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement