Jump to content
  • Advertisement
Sign in to follow this  
Angelic Ice

Unity Changing a C++ vector list via Lua

This topic is 1150 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

Hello gamedev-community smile.png

I am currently working with Lua 5.1 and C++ and want my Lua-script cause C++ to add a new entry to my vector list.

 

My whole game consists of multiple states - for this case the playstate will be active.
Furthermore this state contains a playstate-class whose update()-function runs the Lua-script at some point.
This Lua-script contains multiple instructions for the game - including the instruction to create/remove a pointer to the vector list.

The pointer points at objects of the NPC-class.

--here are the predefinitions

function dialogue_text()
    while true
    do

       blendin ("angry", "y")         --This function shall cause C++ to save a new pointer to a new object in the vector list.
       y "Hi! How are you?"           --Predefined function, changes the text.
       m "I am fine - how about you?" --Same as the last line.

    end
end

blending("angry", "y") contains all important attributes of the new object for now. For example y is just a shortcut for the real name of the character and angry describes a certain state. Nonetheless I am not really happy with the syntax of this function but that is not the main focus here.

 

Secondly there is the C++ header of the class, holding the vector list:

class Npc; //Mentioning that the class Npc exists, this is the object to which a pointer will point.


class Playstate : public Gamestate
{

public:
	Playstate();
	virtual ~Playstate();
	void HandleEvents(Game& game);
	void Update(Game& game);
	void Draw(Game& game);
	

	std::vector<std::shared_ptr<Npc>>::iterator it; //The vector list which shall hold shared pointers to the objects.

	
	typedef std::vector<std::shared_ptr<Npc>> container;
	container c;

private:

//All kind of variables would be defined here.

};


#endif

So if the state manager calls for the playstate-state it's update-function will be called. Now the Lua-script runs and this is where I stuck.

 

As you can see within the Lua-script there are commands like y "Hi! How are you?" which changes a file with a Lua table in it. This table file is being accessed and extracted by my C++-playstate.

 

However I feel like saving the characters in a Lua table and adding/removing these entries is a bit weird.

Specially because there would be two "tables/lists" containing nearly the same information. The Lua table and the vector list (which points to the real object).

 

Additionally being forced to open the Lua-table within the C++-Update() and check if there is something new compared to the vector list is a weird way. I would rather directly manipulate the vector list with a new command/instruction in the Lua-script.

 

On the other hand calling a static int() from the Lua script is not working out because the vector list is inside the playstate-class (or the current running object of this class at execution time). Though this might be a mistake I have done on my end.
 

To summarise my post in questions:

 

How could I access this vector list via Lua? Should I go with my idea of using a Lua-table which will be accessed by C++ when it is needed?

 

Can I use a struct int() for this instead? If yes, how can I make it possible to access the vector list?

 

Additionally, is there a way to change my syntax 'blendin("angry", "y")' to 'blendin angry y'?

 

Thanks for taking your time to read my post. I am looking forward for your ideas and suggestions! If there is any question about my questions/case, then feel free to tell me of course.

Edited by Angelic Ice

Share this post


Link to post
Share on other sites
Advertisement

Assuming there will be multiple Playstate's, blendin() must know exact instance to call.

It might be some global field (either side, lua or c++), or it might be in environment or lexical closure of blendin(), which should be created/initialized per Playstate now.

If you go with static global field on c++ side, your syntax for blendin() won't change. In "static int()" function you read that global field and pass arguments to exact instance of Playstate.

If you go with dynamic per-Playstate initialization, then blendin() itself, or its environment should be initialized with exact Playstate. Then blendin() might be a wrapper to other function that will pass Playstate pointer/reference as first argument, so "static int()" will know where to take Playstate pointer.

 


Additionally, is there a way to change my syntax 'blendin("angry", "y")' to 'blendin angry y'?

 

No. Lua allows calling functions when its only argument is either string or table.

So blendin "angry" "y" is possible, and with some setup blendin { angry, y} also possible (angry and y is some unique values, available from current environment).

But you can't make blending angry y, that's not a valid syntax.

Share this post


Link to post
Share on other sites

Thanks for your help!

However I have a question: What do you mean by "global field"? Like some static global pointer in which I pass the object of the state when I create it?

 

 

 


No. Lua allows calling functions when its only argument is either string or table.

So blendin "angry" "y" is possible, and with some setup blendin { angry, y} also possible (angry and y is some unique values, available from current environment).

But you can't make blending angry y, that's not a valid syntax.
 

 

Oh, not even like predefining keywords similar to local y = function(string)? I once heard about Internal DSL but after a long research I came to no real solution nor an idea how it would work.

Edited by Angelic Ice

Share this post


Link to post
Share on other sites

What do you mean by "global field"? Like some static global pointer in which I pass the object of the state when I create it?

Yes, sort of that. It should store currently active Playstate, updated every time you switch states.

 

 

 


Oh, not even like predefining keywords similar to local y = function(string)?

What do you mean? You can't define new keywords in Lua, and there's no macros in language.

 

 

 


I once heard about Internal DSL but after a long research I came to no real solution nor an idea how it would work.

 

By "internal dsl" keywords I found this: http://www.lua.org/wshop11/luaws11_ag.pdf

Do you mean the same by "internal"?

All examples there built using Lua's syntactic sugar - parentheses around function's arguments can be omitted when there's just one argument, and it's either table constructor or string literal.

That's how you can implement blendin "angry" "y" - blendin() takes first string literal "angry", and returns another function. That second function will take second literal "y", and might finally do something. E.g:

local function blendin(key)
    return function(value)
        print('Want make assignment "'..key..'"="'..value..'"')
        -- do some actual work to create new entry in c++ list
    end
end

blendin "angry" "y"

Note that in case of 'blendin angry y' row - angry and y is not a string literal, those are variables, local or global.

So it's not the case of calling function with string literal or table constructor, and will be treated as syntax error.

Edited by vstrakh

Share this post


Link to post
Share on other sites

If you really wanted to you could add #define capabilities to Lua by simply running the C preprocessor on your scripts before executing them (it's not hard) but I think it would be a questionable approach for what you're trying to do.

 

My suggestion: stop trying to come up with a DSL and just use normal Lua constructs. There's quite a bit of syntactic sugar available so you can be creative and still manage to write something nice without resorting to horrible hacks just to e.g. remove parentheses.

Share this post


Link to post
Share on other sites

Yes, sort of that. It should store currently active Playstate, updated every time you switch states.

 

I managed to make it work! Thanks a lot smile.png

 

For the ones who might have the same problem, here is my solution to it:

 

- I created an extra pointer outside of the Playstate-class.

- Before the Playstate-class is going to call the Lua-script, I pointer = this; the object to the pointer.

- Lua runs the blendin()-function and ends up calling the static int() within my C++ code.

- The static int() accesses the pointer and does: pointer->container.push_back(const char* var);

 

 

 




By "internal dsl" keywords I found this: http://www.lua.org/wshop11/luaws11_ag.pdf

Do you mean the same by "internal"?

All examples there built using Lua's syntactic sugar - parentheses around function's arguments can be omitted when there's just one argument, and it's either table constructor or string literal.

That's how you can implement blendin "angry" "y" - blendin() takes first string literal "angry", and returns another function. That second function will take second literal "y", and might finally do something.

 

That looks interesting. Well, the reason why I came up to this is was inspired by this under the point Simple Scripting Language

: http://www.renpy.org/why.html

label family:
    scene bg beach2
    with dissolve

    "It wasn't long before Mary broke the silence, by asking me a question."

    show mary dark smiling
    with dissolve

    m "I told you a little about my family... but I haven't asked you about yours yet. What's your family like?"

    p "When I'm on the island here, I live with my aunt and uncle, but back home, I live with my mother, father, and sister."

    m "A sister? Is she older or younger?"

It is using Phyton, though I have no experience with it. So I tried to figure out if there is any similar approach to this syntax with Lua. However thanks for informing me that this is not the case.

 

 

 


If you really wanted to you could add #define capabilities to Lua by simply running the C preprocessor on your scripts before executing them (it's not hard) but I think it would be a questionable approach for what you're trying to do.



My suggestion: stop trying to come up with a DSL and just use normal Lua constructs. There's quite a bit of syntactic sugar available so you can be creative and still manage to write something nice without resorting to horrible hacks just to e.g. remove parentheses.

 

Oh, but why do you call these "horrible hacks"? What makes them horrible?

Edited by Angelic Ice

Share this post


Link to post
Share on other sites


Oh, but why do you call these "horrible hacks"? What makes them horrible?

 

They are horrible because they invariably break down at some point in the future when you need to add some more functionality and suddenly realize it's impossible, and start coming up with alternatives and end up with something really nasty compared with just using the language as it's meant to be used.

Share this post


Link to post
Share on other sites

 


Oh, but why do you call these "horrible hacks"? What makes them horrible?

 

They are horrible because they invariably break down at some point in the future when you need to add some more functionality and suddenly realize it's impossible, and start coming up with alternatives and end up with something really nasty compared with just using the language as it's meant to be used.

 

 

Thanks for explaining! It makes totally sense to me thus I will avoid this.

 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!