Overriding setPitch

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
dizzykiwi3
Citizen
Posts: 58
Joined: Tue Jan 14, 2014 2:03 am

Overriding setPitch

Post by dizzykiwi3 »

This probably has a simple solution but I'm a bit unfamiliar with Lua

How would I go about overriding the setPitch function so that I can have it link to a speed value so that it ramps down during slow-motion moments

The way I'm currently trying to do it is

Code: Select all

rampspeed = .1
origsetPitch = Source:setPitch
function Source:setPitch(pitch)
  origsetPitch(pitch * rampspeed)
end
but that isn't working.
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Overriding setPitch

Post by slime »

You could make a completely new function which does what you want, and change your game's code to call the new function instead of Source:setPitch directly. It would also have the added benefit of letting you use Source:setPitch directly in specific cases if you want.

Like this:

Code: Select all

rampspeed = 0.1

function SetRampingPitch(source, pitch)
    source:setPitch(pitch * rampspeed)
end
User avatar
dizzykiwi3
Citizen
Posts: 58
Joined: Tue Jan 14, 2014 2:03 am

Re: Overriding setPitch

Post by dizzykiwi3 »

I was thinking about doing that, it'd just save me some time if I could override it.

I'm also a bit unfamiliar with using the color and self parameters in lua functions so I wanted to try it out

is this syntax close to correct? It isn't working but I think it's close?

Code: Select all

origsetPitch = setPitch
function Source:setPitch(pitch)
  self:origsetPitch(pitch * .1)
end
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: Overriding setPitch

Post by BruceTheGoose »

The self variable refers to the table used to call the method.

Code: Select all


Player = {}
Player.x = 0

function Player:move(x,dt)
   self.x = self.x + x * dt
end

function Player:update(dt)
   self:move(5,dt)
end


"I don't know."
Post Reply

Who is online

Users browsing this forum: No registered users and 105 guests