george68 Posted January 21, 2016 Share Posted January 21, 2016 Hi, I am trying to find a way of setting up custom properties for a model using the editor. One way is to create script properties but I don't know how I can access them using C++ Any help will be appreciated 1 Quote Link to comment Share on other sites More sharing options...
shadmar Posted January 21, 2016 Share Posted January 21, 2016 These will crosstalk out of the box: http://www.leadwerks.com/werkspace/page/api-reference/_/entity/entitygetkeyvalue-r773 http://www.leadwerks.com/werkspace/page/api-reference/_/entity/entitysetkeyvalue-r774 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Charrua Posted January 21, 2016 Share Posted January 21, 2016 these both are very handy, or if you named the object properly then you can search it inside the world->Entities list see: http://www.leadwerks.com/werkspace/blog/183/entry-1613-lua-cpp-how-to-27/ Quote Paren el mundo!, me quiero bajar. Link to comment Share on other sites More sharing options...
Rick Posted January 21, 2016 Share Posted January 21, 2016 We've asked before for the editor to be able to create key/value pairs that could be added by Josh to the entity via SetKeyValue(). This would allow anywhere to get the values and this would be the best solution I feel. 1 Quote Link to comment Share on other sites More sharing options...
george68 Posted January 22, 2016 Author Share Posted January 22, 2016 I've placed a model into a map and attached to it a script with properties: using the code below I cannot access any key value: if (Leadwerks::Map::Load(getLevelName(iLevelCur))) { for (std::list<Leadwerks::Entity *>::iterator it=world->entities.begin(); it!=world->entities.end(); it++) { Leadwerks::Entity *ent = *it; if (ent) { std::string s = ent->GetKeyValue("name"); if (!ent->GetKeyValue("name").compare("block")) { ent->SetGravityMode(false); s = ent->GetKeyValue("ID"); Leadwerks::System::Print(s); } } } } Quote Link to comment Share on other sites More sharing options...
Charrua Posted January 22, 2016 Share Posted January 22, 2016 where is world created? if in lua, then cpp world should be empty (or NULL) Quote Paren el mundo!, me quiero bajar. Link to comment Share on other sites More sharing options...
george68 Posted January 22, 2016 Author Share Posted January 22, 2016 I created the world using the editor and I loaded the world using the C++. The cpp world is not empty because I found the entity with the name "block". Quote Link to comment Share on other sites More sharing options...
Charrua Posted January 22, 2016 Share Posted January 22, 2016 but if you find the "block" entity, then GetKeyValue("name") is working. "name" is a key already implemented, so it allways work, you have a textbox on the editor for the name. if i'm not wrong, then the problem is with keys created by you, isn't it? probably your problem is on the SetKeyValue command, which i suppose you are setting in the lua script. Quote Paren el mundo!, me quiero bajar. Link to comment Share on other sites More sharing options...
Rick Posted January 22, 2016 Share Posted January 22, 2016 Show us the script where you are assigning these values? If you think just because you have the script property "ID" that it'll get translated to a key/value name ID then you'd be wrong. You have to explicitly assign this in your scripts Start() function like: Script.ID = 0 --int "ID" function Script:Start() self.entity:SetKeyValue("ID", self.ID) end The name is a special field and Josh does set this script property to a key/value. [EDIT] It might be a cool idea to make a generic function that does this translation for people. You should be able to loop over 'self' and get all the fields and make them key/value and add to the entity (just ignore functions and tables when looping over). Then you could do something like the below and it'll be easier to do this for other objects. function Script:Start() MakePropertyKeyValues(self) end 2 Quote Link to comment Share on other sites More sharing options...
Josh Posted January 22, 2016 Share Posted January 22, 2016 They key / value system actually precedes our implementation of Lua and has nothing to do with it. There is an experimental mechanism for setting and retrieving simple script values between C++ and Lua. These functions are in the Entity class: virtual void SetString(const std::string& name, const std::string& s); virtual std::string GetString(const std::string& name); virtual void SetObject(const std::string& name, Object* o); virtual Object* GetObject(const std::string& name); virtual bool GetBool(const std::string& name); virtual void SetFloat(const std::string& name, const float f); virtual float GetFloat(const std::string& name); 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Charrua Posted January 22, 2016 Share Posted January 22, 2016 this is from when? last year i passed via keyvalues data from cpp to lua and back to cpp using an entity as a placeholder. this is not working at present?, i don't tested recently Quote Paren el mundo!, me quiero bajar. Link to comment Share on other sites More sharing options...
Josh Posted January 22, 2016 Share Posted January 22, 2016 Ah, well you can retrieve the keyvalues in Lua as well, with the same command, but I think he wants to set script values in the editor and retrieve them in C++. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
george68 Posted January 22, 2016 Author Share Posted January 22, 2016 which i suppose you are setting in the lua script. No I don't If you think just because you have the script property "ID" that it'll get translated to a key/value name ID then you'd be wrong Yes, that is what I was thinking. Here is the script: Script.ID = 1 --int "ID" Script.Owner = 1 --int "Owner ID" Script.Type = "BlockNM" --string "Type" Script.Dir = 1 --int "Direction" Ah, well you can retrieve the keyvalues in Lua as well, with the same command, but I think he wants to set script values in the editor and retrieve them in C++. Yap, exactly. How I can do it? [EDIT] Thank you all for your time Quote Link to comment Share on other sites More sharing options...
Josh Posted January 22, 2016 Share Posted January 22, 2016 It would just be self.entity:GetKeyValue() (in Lua). However, these values cannot be set in the editor. C++ programming in Leadwerks is much more open-ended than the Lua entity script system. There could probably be some better support for communication between Lua and C++, but a lot of people who use C++ just want to code in that so it hasn't been a high priority. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
george68 Posted January 22, 2016 Author Share Posted January 22, 2016 Oh I see.. I hoped that I could use the editor as my level editor for a game I want to develop it with C++ For example my game has many types of boxes with different properties. Placing these boxes in the editor is a piece of cake, but I'd like to have the ability to give different behaviors using custom properties for each one. Quote Link to comment Share on other sites More sharing options...
Charrua Posted January 22, 2016 Share Posted January 22, 2016 found a script i used ... when testing set/get key value its called EntData.lua --simply to pass data to c++ Script.AnInt=5--int "anInt" Script.AString="Hola"--string "string" function Script:Start() self.entity:SetKeyValue("anInt", self.AnInt) self.entity:SetKeyValue("aString", self.AString) end if you attach this script to an entity in the editor, you may set the values you want and at startup, keys are asigned to the entity i'm sure i used that to then read the keys from cpp side, i only found the lua side other thing i used to use... just because name is safe, was use the "name" as a csv, with something like name=theentty, customval1=34, customval2=hola, and so.. then whit a utiliti function like getNthCsvFromString(string s, int nth) i get the "name" key and get the individual csv items with the previous function... i know that this kind of names are a mess, but was what i used before knowing better ones jio 1 Quote Paren el mundo!, me quiero bajar. Link to comment Share on other sites More sharing options...
george68 Posted January 22, 2016 Author Share Posted January 22, 2016 @Charrua: Thanks, I'll give it a try. Quote Link to comment Share on other sites More sharing options...
george68 Posted January 22, 2016 Author Share Posted January 22, 2016 @Rick & @Charrua: It works! This is what I needed. Quote Link to comment Share on other sites More sharing options...
Charrua Posted January 22, 2016 Share Posted January 22, 2016 i tested the opposite, change a key in cpp and test it on lua side.. i realize how i forgot how to code in lua.. --simply to pass data to c++ Script.AnInt=0 --int "anInt" Script.AString="Hola" --string "string" function Script:Start() self.entity:SetKeyValue("anInt", self.AnInt) self.entity:SetKeyValue("aString", self.AString) end function Script:UpdateWorld() if self.entity:GetKeyValue("anInt") ~= self.AnInt then self.AnInt = self.entity:GetKeyValue("anInt") System:Print("AnInt changed: "..self.AnInt) end end and in cpp, after found "theEnt" (as i named the entity with this script attached) and storend on a Entity* pointer.. i delared a glogal int int anInt=0; in main loop: somewhere: if (window->KeyHit(Key::G)) { anInt++; theEnt->SetKeyValue("anInt", String(anInt)); } Quote Paren el mundo!, me quiero bajar. Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.