Jump to content

resourceRoot element > resource element


IIYAMA

Recommended Posts

  • Moderators
Posted

What is the best way to get the resource from the resourceRoot element?

covert: getResourceRootElement() to getThisResource ( )

--convert: 
resourceRoot  
-- to 
resource 

I am trying to get the resource from an element in the game.

        local elementParent = source 
        local elementsThatHaveParents = 0 
         
         
        repeat 
            local elementParent_ = getElementParent ( elementParent ) 
            if elementParent_ and elementParent_ ~= root then 
                elementParent = elementParent_ 
                elementsThatHaveParents = elementsThatHaveParents+1 
            else 
                elementParent_ = nil 
            end 
        until not elementParent_ 

Tre.png

https://wiki.multitheftauto.com/wiki/Element_tree

And this a workaround which I am not happy about.

local theResourceThatOwnsThisElement = false 
  
local resourceTable = getResources() 
for i=1,#resourceTable do 
    local resource = resourceTable[i] 
    if getResourceRootElement(resource) == elementParent then 
        theResourceThatOwnsThisElement = resource 
        break 
    end 
end 

Posted

Haven't tested, but perhaps this would give you something:

local resource = getResourceFromName(getElementID(resourceRoot)) 

where resourceRoot is the predefined global variable, which is equal to getResourceRootElement() - but of course, you'd not be using this resource root element as otherwise you'd just use getThisResource().

  • Like 1
  • 1 month later...
  • Moderators
Posted
Sorry for the bump, but can anyone please explain how that fix works?
local theResourceRoot = --... resourceRoot from another resource ... 
  
local theResourceName = getElementID(theResourceRoot) -- get the resource name. 
  
local resource = getResourceFromName(theResourceName) -- get the resource element. 

Posted

Spawn yourself a vehicle through a random resource and run some code similar to this.

noki().vehicle.parent.parent --> resourceRoot of the resource that created the vehicle 
noki().vehicle.parent.parent.id --> empty string 
Resource.getFromName(noki().vehicle.parent.parent.id) --> suddenly turns an empty string into a resource element 

I just cannot understand how that process works.

  • Moderators
Posted

I don't work with oop and that repeat loop is required, because you never know which element is the resourceRoot.

local elementParent = vehicle -- < add here the element. 
            repeat 
                local elementParent_ = getElementParent ( elementParent ) 
                if elementParent_ and elementParent_ ~= root then 
                    elementParent = elementParent_ 
                else 
                    elementParent_ = nil 
                end 
            until not elementParent_ 

local theResourceRoot = elementParent 
      
local theResourceName = getElementID(theResourceRoot) -- get the resource name. 
      
local resource = getResourceFromName(theResourceName) -- get the resource element. 

Posted
Spawn yourself a vehicle through a random resource and run some code similar to this.
noki().vehicle.parent.parent --> resourceRoot of the resource that created the vehicle 
noki().vehicle.parent.parent.id --> empty string 
Resource.getFromName(noki().vehicle.parent.parent.id) --> suddenly turns an empty string into a resource element 

I just cannot understand how that process works.

Weird that you get empty string as resource id. You could try non-oop methods and see if there's a bug with any of the oop ones.

Or pehaps vehicle.parent.parent is not resource root element in this case (though it looks correct from the diagram in 1st post).

getResourceFromName("") not returning false is also weird.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...