Checking whether item was bought before

Bonzo

New Member
Reaction score
4
I want heroes to be able to buy spells in shops. The shops sells powerups which run simple triggers that add the spell. The players now need to get their money back if the spell they bought is already learnt by the hero (there's only 1 hero per player). I made this trigger which detects whether the item bought has already been bought by the player.


Item name: chain lightning
String: Skills[index]
TempBoolean is just a helper variable.
Code:
Chain Lightning
    Events
        Unit - Shop 0001 <gen> Sells an item
    Conditions
        (Item-type of (Sold Item)) Equal to Chain Lightning
    Actions
        For each (Integer A) from 1 to (Length of Skills[(Player number of (Owner of (Buying unit)))]), do (Actions)
            Loop - Actions
                For each (Integer B) from 1 to (Length of Skills[(Player number of (Owner of (Buying unit)))]), do (Actions)
                    Loop - Actions
                        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                            'IF'-Conditions
                                (Name of (Sold Item)) Equal to (Substring(Skills[(Player number of (Owner of (Buying unit)))], (Integer A), (Integer B)))
                            'THEN'-Actions
                                Set TempBoolean = True
                            'ELSE'-Actions
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            'IF'-Conditions
                TempBoolean Equal to False
                (Unit-type of (Buying unit)) Equal to Elemental Mage
            'THEN'-Actions
                Unit - Add Chain Lightning  to (Buying unit)
                Set Skills[(Player number of (Owner of (Buying unit)))] = (Skills[(Player number of (Owner of (Buying unit)))] + (Name of (Sold Item)))
            'ELSE'-Actions
                Player - Add 10 to (Owner of (Buying unit)) Current Gold
                Display Text: 'Spell already bought or spell doesnt match with buying hero.'
        Set TempBoolean = False

The problem is that there's a string created for every player and I heard strings take much place in the cache (or anywhere else). Also the search for a content in the string can include MANY comparisons.
I'm sure that there is a systematic solution for this problem - maybe in this forum. But I havent found it yet. Do you know anything else?
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Some alternative would be to create two arrays:
Set Items[1] = Item 1
Set Items[2] = Item 2
Set Items[3] = Item 3
...

Set Abilities[1] = Ability you get for item 1
Set Abilities[2] = Ability you get for item 2
Set Abilities[3] = Ability you get for item 3
...


And something like this:

Event:
- A unit acquires an item
Conditions:
Actions:
Set Index = 0
For each Integer A from 1 to "number of items / abilities in array"
- If Item-type of (Item being manipulated) equal to Items[Integer A] then
- - Set Index = Integer A
If Index equal to 0 then
- Skip remaining actions
If (Level of Abilities[Index] for (Triggering unit)) greater than 0 then
- Game - Display... "already have that item"
- Player - Add Gold
Else
- ... you don't have it yet
- Unit - Add Abilities[Index] to (Triggering unit)
 

Bonzo

New Member
Reaction score
4
Thanks AceHart that's what I wanted. I didnt know the 'Skill level for unit' comparison before. This is what I have now.
I gave index numbers to the heroes so that i can prove whether the spell can be learned by the hero. Every herotype can choose between 10 different spells:
Herotype[1]: Spells 1-10
Herotype[2]: Spells 11-20
and so on...

Helper_Int = the index of the Spell/item
SpellbuyHelp = index of buying herotype

Code:
Buy Spells
    Events
        Unit  - Shop 0001 <gen> Sells an item
    Conditions
    Actions
        Set Helper_Int = 0
        For each (Integer A) from 1 to 40, do (Actions)
            Loop - Actions
                If ((Unit-type of (Buying unit)) Equal to HeroType[(Integer A)]) then do (Set SpellbuyHelp = (Integer A)) else do (Do nothing)
                If ((Item-type of (Sold Item)) Equal to Spellitems[(Integer A)]) then do (Set Helper_Int = (Integer A)) else do (Do nothing)
        If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            'IF'-Conditions
                And - All (Conditions) are true
                    Conditions
                        ((SpellbuyHelp x 10) - 10) < Helper_Int
                        (SpellbuyHelp x 10) >= Helper_Int
            'THEN'-Actions
                If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                    'IF'-Conditions
                        (Level of Spells[Helper_Int] for (Buying unit)) > 0
                    'THEN'-Actions
                        Display the text: ‘Already Bought’
                        Player - Add 10 to (Owner of (Buying unit)) Current Gold
                    'ELSE'-Actions
                        Unit - Add Spells[Helper_Int] to (Buying unit)
            'ELSE'-Actions
                Player - Add 10 to (Owner of (Buying unit)) Current Gold
                Display the text: ‘Wrong Class’

Thanks, you helped me a lot.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top