User:Bkiro/sandbox

From Valve Developer Community
Jump to: navigation, search
I update this page as i learn.
This page is made by me to keep track of what i've learned and also as a way to revise in case i come up with some problems. It can also help fellow coders getting into Dota 2 Workshop tools to help themselves.


Codes

My Codes

Displaying Popup Message When Game Starts (Beginner)

This will Display An ingame Popup Message When The Game Starts ( Pre Game )

In scripts\vscripts\addon_game_mode.lua

-- addon_game_mode.lua

-- First we set up a listener

function Activate()
	GameRules.AddonTemplate:InitGameMode()
end

function CAddonTemplateGameMode:InitGameMode()
	GameRules:GetGameModeEntity():SetThink( "OnThink", self, "GlobalThink", 2 )
        -- This above thinker is irrelevent for this example

	ListenToGameEvent("game_rules_state_change",show_message,nil)
        -- This will run the function "show_message" whenever the event "game_rules_state_change" occurs.

end 

function show_message()
        g_state=GameRules:State_Get() -- This Line will assign the game state to the variable g_state
        if g_state == DOTA_GAMERULES_STATE_PRE_GAME then
                ShowGenericPopup( "#popup_title", "#popup_body", "", "", DOTA_SHOWGENERICPOPUP_TINT_SCREEN )
                -- "#popup_title" and "#popup_body" can be edited in resource\addon_english.txt
        end
end

In resource\addon_english.txt

--> addon_english.txt
"lang"
{
	"Language"		"English"
	"Tokens"
	{		
		"addon_game_name"			"name_of_your_addon"
		"popup_title"                           "Title of your popup message"
    		"popup_body"                     	"What message do you wanna give"
	}
}

Check for enemy units alive on the map and declare victory if all are dead (Beginner)

In scripts\vscripts\addon_game_mode.lua

-- addon_game_mode.lua


function Activate()
	GameRules.AddonTemplate = CAddonTemplateGameMode()
	GameRules.AddonTemplate:InitGameMode()
end

function CAddonTemplateGameMode:InitGameMode()
	GameRules:GetGameModeEntity():SetThink( "OnThink", self, "GlobalThink", 2 )
	ListenToGameEvent("game_rules_state_change",initiate,nil) -- setting up the listener
	GameRules:SetPreGameTime( 10.0 ) -- Self Explanatory
	GameRules:SetGoldTickTime( 10.0 ) -- Self Explanatory
end

function initiate()
	g_state=GameRules:State_Get()
	if g_state == DOTA_GAMERULES_STATE_GAME_IN_PROGRESS then 
		GameRules:GetGameModeEntity():SetThink( VictoryAssesment, 32) 
--[[Delayed because only neutral creeps are there in my game mode and the spawn after 30 secs. so just to be on the safe side, 
32 seconds delay was put so that it will not check for units until they spawn]] 
	end
end

function VictoryAssesment()
	local unit = FindUnitsInRadius(DOTA_TEAM_GOODGUYS,
                              Vector(0, 0, 0),
                              nil,
                              FIND_UNITS_EVERYWHERE,
                              DOTA_UNIT_TARGET_TEAM_ENEMY,
                              DOTA_UNIT_TARGET_ALL,
                              DOTA_UNIT_TARGET_FLAG_NONE,
                              FIND_ANY_ORDER,
                              false)
 -- for more info on FindUnitsInRadius, Check in the Technique section.
	if #unit==0 then -- #unit will count the number of units stored in the unit table
		GameRules:SetGameWinner(DOTA_TEAM_GOODGUYS)
		return nil
	end
	return 1
end

References

Finding Units

Techniques

Finding Units

FindUnitsInRadius()

Tip.pngTip:Read the Discussions page to see an example on how it can be applied even for moving points

Calculating distance between 2 points

local distance = (point1 - point2):Length2D()

point1 and point2 are position vectors on the map

To get position of a particular unit we can use the GetAbsOrigin function as

local point = unit:GetAbsOrigin()


Thinking

Firstly what is Thinking ?

Now heres how Thinker functions are used.