Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic accessor #29

Closed
DanielElam opened this issue Dec 19, 2015 · 7 comments
Closed

Dynamic accessor #29

DanielElam opened this issue Dec 19, 2015 · 7 comments
Assignees

Comments

@DanielElam
Copy link

In my library, I have a class with a dictionary, and I want scripts to be able to directly access them, e.g myobject.ChildObject.DeeperChildObject.

I made the base class inherit from DynamicObject, and overrode "TryGetMember", but the method does not get invoked when accessing an object in the LuaGlobalTable. I was going to try inherit from LuaTable, but this causes issues with protobuf-net/serialization.

How could I achieve this with NeoLua?

@neolithos
Copy link
Owner

Do you have some code snippets?
Overall it sounds okay.

@DanielElam
Copy link
Author

Class snippets

public abstract class Instance : DynamicObject
{
        public Dictionary<string, Instance> Children = new Dictionary<string, Instance>();

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            Instance value;

            if (Children.TryGetValue(binder.Name, out value))
            {
                result = value;
                return true;
            }

            return base.TryGetMember(binder, out result);
        }
}

public class Workspace: Instance
{
}

public class Part : Instance
{
}

An instance of Workspace is added to the global table.

var workspace = new Workspace();
workspace.Children.Add("Part", new Part());

var global = new LuaGlobal(_lua)            
{
    ["workspace"] = workspace,
};

Script executed with

var chunk = _lua.CompileChunk("print(workspace.Part)", "test", new LuaCompileOptions());
var result = global.DoChunk(chunk);

I should be able to access it with

print(workspace.Part)

@neolithos
Copy link
Owner

Hi, I know what is going on.
I did a small fix, but I will need more time to evaluate the problem.

Can you see, if the commit helps you?

@DanielElam
Copy link
Author

That works great! Thanks, that's really helpful.

@neolithos neolithos reopened this Dec 21, 2015
@neolithos
Copy link
Owner

Becareful, I only checked GetMember.

@neolithos
Copy link
Owner

The branche "LuaTable" has the correct solution for this problem. If you want to check it out. But becareful, it was a big rewrite. And I am not finished, yet.

@neolithos
Copy link
Owner

This commit 91181ee should solve your issue finally.

Can you give me feedback?

@neolithos neolithos self-assigned this Mar 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants