Looking for treasure chest items (code examples)

Any examples about how to make random treasure chest items would be nice to have?

I usually make an encompassing item class that contains some generic things items have (like weight, a name, and a description). Then I create another class, which would be more specific, like a weapon, which has attack power, attack speed, whether it uses ammo or not, etc. From there, I’d make the actual item class itself - a sword, for example. When you go to your treasure chest itself, you can make it spawn an instance of the sword (or whatever else you put into it).

For randomizing items or weapons, it should be trivial to create an instance of a class and then alter its values somewhat within reason, or add a selection of random modifiers (i.e. “Burning” or “Freezing” or “Black Hole” or whatever):



# This is the treasure chest's code:

from bge import logic

import Items  # Where the items are stored

def treasure_chest(cont):

    obj = cont.owner

    if not 'init' in obj:

        obj['inside'] = random.choice(Items.Sword, Items.Potion, Items.Hammer)

# Later on, when you want to open it:

item = sce.addObject('Item', obj)

item['data'] = obj['inside']()  # Actually creates the instance of the item to "place" in the item, to identify it

obj['inside'] = None


Note that this just “places” one object in the chest, but the idea is there. If you want randomization, then you can either move it to the treasure chest’s code (where it spawns the object and randomizes some of its properties, perhaps within a certain range), or move it to the item (where weapons randomize their different stats, like attack power or weight, when they’re created).

Basically, I think it should revolve around creating a class that you can re-instance for each weapon you want to make. That way, you can easily know what a player has, since it’s an instance of (or contains data that is an instance of) an already existing class.

I would probably keep the display mesh of the item or weapon separate from the data to make it easy to reuse. For example, rather than creating a Hammer weapon and a Fire Hammer weapon and an Ice Sword weapon and so on, I’d just create meshes and make the actual weapon or item a kind of “proxy”. It would replace its mesh depending on the data of the weapon itself, rather than the game literally spawning a hammer when it needs to.

To be honest, this is kind of a complex question / deal to work at - it’s not just “how do I make a randomized weapon”, since how you do that depends on how you’ll attempt to access or use that weapon. Inventory, weapons, and their underlying data is all tied together, but should probably still be separated for ease of use.

One way to represent the items in a chest would be to use a list. You could chose random items like so:

import random

POSSIBLE_ITEMS = ['gold', 'sword', 'arrows', 'axe']
ITEM_COUNT = 5

itemsInChest = []
for i in range ITEM_COUNT:
    itemsInChest.append(random.choice(POSSIBLE_ITEMS))

Thank you, both. The Idea was for a rpg genre or combination of type game…