Hot answers tagged

41

Use a boolean flag. In the example shown, you'd modify the code to be something like the following: //a boolean flag that lets us "remember" if this thing has happened already bool thatThingHappened = false; void Update() { if(mousebuttonpressed && !thatThingHappened) { //if the mouse is pressed and that thing hasn't happened yet ...


22

A common approach is to have a component-based approach where the base-class "Unit" just implements the most basic aspects all units have in common, while each unit then has a list of multiple component-objects which say what it can do and how it does it. For example, a tank might have the components Mobile, Destructible, Attacker, an immobile turret only ...


20

Should bool flag not suffice or you wanted to improve readability* of the code in void Update() method, you could consider using delegates (function pointers): public class InputController { //declare delegate type: //<accessbility> delegate <return type> <name> ( <parameter1>, <paramteter2>, ...) public delegate void ...


16

That is an interesting question. Mainly because answering it raises the shades-of-grey versus black-vs-white dilemma. is there anything wrong with building a game before I design it? If you think of that question as a yes or no type, the answer can only be: yes, there is. If the answer can be more nuanced, then it changes. Reason: you should never ...


13

Have you looked into entity component systems and event messaging strategies? Status effects should be components of some sort which can apply their persistent effects in an OnCreate() method, expire their effects in OnRemoved() and subscribe to game event messages to apply effects which occur as a reaction to something happening. If the effect is ...


12

Without knowing more about the exact game you're writing, and how you're writing it, it is very difficult to say generic solutions to your problem. However, you may want to consider this decision you're taking of leaving the networking code to the end, depending on how crucial networking is for your game. What I mean is that, if you're writing a network ...


12

Create an engine module/folder/whatever, that contains everything that can be generalized and is 100% independent from the rest of the game. This would include some code, but also generic assets that are shared among games. Put this engine in its own git repository, which will be included in the games as a git submodule That's exactly what I do ...


11

RobStone is on the right track, but I wanted to elaborate since this is exactly what I did when I wrote Dungeon Ho!, a Roguelike that had a very complex effects system for weapons and spells. Each card should have a set of effects attached to it, defined in such a way that it can indicate what the effect is, what it targets, how, and for how long. For ...


9

Modular arithmetic can help you here. % is the modulus operator in Java, and the expression X % Y returns the remainder of the division operation X / Y. This is useful for making numbers "wrap around." Based on your sample code, a full cycle of your day is 200 units of Statistics.duration. The first 50 are the day portion, the second 50 are the noon portion,...


7

Short version: no, your job as an engineer is to evaluate all applicable solutions to problems before choosing a solution Long Version: "ECS" is an overloaded term. It had a somewhat clear definition at first but it's been overly muddled since. It doesn't matter much, though, since the answer is the same either way: every architecture has pros and cons. If ...


7

The only real problem you'll be fighting is floating point precision; only 6 digits. When you accumulate floating point error over astronomical distances, you get astronomical inaccuracy. This causes distant bodies to rapidly alternate between two or more places from frame to frame. It's likely you'll have two coordinate systems and, in most cases, you'll ...


6

At some point an engine MUST specialise and know stuff about the game. I will go off on a tangent here. Take resources in an RTS. One game may have Credits and Crystal another Metal and Potatoes You should use OO concepts properly and go for max. code-reuse. It's clear that a concept of Resource exists here. So we decide resources have the following: ...


6

If what you want is to bypass the floating inaccuracy caused by single point precision problem, for the sake of creating bigger environments for your game, then it depends on what you are willing to accept as a solution. Let's start by making this clear: it is impossible to alter the coordinate system at the inner core of Unity. So, you can't use double-...


6

First, you should not have a strict 1-1 mapping of Components to Systems. It's unclear to me from your question if that's the case already. You may very well have singular systems that use or interact with numerous components. Rendering, physics, AI, etc. are all Systems (they perform a cohesive set of updates and logic) but interact with many Components. A ...


6

"Manager" classes can be problematic for various reasons. The two key reasons tend to be: the name is unclear (what actually does "management" entail, and is it always the same for every type of thing being managed?) they tend towards being buckets of functionality that violate the single responsibility principle (that is, that a type should do one thing) ...


5

So the other answer is good, but I will say this. In C++ the RAII idiom means "you never type object.close() or object.free() or object.release() or anything like that, it is a bit laxer on initialisation because of assignment operators. Suppose we have the following class Object { public: Object() { /* CANNOT doStuff after this, as it isn't ready*/ } ...


5

That's a question that frequently pops up. I will take the liberty to forward you to another fairly detailed answer I already gave to the same issue, instead of just repeating it here: Is a custom coordinate system possible in Unity From there, what I would suggest the most is that you read the amazing paper at: http://citeseerx.ist.psu.edu/viewdoc/download?...


5

FXC or D3DXCompileShader or D3DCompile to compile basic HLSL vertex/pixel/... shaders (single entry point, just like GLSL). Then create shaders from the generated shader blobs using API functions such as CreatePixelShader (D3D9) or CreatePixelShader (D3D10/11). Then apply these shaders with SetPixelShader (D3D9) or PSSetShader (D3D10,11). To transfer ...


5

Your design contradicts object oriented programming. In object oriented programming (which is the core of the C# language) you combine data and logic in a class. This means a class is a construct that defines all of its properties (cost, level, children in your example) as well as what it can do. Hence you should of course combine those two parts, ...


5

Sword, Axe, et cetera as subclasses of Weapon sound like inheritance abuse to me. Fundamentally these types of weapon differ only in data (how much damage they do, what type of damage it is, what sprites or animations or effects are associated with the weapon, and so on). Consequently I'd advocate for an approach where you flatten this hierarchy away, ...


5

When you read the blog post you linked to in the comments, then you will see that "being a Manager if by another name" is exactly what it wants you do to. It's general consensus in software development that global variables are evil, and the only alternative is that any data is held by other data. The problem with a class named FoobarManager is that the ...


4

It's OK to have component dependencies. And the more explicit the dependency the better. There's nothing worse than dependencies hidden behind useless layers of indirections. Your case doesn't look like a dependency hell to me. No circular dependency or unclear ownership of data: consider yourself lucky! Now I still believe that you need to reverse your ...


4

I'd move all those UI constants to a centralized file. It doesn't matter if they are only used in one class or not. The reason is that if you ever want to adjust that type of stuff, it is nice to have it all in one location. You don't want to have to muck with a dozen different classes just because you decided you wanted to change the overall look and feel ...


4

For synchronisation, you need to decide who (your code, animations, or neither) is the timing authority—the "beat" that everyone else "dances" to. Different arrangements suit different games: Animations are the timing authority (your current setup) The game code may trigger animations. When animations reach certain points, they raise events (e.g. ...


4

You're thinking in very static terms. Just because an object is half a world away doesn't necessitate any issues. If entity coordinates are stored relative to chunk rather than relative to world, this is trivial to achieve. Welcome to the way you'd have to do it if you were writing a voxel world in native code. So let's assume a concept called locales. It'...


4

I doubt that there's a way to change the coordinate system Unity uses internally for its transforms/physics/etc. Not without recompiling the engine source at least. But that may not be necessary to get both the precision and scales you need. Kerbal Space Program is made with Unity, and plays out over interplanetary scales. The developers have been great ...


4

I've worked quite a bit with Mecanim in Unity, and feel I have a quite good understanding of how it works. Like you say, blend-trees are almost definitely the way to go for doing locomotion. Blend-trees are generally for when you want to continously blend animations to create the final output. Like give the player the control of how fast the avatar is ...


4

When doing complex projects like games, you often can't make all the features you want, because you're running out of time/money or because they didn't turn out to be as good as you expected. This is known as feature creep. But there's a flip side to this; you will also find features that you didn't think you needed, but as the project takes shape their need ...


4

What you're looking at is an in-line initialization of an associative array (which functions similarly to a dictionary or map in other languages). If you made a variable in javascript like so: var phones = {}; Then added values (right hand side) to keys (after the dot on the left) phones.Steve = '2342311'; phones.Kyle = '3230009'; You will have an ...


4

A lot of games use a component-based system for entities which is where a bunch of behaviours and abilities can be added to a more generic unit type rather than being coded as part of the entity's class (or equivalent).



Only top voted, non community-wiki answers of a minimum length are eligible