PDA

View Full Version : Mount a script as global function ?



Spider_hip
04-04-2011, 11:02 AM
Hi

I created a tiny code which handles camera's mouse wheel zoom in and out smootly, also looks for mouse position, so if you want to zoom in to upper left side, it zooms and position itself to mouse coordinates.

I created this code in rpg_mouse style test game's top_player_mouse script.

I always wondered and couldn't success this on my programming advanture life;

Is there anyway to take this code out of top_player_mouse script and create a mouse zoom script in systems file to call that script as global from anywhere. So In everygame which I create , I can call it and use it without need to rewrite again.

I mean I want it to be like engines module. Like how I use camera:set_scale fucntion anywhere as I want. I want to use my code as

camerazoom(max zoom limit, min zoom limit, zoom speed, move speed) anywhere.

I hope I can describe what I want clearly.

Oh and here is my code; ( I deleted camera track player function of course, and I don't write other game's codes here, you already know rpg_mouse control game)

function OnPostInit()
GetCamera:SetMoveLerp(0.05);
GetInputManager:AddBinding("mouse_wheel_up", "OnMouseWUp", this:GetID());
GetInputManager:AddBinding("mouse_wheel_down", "OnMouseWDown", this:GetID());
end
xkamera = 1;
ykamera = 1;

function OnMouseWDown(bKeyDown)
if (bKeyDown == false) then
return true;
end

xkamera = xkamera-0.02
ykamera = ykamera-0.02

local vPos = ScreenToWorld(GetInputManager:GetMousePos())

GetCamera:SetScaleTarget(Vector2(xkamera,ykamera))

GetCamera:SetPosCenteredTarget(Vector2(vPos))
return true;
end

function OnMouseWUp(bKeyDown)
if (bKeyDown == false) then
return true;
end

xkamera = xkamera+0.02
ykamera = ykamera+0.02
local vPos = ScreenToWorld(GetInputManager:GetMousePos())
GetCamera:SetScaleTarget(Vector2(xkamera,ykamera))
GetCamera:SetPosCenteredTarget(Vector2(vPos))
return true;
end

Thank you