How to: Gold Income per second

Status
Not open for further replies.
Level 5
Joined
Aug 21, 2012
Messages
107
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:
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.
 
Here is a JASS solution, assuming you know and want to use JASS.
JASS:
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.
 
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.
 
Status
Not open for further replies.
Top