Jump to content

How to delete entity and re-use variable


Alienhead
 Share

Recommended Posts

How to completely remove an entity that uses the same variable in the same routine.

I have two variables that hold references to two weapon entities the player uses. 

Scenario:  Player changes weapons but nulling the variable holders for the previous weapons dont work because the new weapons fill in the varaible preventing it from being deleted.

    -- drop old weps
    self.weplefthand = nil
    self.weprighthand = nil
    collectgarbage()
 
    self.weplefthand = NewWepToHold1
    self.weprighthand = NewWepToHold2

I'm only happy when I'm coding, I'm only coding when I'm happy.

Link to comment
Share on other sites

1 hour ago, Alienhead said:

Player changes weapons but nulling the variable holders for the previous weapons dont work because the new weapons fill in the varaible preventing it from being deleted.

It should not prevent it at all. Are you sure there are no other pointers exist to those weapons?

Example how same reusing of var deletes prev entity without other references to it:

local displays = GetDisplays()
local window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[1], WINDOW_CENTER | WINDOW_TITLEBAR)
local framebuffer = CreateFramebuffer(window)
local world = CreateWorld()
-- Create a camera
local camera = CreateCamera(world)
camera:SetClearColor(0.125)
camera:SetFov(70)
camera:Move(0, 2, -8)
-- Create light
local light = CreateBoxLight(world)
light:SetRotation(45, 35, 0)
light:SetRange(-10, 10)

local box = CreateBox(world)
box:SetPosition(1, 1, 1)

-- Main loop
while window:Closed() == false and window:KeyHit(KEY_ESCAPE) == false do
    collectgarbage()
    world:Update()
    world:Render(framebuffer)
    if (window:KeyHit(KEY_SPACE)) then
        box = CreateBox(world)
    end
end

 

Link to comment
Share on other sites

On 11/7/2024 at 3:43 AM, Alienhead said:

How to completely remove an entity that uses the same variable in the same routine.

I have two variables that hold references to two weapon entities the player uses. 

Scenario:  Player changes weapons but nulling the variable holders for the previous weapons dont work because the new weapons fill in the varaible preventing it from being deleted.

    -- drop old weps
    self.weplefthand = nil
    self.weprighthand = nil
    collectgarbage()
 
    self.weplefthand = NewWepToHold1
    self.weprighthand = NewWepToHold2

There is no need to set those variables to nil at all.

-- drop old weps
self.weplefthand = NewWepToHold1
self.weprighthand = NewWepToHold2
collectgarbage()

If the old objects are not being deleted, you must have the same objects referenced somewhere else. You can call SetHidden() to hide them, if nothing else works.

In Leadwerks, stuff like this could cause invalid pointer errors, which are impossible to detect and cause random memory overwrite. This was the #1 issue I saw causing problems in games made by the community.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...