Jump to content

SetMaterial/GetMaterial crash


beo6
 Share

Recommended Posts

Hello,

 

i was looking to reproduce the crash that i have sometimes when opening a new project but instead i noticed some other crash that got introduced in the last update.

 

When i change the Material of a box brush it gets white.

 

when i change the material of a model after that it crashes. However when i change the material of the model first it seems to work.

 

i use the same method in my small game and it crashes directly and i do not change the material of a brush there.

 

I hope the project helps here more since it is a bit hard to explain.

 

//Edit: sorry forgot to press "Attach This file". smile.png

 

//Edit2: i should also descripe how to see the crash in the attached project when running:

Press 1 to switch materials of the model (the ball on the right)

you can press it as often as you want.

 

Now press 2 to switch the model of the box. You can still press it as often as you want, however the texture gets white.

 

If you now press 1 again it crashes.

 

The attached Script to both entities is the following:

function Script:Start()

self.currentAppearance = "visible"

self.defMaterial = self.entity:GetMaterial()

self.invMaterial = Material:Load("Materials/Effects/Invisible.mat")

end

 

function Script:ChangeAppearance(appearancename)

if ( appearancename == "hidden" ) then

self.entity:SetMaterial(self.invMaterial)

end

if ( appearancename == "visible" ) then

self.entity:SetMaterial(self.defMaterial)

end

self.currentAppearance = appearancename

end

 

function Script:UpdatePhysics()

local window = Window:GetCurrent()

 

if (window:KeyHit(Key.D1)) then

if self.currentAppearance == "visible" then

self:ChangeAppearance("hidden")

else

self:ChangeAppearance("visible")

end

end

end

(except the Key to press is different of course)

 

//Edit3: It might be actually more of a problem with GetMaterial() because when i directly assign the ball.mat as material it does not crash.

Link to comment
Share on other sites

  • 1 month later...

I think when you set the material, the old material gets released, and since the reference count reaches zero it is deleted:

http://www.leadwerks.com/werkspace/page/documentation/_/getting-started/reference-counting-r764

 

I've corrected your script below. Notice I added a Delete function to clean up those two materials:

function Script:Start()

self.currentAppearance = "visible"

self.defMaterial = self.entity:GetMaterial()

self.defMaterial:AddRef()-- this will keep the material from getting deleted automatically!!!

self.invMaterial = Material:Load("Materials/Effects/Invisible.mat")

end

 

function Script:ChangeAppearance(appearancename)

if ( appearancename == "hidden" ) then

self.entity:SetMaterial(self.invMaterial)

end

if ( appearancename == "visible" ) then

self.entity:SetMaterial(self.defMaterial)

end

self.currentAppearance = appearancename

end

 

function Script:UpdatePhysics()

local window = Window:GetCurrent()

 

if (window:KeyHit(Key.D1)) then

if self.currentAppearance == "visible" then

self:ChangeAppearance("hidden")

else

self:ChangeAppearance("visible")

end

end

end

 

function Script:Delete()

if self.defMaterial~=nil then

self.defMaterial:Release()

end

if self.invMaterial~=nil then

self.invMaterial:Release()

end

end

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...