Sign in to follow this  

Lua Scripting w/ generics

This topic is 484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So... I'll start by mentioning that I have never worked with lua. 

 

I have a ECS system in which I have a Component Container.

The component container stores the components as

 

Dictionary<Type,Dictionary<int,object>> Components;

In c# I obtain a component via

 

container.GetComponent<ComponentType>(entityId);

        public T GetComponent<T>(Entity ent)
        {
           //An Entity can be implicitly converted to an int, and an int can be implicity converted to an entity.
 
            if (!_components.ContainsKey(typeof(T)))
            {
                    return default(T);
            }
            return (T)_components[typeof(T)][ent];
        }

I started messing around with a Lua Interperter and got to the point where I wanted to create a script that would allow two entities to interact.

but.. I ran into issues immediatly since I was unable to (in Lua) call the generic method.. (Searching the google seems to indicate that generics in lua are not trivial... and I didn't find any solution.

 

So I added the following function to the component Container

        public object GetComponent(String componentType, int ent)
        {
            var v = _components.Keys.FirstOrDefault(x => x.ToString().EndsWith(componentType));
            if (v == null) return null;
            if (!_components[v].ContainsKey(ent)) return null;
            return _components[v][ent];
        }

 

Although it works, I am not particularly happy with it... I am worried that the string comparison to find the key will be slow. 

 

I am considering replacing the 

Dictionary<Type,Dictionary<int,object>> with a Dictionary<int,Dictionary<int,object>> but this would require that I create a mapping of ComponentType->id and I think that mapping would need to be kept up to date manually... which is also something I'd like to avoid.

 

 

Does anyone have any adivce, suggestions?

 

 

Share this post


Link to post
Share on other sites

In c++ I use a register_component_type<T>(string guid) function which will hash the string id using a stanard/fast hash function and then store the string and type in fast access hash maps with the hashed string id as the key, although I keep another hashmap for the Type to hash id storage also.

 

Then - you can either have

 

GetComponent(int type) - the direct access one

 

GetComponent<T>()

{

     return GetComponent( type_to_hashid_map.find(typeof(T)) );

}

 

GetComponent(string guid)

{

    return GetComponent( fast_hash_func(guid) );

}

 

Where you use the hashed string id instead of the type within the dictionary. Given the type you can look up the hashed id (because you stored it using the register function) or given the string you can use the quick hash function you used to first hash the GUID.

 

Its also easy enough to write out the hashed string GUIDs to file so you can use them directly in your lua script - the hash function you use should always produce the same exact ID for any given string.

 

This is what I do in c++ - but seems like it would work fine.

Share this post


Link to post
Share on other sites

This topic is 484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

Sign in to follow this