Hot answers tagged

91

You can use int, and consider everything in cents. $1.20 is just 120 cents. At display, you put the decimal in where it belongs. Interest calculations would just be either truncated or rounded up. So newAmt = round( 120 cents * 1.04 ) = round( 124.8 ) = 125 cents This way you don't have messy decimals always sticking around. You could get rich by ...


66

Okay, I'll jump in. My advice: it's a game. Take it easy and use double. Here is my rationale: float does have a precision issue that appears when adding units to millions, so though it might be benign, I would avoid that type. double only starts getting problems around the quintillons (a billion billions). Since you are going to have interest rates, you ...


44

Log an error and gracefully exit. Ideally, display a human readable error on screen as well. There should be a core pipeline of hard coded functionality that operates without these data files. It's the same pipeline that loads the data files in the first place. It should be capable of detecting when these core data files are corrupt or otherwise faulty and ...


21

Floating point types in Java (float, double) are not good representation for currencies because of one main reason - there is a machine error in rounding. Even if a simple calculation returns a whole number - like 12.0/2 (6.0), the floating point might wrongly round it (due tho the specific representation of these types in memory) as 6.0000000000001 or 5....


20

An idea is to use the Visitor design pattern. You need a Renderer implementation that knows how to render props. Every object can call the renderer instance to handle the render job. In a few lines of pseudocode: class Renderer { public: void render( const ObjectA & obj ); void render( const ObjectB & obj ); }; class ObjectA{ public: ...


17

Lua was actually designed to be an efficient data file format. It's original use was to load huge geographical data sets. Actual scripting/logic features came later. Even with all the new scripting features, it is still used by its original authors to store and load large data sets. That is not to say that Lua is the most efficient choice. That will ...


17

For small scale game and where process speed, memory is important issue (due to precision or work with math co-processor can make painfully slow), there double is enough. But for large scale games (for example, social games) and where process speed, memory is not limited, there BigDecimal is better. Because here, int or long for monetary calculations. ...


13

You want to store your currency in long and calculate your currency in double, at least as a backup. You want all transactions to take place as long. The reason you want to store your currency in long is that you don't want to lose any currency. Let's suppose you use a double, and you have no money. Someone gives you three dimes, and then takes them back. ...


13

You could save all the data both locally on your player's computer, and sync it automatically to Steam Cloud, which indeed allows you to store stats/user preferences/progress/etc.. online. Steam Cloud API : header: ISteamRemoteStorage.h Game settings, savegames, and other user-specific bits can be replicated to the Steam Cloud to provide the ...


12

Byte56 mentioned one option. There is at least one other: Assume default values and display a Warning. Depending on the nature of your data, it might be perfectly acceptable to assume some default values and warn the user that "since file xxx failed to load, we are using a generic yyy object."


11

(1) Only save the really important bits, (2) only save them when they have changed since the last time they were saved, (3) save them individually, and (4) save them via an asynchronous system. Much of the world is likely to be static - that data doesn't need to be serialised at all, as it probably already exists on disk in some form. At the other end of ...


9

There are a couple different ways to store the data for a game with blocks like Minecraft. The way I believe Minecraft does it is breaks the world up in the 16x16x256 Chunks. The chunks around the player are loaded into memory when the player starts the game, then a background thread loads more as you walk around. Here is a video that shows it: http://www....


8

My thoughts would be to mark the object to be dead then let the factory handle it the on next frame for deletion. Is it the best and fastest way? What do you think? Why wait until the next frame? Just remove it at the end of the current frame. But I wouldn't worry about it being the "fastest" way, unless you're dealing with lots of dead objects every frame....


8

I'd go as far as to say that any value which may be displayed to the user should almost always be an integer. Money is only the most prominent example of this. Dealing 225 damage four times to a monster with 900 HP and finding that it still has 1 HP left will subtract from the experience just as much as finding that you are an invisible fraction of a penny ...


8

Im late to the party here but I spent A-LOT of time researching this. First why I don't use the following: XML: Excessively verbose. Tons of redundancy. Repeating field names? GROSS JSON: I think JSON is great for a UI layout but for a database, hell no. It will have the same problems as XML, redundancy, and deep nesting. GROSS. SQL: This is a great ...


6

When we implemented our networking engine we exploited a number of compression techniques: First we write all of our snapshot bit-wise: bools are only 1 bit instead of 1 byte (or more depending on compiler). We wrote a bitstream class that reads and writes data to a stream. This saves a fair bit of data all alone when packing flags down. For an example of ...


6

It depends on whether this happens during development or release. During development, you will have all kinds missing things, errors, and mess-ups, constantly, all the time, and you may even want to "hot" load assets on demand or replace an asset while the game is running. You might edit scripts with the game running to test an AI performs better, ...


5

Even if it's "only a game", I would use Money Pattern from Martin Fowler, backed up by a long. Why? Localization (L10n): Using that pattern you can easily localize your game currency. Think about the old tycoon games like "Transport Tycoon". They easily allow the player to change in-game currency (i.e From British Pound to US Dollar) to meet real world ...


5

There's no single "best" way to do this, it depends on the needs of your game. Here are some options: Hard Coded Both the easiest and probably least flexible approach. No reason you can't just make a static class with static fields for each of the levels. Actually, this isn't a bad way to prototype, so you can move it into some other approach later. If ...


5

You'll want to use a List. Probably a List<InventoryItem> or similar. The documentation for List<T> is here. Here is a set of answers on how to serialize a list to XML (you didn't say how you were saving your XML - this link gives you some options). An XML file can store the list of items "inline", so it might end up something like: <...


5

Do you need to have every single chunk (presuming you're using chunks) in memory at once? Some will be occluded - particularly underground - or behind mountains etc. Lots will probably be just air/empty and so could be marked with a flag. Also you could use a LOD octree or similar structure to try keep the detail currently visible inversely related to the ...


5

For statistics you can use ISteamUserStats, which is a part of the Steamworks offering. This interface allows you to define (from the Steamworks developer site) a set of tracked statistics with various properties, and can even auto-grant Steam achievements based on the stat values. The interface supports multiple types of numerical data (integer, floating ...


4

I think that the problem here is not lua or xml but the design. IMO you should use a fatory pattern coupled with a prototype. Your lua script should be used to create a new prototype of monster, then your factory will create new "instances" of this monster (cloning the prototype). This way you have to run your script only once. However, if all of your ...


4

It's typical than an Entity System will duplicate some data. Some solutions I've seen is that the Position component contains a transform representing the world position & orientation of the entity. The renderable component has a reference to the position component, as does the physics component. In other designs, the renderable system and physics ...


4

I know you've already accepted Zhen's answer but I'd like to put another out there just in case it helps anyone else. To reiterate the problem, the OP wants the ability to keep the rendering code separate from the logic and data. My solution is to use a different class all together to render the component, which is separate from the Renderer and the logic ...


4

Build a rendering-command system. A high-level object, which has access to both the OpenGLRenderer and the scenegraph/gameobjects, will iterate the scene graph or gameobjects and build a batch of RenderCmds, which will then be submitted to the OpenGLRenderer which will draw each in turn, and thereby containing all OpenGL related code therein. There are more ...


4

The overall problem by using any text-based solution is that they are highly unreliable and naive implementations are often prone to data integrity issues. This is where david's suggestion to use a database becomes important. Databases offer you the ability to write entries to a table without worries about concurrent operations from other connections. ...


4

You probably do want to communicate to this information to the client so that they are able to view it. You can treat the client as a dummy terminal though with sparkly representation and have a "neutral" server as the authority. Lets consider League of Legends in this context. At the beginning of the game, each client connects to Riots servers. Every ...


4

I don't think placing game related files in My Documents/My Games/ annoys users, it's pretty much standard to put it there so why don't you do it? Each user can have it's own save games, skins and mods this way and it's easy accessible/mod-able. However, for larger assets you do want to consider to share them amongst user accounts. I think you have two ...


4

You need to transfer the asset metadata as well as the asset files themselves. The metadata files might be invisible depending on your settings. On the old computer, go to Edit>Project Settings>Editor in Unity and set Version Control to Visible Meta Files. You should now see .meta files appear next to all the assets in your Assets folder. Copy those ...


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