Calling Something Once in Think

Hey guys, I have an if x is true then blah statement in a think function, and I want the statement to only call something once if it’s true and not keep calling it every tick. Can anyone help me?

What are you trying to run? I don’t see why you would need to only run it once in think.

Regardless, this is how you would do that:

[lua]hook.Add(“Think”, “RunOnceThink”, function()
–Your code
hook.Remove(“Think”, “RunOnceThink”)
end
)[/lua]

Actually I meant HUDPaint not Think, I have a function for some buttons hooked on it and I’m trying to do a transition effect when my cursor is above something.

So something like this?

[lua]–Replace this with the condition
local condition = true

hook.Add(“HUDPaint”, “RunOnceHUDPaint”, function()
if condition then
–Your code
hook.Remove(“HUDPaint”, “RunOnceHUDPaint”)
end
end
)[/lua]

That worked on my first transition effect, but then it’s removed forever.

I think I’m not understanding what you want to do.

[lua]
local function Classes()
local color = Color(0, 0, 0, alpha)

if ClassMenu then
	alpha = Lerp((CurTime() - OpenTime) * 10, 0, 200)
end
if not ClassMenu then 
	alpha = Lerp((CurTime() - HideTime) * 3, 200, 0)
end

local client = LocalPlayer()

local x = ScrW()/2
local y = ScrH()/2

local w = 500
local h = 200

local margin = 10

top = gui.MouseY() < 290 and ClassMenu 
if top then -- I want this part to run once everytime i put my mouse over 290 so I can define the start time and position for a transition effect
	start = y
	StartTime = CurTime()
end

hy = Lerp(CurTime() - StartTime, start, y - (h + margin))

DrawOption(x, y - (h + margin), w, h, "SUPPORT", color)
DrawOption(x, y, w, h, "DEFAULT", color)
DrawOption(x, y + (h + margin), w, h, "EVADER", color)

end
hook.Add(“HUDPaint”, “DrawClassMenu”, Classes)
[/lua]

How about…
[lua]if top and not StartTime then[/lua]

As far as I can tell, you don’t set StartTime to nil once it’s ‘started’.

What about after that then.

[lua]
local condition = true
hook.Add(“HUDPaint”,“whatevs”,function()
if condition then
–do your shit
condition = false
end
end)
[/lua]
?

You’ll also need to ‘hook’ it to an event for when the mouse leaves, as to reset the condition.

I had to do this for a game i was making with XNA a while back, you end up with a LOT of if statements.

[editline]17th January 2012[/editline]

This is C#



if (m_rBounds.Contains(a) && Mouse.GetState().LeftButton == ButtonState.Pressed)
            {
                if (doOnce == false)
                {
                    doOnce = true;
                    
                    CALLBACK(m_Callback.Class, m_Callback.Method);
                    
                    //Globals.GlobalContent.TryLuaString(script);

                    Handlers.AudioHandler.PlaySound(Globals.UTIL_Content.FindSoundByName("menu_click"));

                    return;
                }
                return;
            }

            if (doOnce == true)
                doOnce = false;

This is what i have for a custom GUI button written in C#.

You have to return whenever the condition is true. Then after that you set the condition to false, as this’ll get run when the mouse is not inside the bounds.

Mine was written as a ‘Do Once’ for clicking a button, but if you exclude checking for a click, it should work just as well.

Time to break out the spoons