Samples and Examples (Help!)


#1

HI,
I’m having fun exploring chaiScript - and discovering how to do stuff, slowly!

I’m no expert C++ programmer, and struggle to get my head around what’s going on with some of the stuff - so I am hoping you can help.

In another post (Getting started with The API, you ask about specific examples of what people would like to see done - Well, consider this such a request!

A C++ Class called Entity with a few properties - say width, height , x, y and name

A C++ Class - let’s call it Factory - with a Vector of Entity.
Factory has a function
Entity* CreateEntity()
that creates an entity and adds it to the Vector, returning the created Entity.

A chaiScript file - let’s call it FactoryHelper. This has a function called CreateEntities()
This function calls the Factory->CreateEntity() to create an Entity, then sets the width, height, x and y properties of that Entity.
I would like this FactoryHelper script to also contain other functions (say one called UpdateEntity() ) that could also be called separately.

My real-world application for this would be that the FactoryHelper will create several entities - perhaps calculating their x and y coordinates, changing their widths - whatever.

Then the entity itself could call the UpdateEntity() method in the script in order to provide some functionality.

I sort of have some of this working - but haven’t worked out how to call functions in script from c++, or whether I can have multiple functions in the same script file, and call them from different c++ classes…

I appreciate this is a complex scenario for an example - but I think it will be useful for others too.

Using ‘real’ names will help me, certainly, and this example, I think, covers a lot of stuff in a simple scenario.

I am making lots of notes while I investigate chaiScript - and anticipate blogging about it once I get it implemented into my game; I’ll certainly post a link here when I do to help others.

Cheers - and thanks for providing chaiScript!!


#2

I’ve taken a stab at this for a start. Let me know what you think. Feel free to fork it / add to it.

https://github.com/ChaiScript/ChaiScript/blob/add_examples/samples/factory.cpp

Comment on this thread or on github, etc.

-Jason


#3

That’s awesome! Thank you so much.

I will have a play with this over the weekend - but looks like exactly what I was trying to do - and done much more elegantly than I could :wink: