Jump to content

Custom properties


george68
 Share

Recommended Posts

I've placed a model into a map and attached to it a script with properties:

 

LE1_zps3dji5qtu.png

 

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);
	  }
 }
}
}

Link to comment
Share on other sites

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.

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

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

  • Upvote 2
Link to comment
Share on other sites

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);

  • Upvote 1

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

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

Link to comment
Share on other sites

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.

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

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.

Link to comment
Share on other sites

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

  • Upvote 1

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

:)

 

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));
}

Paren el mundo!, me quiero bajar.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...