Jump to content

Change ownership of a variable between resources?


Olle Risk

Recommended Posts

Alright so I have two resources A and B, in A I define a variable:

local myVariable = nil 

In B I have a function that creates an object and assign it to a variable, (the function is exported)

function makeWindow() 
    local myVariable = guiCreateWindow(blahblahblah) 
end 

Now, is it possible to pass myVariable from A to the function in B as an argument, assign the object to the variable defined in A then somehow return it back to A?

Why would I do this? Well this object is big and needs to be accessed by multiple resources and I don't wanna include it into all resources, I want it to be owned by the resource that calls the export (A) so that it doesn't get removed once I restart B.Is this possible?

Link to comment
  • Moderators

Objects that are created by scripting are bounded to the resourceDynamicElementRoot. Once that element is destroyed it will take all his children with him.

When you stop a resource, the resourceRoot is destroyed, which means that all children bellow are destroyed too.

It works like this:

  • resourceRoot
    • resourceDynamicElementRoot
      • Elements created by scripting

getResourceMapRootElement

  • Map elements

Wiki:

Tre.png

Afaik, you can't edit this with setElementParent.

You will have to initialize the elements again once you start it.

Link to comment
-- RESOURCE A 
local myObject = createObject(...) 
myObject = exports.B.manipulateObject(myObject) 
  
-- RESOURCE B 
function manipulateObject(object) 
 local anotherObject = createObject(...) 
 return anotherObject 
end 

You can send and return any element. So, yes, you can do that.

IIYAMA's explanation is irrelevant to your scenario here, if I understood you correctly.

Link to comment

Thanks JR10, that was similar to what I was looking for, but It's still not exactly what I'm trying to do, let's assume this object is a large image created by guiCreateStaticImage in resource B. Using your example with a few modification, would this work?

-- RESOURCE A 
local myObject = nil 
myObject = exports.B.manipulateObject(myObject) 
  
-- RESOURCE B 
function manipulateObject(object) 
    object= guiCreateStaticImage(...) 
    return object 
end 

I mean, would this let resource A be the owner of this image so that if I restart B this image won't disappear from A?

Link to comment
  • Moderators

He could try to create all elements in 1 resource.

-- RESOURCE A 
local myElement = nil 
local elementCustomName = "HelloWorld!" 
myElement = exports.B.createElementForResource( 
    elementCustomName, -- a custom name for your element, to prevent multiply elements of the same use. 
    createObject, -- the functions you want to execute 
    bla,bla,bla,bla -- arguments 
) 
  
-- RESOURCE B 
local customElementNames = {} 
function createElementForResource(...) 
    if isElement(customElementNames[elementCustomName]) then 
        return customElementNames[elementCustomName] 
    else 
        local data = {...} 
        local elementCustomName = data[1] 
        table.remove(data,1) 
        local functionInUSe = data[1] 
        table.remove(data,1) 
  
        local element = functionInUSe(unpack(data)) 
        if element then 
            customElementNames[elementCustomName] = element 
            return element 
        end 
    end 
    return false,"Element failed to create." 
end 

As long this(B) is the only resource the is running, it will work.

Link to comment

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...