Unity: Storing and using weapons and objects

Started by
4 comments, last by dirmp 8 years, 9 months ago
Good afternoon, I'm planning a system that I use, but I'm with some questions... I am creating a game where it will have a system to store weapons and objects, as in the main RPG's, where the user can pick up a weapon or object from the floor and use. I've done the whole system to pick up the item, hold the hand of the player and all that, my question is, will bring various items and weapons to the game that will not be visible in the scene, and will be acquired in some other way (purchase, crafting , among others). I wonder how I can "store" all these objects in the game, to be initialized later... I must use instantiate when the object is obtained? Also, as I will add many objects, I must enter the unity and manually add each object, or is there some way to make an external database, to facilitate the future?

Thank you!

Advertisement

Use a json string like this.

[

{"id":"Sword1", "attack":"5", "crit":2},
{"id":"Sword2", "attack":"12", "crit":1}
]

and then parse it and load up your object that way.

Or create prefabs in unity for all your objects and have a weapon component that holds all the weapon data. Then you can use the unity editor to edit these weapons.

So like WeaponData would be a component with an attack and crit variable.

Then create a game object and add the component to it. From here you can now save the game object as a prefab. You can now copy and past this prefab to your hearts content and rename then and have each as a different weapon which you will be able to edit individually.

There's honestly a ton of ways to do this. The most straight-forward would be to just create a script file that defines the properties of each item, though, its also least flexible in that it makes it difficult to change things after you've shipped.

A data-driven approach is probably preferable, like Derek suggests; you can use JSON or some other format to your liking. Load and parse them at run-time so that you can add or change the items later if you need. There's a way to specifiy that an arbitrary file is meant to be published with your Unity game package, rather than processed at build-time. You might want to do some basic hiding/obfuscation of any plain-text files, just to deter the casual cheater, one easy way is to put these things in a .zip file, or to do some basic XOR "encryption" -- but don't get caught up trying to come up with a perfect anti-cheat thing because you won't (people much smarter than you and I haven't yet in their entire careers, you or I aren't going to do it in our spare time) all you want is to prevent people discovering easy cheat attacks while casually browsing your game files.

More generally, the tendency with Unity, especially if you're less experienced, is to think of everything in the game as *the* thing -- you have a game with 12 loaves of bread, and you literally have 12 loaves of bread defined inside Unity, and maybe you help yourself out with prefabs. But what your really want in this instance is not to define disticnt game-objects for each instance, but to be able to create instances from a kind of recipe. What you store in the JSON (or whatever) file, is the recipe.

throw table_exception("(? ???)? ? ???");

There's honestly a ton of ways to do this. The most straight-forward would be to just create a script file that defines the properties of each item, though, its also least flexible in that it makes it difficult to change things after you've shipped.

A data-driven approach is probably preferable, like Derek suggests; you can use JSON or some other format to your liking. Load and parse them at run-time so that you can add or change the items later if you need. There's a way to specifiy that an arbitrary file is meant to be published with your Unity game package, rather than processed at build-time. You might want to do some basic hiding/obfuscation of any plain-text files, just to deter the casual cheater, one easy way is to put these things in a .zip file, or to do some basic XOR "encryption" -- but don't get caught up trying to come up with a perfect anti-cheat thing because you won't (people much smarter than you and I haven't yet in their entire careers, you or I aren't going to do it in our spare time) all you want is to prevent people discovering easy cheat attacks while casually browsing your game files.

More generally, the tendency with Unity, especially if you're less experienced, is to think of everything in the game as *the* thing -- you have a game with 12 loaves of bread, and you literally have 12 loaves of bread defined inside Unity, and maybe you help yourself out with prefabs. But what your really want in this instance is not to define disticnt game-objects for each instance, but to be able to create instances from a kind of recipe. What you store in the JSON (or whatever) file, is the recipe.

Use a json string like this.

[

{"id":"Sword1", "attack":"5", "crit":2},
{"id":"Sword2", "attack":"12", "crit":1}
]

and then parse it and load up your object that way.

Or create prefabs in unity for all your objects and have a weapon component that holds all the weapon data. Then you can use the unity editor to edit these weapons.

So like WeaponData would be a component with an attack and crit variable.

Then create a game object and add the component to it. From here you can now save the game object as a prefab. You can now copy and past this prefab to your hearts content and rename then and have each as a different weapon which you will be able to edit individually.

Thanks so much to you both for the tips, I know almost nothing about JSON, so I'll try to know better and then say it solved my problem... I believe that is the best way. The JSON is supported by Unity or any additional plugin is required? Thanks!

There's honestly a ton of ways to do this. The most straight-forward would be to just create a script file that defines the properties of each item, though, its also least flexible in that it makes it difficult to change things after you've shipped.

A data-driven approach is probably preferable, like Derek suggests; you can use JSON or some other format to your liking. Load and parse them at run-time so that you can add or change the items later if you need. There's a way to specifiy that an arbitrary file is meant to be published with your Unity game package, rather than processed at build-time. You might want to do some basic hiding/obfuscation of any plain-text files, just to deter the casual cheater, one easy way is to put these things in a .zip file, or to do some basic XOR "encryption" -- but don't get caught up trying to come up with a perfect anti-cheat thing because you won't (people much smarter than you and I haven't yet in their entire careers, you or I aren't going to do it in our spare time) all you want is to prevent people discovering easy cheat attacks while casually browsing your game files.

More generally, the tendency with Unity, especially if you're less experienced, is to think of everything in the game as *the* thing -- you have a game with 12 loaves of bread, and you literally have 12 loaves of bread defined inside Unity, and maybe you help yourself out with prefabs. But what your really want in this instance is not to define disticnt game-objects for each instance, but to be able to create instances from a kind of recipe. What you store in the JSON (or whatever) file, is the recipe.

Use a json string like this.

[

{"id":"Sword1", "attack":"5", "crit":2},
{"id":"Sword2", "attack":"12", "crit":1}
]

and then parse it and load up your object that way.

Or create prefabs in unity for all your objects and have a weapon component that holds all the weapon data. Then you can use the unity editor to edit these weapons.

So like WeaponData would be a component with an attack and crit variable.

Then create a game object and add the component to it. From here you can now save the game object as a prefab. You can now copy and past this prefab to your hearts content and rename then and have each as a different weapon which you will be able to edit individually.

Thanks so much to you both for the tips, I know almost nothing about JSON, so I'll try to know better and then say it solved my problem... I believe that is the best way. The JSON is supported by Unity or any additional plugin is required? Thanks!

https://www.nuget.org/packages/Unity.Newtonsoft.Json/

There is a port of JSON.Net for Unity that you can use, there are also a bunch of json libraries in the asset store.

[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!

There's honestly a ton of ways to do this. The most straight-forward would be to just create a script file that defines the properties of each item, though, its also least flexible in that it makes it difficult to change things after you've shipped.

A data-driven approach is probably preferable, like Derek suggests; you can use JSON or some other format to your liking. Load and parse them at run-time so that you can add or change the items later if you need. There's a way to specifiy that an arbitrary file is meant to be published with your Unity game package, rather than processed at build-time. You might want to do some basic hiding/obfuscation of any plain-text files, just to deter the casual cheater, one easy way is to put these things in a .zip file, or to do some basic XOR "encryption" -- but don't get caught up trying to come up with a perfect anti-cheat thing because you won't (people much smarter than you and I haven't yet in their entire careers, you or I aren't going to do it in our spare time) all you want is to prevent people discovering easy cheat attacks while casually browsing your game files.

More generally, the tendency with Unity, especially if you're less experienced, is to think of everything in the game as *the* thing -- you have a game with 12 loaves of bread, and you literally have 12 loaves of bread defined inside Unity, and maybe you help yourself out with prefabs. But what your really want in this instance is not to define disticnt game-objects for each instance, but to be able to create instances from a kind of recipe. What you store in the JSON (or whatever) file, is the recipe.

Use a json string like this.

[

{"id":"Sword1", "attack":"5", "crit":2},
{"id":"Sword2", "attack":"12", "crit":1}
]

and then parse it and load up your object that way.

Or create prefabs in unity for all your objects and have a weapon component that holds all the weapon data. Then you can use the unity editor to edit these weapons.

So like WeaponData would be a component with an attack and crit variable.

Then create a game object and add the component to it. From here you can now save the game object as a prefab. You can now copy and past this prefab to your hearts content and rename then and have each as a different weapon which you will be able to edit individually.

Thanks so much to you both for the tips, I know almost nothing about JSON, so I'll try to know better and then say it solved my problem... I believe that is the best way. The JSON is supported by Unity or any additional plugin is required? Thanks!

https://www.nuget.org/packages/Unity.Newtonsoft.Json/

There is a port of JSON.Net for Unity that you can use, there are also a bunch of json libraries in the asset store.

I understood, I will try to use this port, thank you!!

This topic is closed to new replies.

Advertisement