1. Find your way through the deepest dungeon in the 18th Mini Mapping Contest Poll.
    Dismiss Notice
  2. A brave new world lies beyond the seven seas. Join the 34th Modeling Contest today!
    Dismiss Notice
  3. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
Hive 3 Remoosed BETA - NOW LIVE. Go check it out at BETA Hive Workshop! Post your feedback in this new forum BETA Feedback.
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

How to: Gold Income per second

Discussion in 'World Editor Help Zone' started by Code_Name_XXX, Mar 28, 2014.

  1. Code_Name_XXX

    Code_Name_XXX

    Joined:
    Aug 21, 2012
    Messages:
    93
    Resources:
    0
    Resources:
    0
    I often wonder and picture my next project would be. But before I do that, I wanted to ask, how do you make a building produce you gold?

    Here's my question::vw_wtf:

    1. How to build a building that gives me 20 gold income every 20 seconds

    2. So it gives me 20 gold per 20 seconds, here's my second question. When I upgraded the building. It should change the gold income per second from 20 to 30. How to do that?

    If this is very easy for you. Just give me the answers and... Thank you for taking time to read my thread.
     
    Last edited: Mar 28, 2014
  2. deathismyfriend

    deathismyfriend

    Joined:
    Oct 24, 2012
    Messages:
    6,526
    Resources:
    14
    Spells:
    12
    Tutorials:
    2
    Resources:
    14
    It is actually simple. The easiest way is to use a hashtable.
    Store the units gold into the hastable based on its unit type.
    Example:
    • Hashtable - Save goldValue as tempUnitType of 0 in goldHashtable

    Then store all units that give you gold in a unit group.
    Every 1 second loop through the unit group and load the gold value from the hashtable.
    Then when you are done looping give the gold to the player.
     
  3. Zeatherann

    Zeatherann

    Joined:
    Nov 25, 2008
    Messages:
    1,303
    Resources:
    6
    Skins:
    1
    Tools:
    1
    Maps:
    4
    Resources:
    6
    Here is a JASS solution, assuming you know and want to use JASS.
    Code (vJASS):
    function UnitGoldIncome takes nothing returns nothing
        local timer T=GetExpiredTimer()
        local integer Id=GetHandleId(T)
        local unit U=LoadUnitHandle(udg_Your_Hashtable,Id,1)
        local player P
        if U==null or GetUnitTypeId(U)==0or IsUnitType(U,UNIT_TYPE_DEAD)then
            call DestroyTimer(T)
            call SaveUnitHandle(udg_Your_Hashtable,Id,1,null)
            call SaveTimerHandle(udg_Your_Hashtable,Id,0,null)
            call FlushChildHashtable(udg_Your_Hashtable,Id)
        else
            set P=GetOwningPlayer(U)
            call SetPlayerState(P,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(P,PLAYER_STATE_RESOURCE_GOLD)+LoadInteger(udg_Your_Hashtable,Id,0))
            set P=null
        endif
        set T=null
        set U=null
    endfunction

    function UnitAddGoldIncome takes unit U,integer Gold,integer Tim returns nothing
        local timer T=CreateTimer()
        local integer Id=GetHandleId(T)
        call TimerStart(T,Tim,true,function UnitGoldIncome)
        call SaveInteger(udg_Your_Hashtable,Id,0,Gold)
        call SaveTimerHandle(udg_Your_Hashtable,Id,0,T)
        call SaveUnitHandle(udg_Your_Hashtable,Id,1,U)
        set T=null
    endfunction


    To use it all you have to do is call
    UnitAddGoldIncome()
    and it will then give the owner income until the building is not alive, or doesn't exist anymore. For use with building upgrades it might get tricky though. One work-around is to re-add the building for the difference of the incomes (new income - old income). If this solution doesn't work for you because you use different systems or want a better function let me know.
     
  4. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,549
    Resources:
    23
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    5
    JASS:
    3
    Resources:
    23
    I'm pretty sure he doesnt know jass, else he would know it.

    Just wanna show you a bit different way without hashtables and in gui:

    1.
    Create a trigger that runs every 20 seconds. In this trigger you loop through players and add "Income[PlayerNumber]" to player's gold.

    Income is an integer array variable. For all playing players you can set it to 20 at start of the game.

    2.
    Create a trigger with event a unit finished construction. ( or sth similiar)

    Here you check for unit type of triggering unit and add increase adequate player's income. For example:

    Set tmpInteger = (PlayerNumberOf(TriggeringPlayer))
    If (UnitTypeOf(TriggeringUnit)) == Barracks Level 2 then
    Set Income[tmpInteger] = (Income[tmpInteger] + 10)

    3.
    Create a trigger which runs when a unit dies. Here just check for unit type again and substract the adequate value from player's income. Same methode as used in 2.

    To Zeatherann, I don't like the idea using a new timer for each unit. And
    integer Tim
    should be
    real Tim
    .
     
  5. deathismyfriend

    deathismyfriend

    Joined:
    Oct 24, 2012
    Messages:
    6,526
    Resources:
    14
    Spells:
    12
    Tutorials:
    2
    Resources:
    14
    The above by IcemanBo is what I suggest also. I was in a hashtable mindset when I said my method above.
     
  6. Zeatherann

    Zeatherann

    Joined:
    Nov 25, 2008
    Messages:
    1,303
    Resources:
    6
    Skins:
    1
    Tools:
    1
    Maps:
    4
    Resources:
    6
    I do enjoy IcemanBo's solution as well. It's a shame that sometimes a building could only wait a single second instead of the twenty desired for their first income though.
     
  7. IcemanBo

    IcemanBo

    Joined:
    Sep 6, 2013
    Messages:
    6,549
    Resources:
    23
    Maps:
    3
    Spells:
    11
    Template:
    1
    Tutorials:
    5
    JASS:
    3
    Resources:
    23
    Hm, I see what you mean. But I maybe would prefer synchron income, else maybe it seems like random acting gold increases at random time.