How do I make a Weapon System using maps? [Solved]

homeofdeath212

  • Posts: 113
I'm trying to make a Weapon System using maps(since it seems like the easiest and more organized way of doing it) and I can't seem to wrap my head around how to make it work.

First. I want this system to be able to contain all the current weapons available(in-game since more weapons are made available by unlocking them through gameplay) in the game and easy to insert new weapons when needed.

Second. I want this system to store every weapon's individualized stats (like Weapon Level, XP,  and etc) and is easily referable whenever the player switch to a new weapon(or switches back to the old weapon)

The code attached is my current attempt of a "storage".  I can't seem to figure out how to insert and update the storage each time a new weapon is inserted into it.


Edit 1:
I finally found a way to make it work. Thanks for the suggestions. Those really helped me "shape up" the coding

« Last Edit: July 03, 2018, 08:50:58 am by homeofdeath212 »
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

merrak

  • *
  • Posts: 2378
I think the biggest problem you're going to face is storing all your data. I have a system with the same requirements you described. What worked for me is using XML files, stored in the extras folder. It's very easy to implement FAST XML parser (I even have published an extension for this), which you can use to read your data into your game.

For example, here's one of my item entries.

Code: [Select]
<asset  type="object"
name="Iron Dagger"
asset=""
icon="Dagger Card"
actions="equip,disarm,drop,take"
typename="weapon"
canSave="true">
<description>It's an iron dagger. The edges are dull. It looks like it's seen some action.</description>
<statistic name="Damage Mean" value="8" hidden="true"></statistic>
<statistic name="Damage Low STD" value="4" hidden="true"></statistic>
<statistic name="Damage High STD" value="4" hidden="true"></statistic>
<statistic name="Damage vs Armor" value="3"></statistic>
<statistic name="Stunning Factor" value="5"></statistic>
<statistic name="Attack Time" value="0.40" hidden="true"></statistic>
<statistic name="Material" value="Iron"></statistic>
<statistic name="Attack Rate" value="A bit sluggish"></statistic> <!-- For card display only. Adjective is based on attack time -->
<statistic name="Move Points" value="12" hidden="true"></statistic>
</asset>

It's easy to go into the file and change whatever values I need to, add new items, etc. If you see where I defined "icon" and "asset" (unused), these specify actor types for Stencyl to load under certain scenarios, so it's easy to organize all the necessary data, not just statistics.

I read all of this data into a class, but you can use maps just as easily. The map key could be a concatenation of the weapon name and the statistic. So long as the objects have unique names, this should work for your scenario.

For my case, I read everything at once into a database. But adding items on an as-needed basis is also perfectly fine--just call the FAST routines whenever you need to.

One word of caution: FAST is appropriately named--it's easy to implement. User friendliness, on the other hand, is iffy. If I type an XML tag wrong, forget a quote, or some other minor thing, the game either crashes or worse, loads only some of the file and then stops. So you'll want to do some verification that everything loads correctly if you go this route.

homeofdeath212

  • Posts: 113
I apologize for the late reply.
Is there's a step by step way to do this? Or where can I find the FAST XML extension?
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

merrak

  • *
  • Posts: 2378
...Is there's a step by step way to do this? Or where can I find the FAST XML extension?

The extension can be found here: http://community.stencyl.com/index.php?topic=48381.0 along with links to documentation and a sample project. If you don't have it already, take a look at ETHProductions' external data extension http://community.stencyl.com/index.php/topic,35620.0.html. What you do is read your xml file into a text attribute, then use the FAST extension blocks to pick it apart and get your data out of it.

homeofdeath212

  • Posts: 113
A little bit daunting but I think I can understand it.
Also, I can only use this to read things only right? I can't modify the XML file if I want to change a text (in-game) ?

By the way, After reading the documentation and trying it myself.  I couldn't seem to put a block inside any of the blocks from the Fast XML extension. Am I missing something?
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

merrak

  • *
  • Posts: 2378
Which blocks are you having trouble with?

This is the example from the sample project. If you haven't yet, I'd suggest to download it and verify everything's working.

If you have the external data extension, you can replace the code block. FAST is a parsing utility, but you can use typed code or the external data extension to write XML files. I added an example from my own project, but it's raw code, not blocks.

Keep in mind that Stencyl can save map attributes so long as the contents are numbers or text, so if you're just wanting to save the player's progress, you shouldn't need to bother with writing XML files. You'll need to implement XML saving if you want to make permanent changes to your weapon data (e.g. you're writing a weapon editor).

homeofdeath212

  • Posts: 113
 I tried the demo and it worked without any problems(even though it did gave me an error initially because I imported the demo first before the extension itself) and afterwards tested the demo again with a slightly edited XML file and I got it to work just fine

My problem is that I can't put anything inside the XML blocks(the space provided) with another block(even from the same extension) like this:


Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

merrak

  • *
  • Posts: 2378
That's weird. Is your XML data block a text attribute?

homeofdeath212

  • Posts: 113
It is. In fact, it's from the demo itself not changed at all.
I simply just moved it and I can't put it back in unless I press Undo
Currently Working On: Galaxiconia (A space shooter/defence game)
https://www.newgrounds.com/dump/item/2af922bd9a873856b9b2274737f80bc5

merrak

  • *
  • Posts: 2378
Interesting... it doesn't work for me either. I developed the extension on an older version of Stencyl (build 91--). Maybe something changed by 9300.

If you change the block definition for "fastconstructor" in blocks.xml to what I have below, that should fix the problem. I changed one of the void tags to text. The other ones that are left void are intentional, and so you can only type in most of the blanks. That's because FAST doesn't use strings to look things up. Rather, it seems to build a class with fields defined by your XML file's structure.

Code: [Select]
<block tag="fastconstructor"
       spec="parse xml data %0 with root node %1"
       code="{ var xml = Xml.parse( ~ ); var ~ = new haxe.xml.Fast( xml.firstElement( ) ); ~ }"
       type="wrapper"
       color="charcoal"
       returns="void">
<fields>
<text order="0"></text>
<void order="1"></void>
<code-block order="2"></code-block>
</fields>
</block>

NobodyX

  • *
  • Posts: 1228
I think I might see the problem. If I understand things correctly then it's messed up because of the "value of [Currently Selected Weapon] for [Weapon Selection Storage]" part, and you need to just replace it with the game attribute "Currently Selected Weapon" instead.

You want the map to look something like

Weapon_Level_WeaponName1 => value
Current_Exp_WeaponName1 => value
etc
Weapon_Level_WeaponName2 => value
Current_Exp_WeaponName2 => value
etc
Weapon_Level_WeaponName3 => value
Current_Exp_WeaponName3 => value
etc

right?

I can't see the blocks you're using to add additional weapons but if they're the same as in the screenshot, then the map will look something like

WeaponName1 => Bullet
WeaponName2 => Bullet
WeaponName3 => Bullet
etc
Weapon_Level_Bullet => value
Current_Exp_Bullet => value

and each time you use a different weapon you're not writing to new map keys for each weapon, you're just overwriting the ones that end with "Bullet".

If you're making or debugging things like this then it helps a ton to use the "print" block to see what the contents of the map are, in case right now you're trying to do it blind.