Moderators IIYAMA Posted November 25, 2015 Moderators Share Posted November 25, 2015 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_ 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 Link to comment
vx89 Posted November 25, 2015 Share Posted November 25, 2015 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(). 1 Link to comment
Moderators IIYAMA Posted November 25, 2015 Author Moderators Share Posted November 25, 2015 Wtf, that works O_o Thank you very much! Link to comment
MTA Team botder Posted November 26, 2015 MTA Team Share Posted November 26, 2015 A particular resource helpmanager uses that "hack": https://github.com/multitheftauto/mtasa ... nt.lua#L48 Link to comment
Moderators IIYAMA Posted November 26, 2015 Author Moderators Share Posted November 26, 2015 Ah I see. Indeed a sort of 'hack'. Link to comment
Noki Posted January 22, 2016 Share Posted January 22, 2016 Sorry for the bump, but can anyone please explain how that fix works? Link to comment
Moderators IIYAMA Posted January 22, 2016 Author Moderators Share Posted January 22, 2016 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. Link to comment
Noki Posted January 22, 2016 Share Posted January 22, 2016 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. Link to comment
Moderators IIYAMA Posted January 22, 2016 Author Moderators Share Posted January 22, 2016 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. Link to comment
vx89 Posted January 22, 2016 Share Posted January 22, 2016 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. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now