Jump to content

Get the valie from LUA option


AggrorJorn
 Share

Recommended Posts

You don't access the menu value directly. What you do is in the SetKey() function (you can look at other entities to see how the SetKey() works) for the model you added this property to, you would use http://www.leadwerks.com/wiki/index.php?title=Entities#SetEntityKey to assign this model instance a key/value. In your case you would do something like:

 

model:SetKey("hand_icon", "true")

 

 

Then in your fps lua file where you pick, you are picking the model and when you get the model you can do:

 

if(model:GetKey("hand_icon")) then

-- change icon to the hand icon

end

 

 

 

 

So the ideas is you create menu options. When the menu options are set the SetKey() function is called which allows you to assign the value to your entities OR models that you can later get at other places.

Link to comment
Share on other sites

hmm Although I'm more clear about what the SetKey does, it's not entirely clear where to put them exactly. This is the menu option.

group:AddProperty( "showicon",PROPERTY_BOOL,"","Show hand icon")

I've added this to the spawn function :

function Spawn(model)
model:SetKey("showicon","1")

This way when a new switch is being dragged on, it has a default value of 1 (true).

 

Next there is the function SetKey(model,key,value). I've place a new model.setkey command but here I get an error.

function SetKey(model,key,value)
entitymodel:SetKey("showicon","true")

The error message "attempt to index global 'entitymodel' (a nill value)". What does this error mean exactly?

Link to comment
Share on other sites

hmm Although I'm more clear about what the SetKey does, it's not entirely clear where to put them exactly. This is the menu option.

group:AddProperty( "showicon",PROPERTY_BOOL,"","Show hand icon")

I've added this to the spawn function :

function Spawn(model)
model:SetKey("showicon","1")

This way when a new switch is being dragged on, it has a default value of 1 (true).

 

Next there is the function SetKey(model,key,value). I've place a new model.setkey command but here I get an error.

function SetKey(model,key,value)
entitymodel:SetKey("showicon","true")

The error message "attempt to index global 'entitymodel' (a nill value)". What does this error mean exactly?

 

 

function SetKey(model,key,value)
entitymodel:SetKey("showicon","true")

 

What is entitymodel? The code you posted doesn't show what entitymodel is? If you look at other script examples there is a way to get the lua entity from the model that is being passed into SetKey(). There is something like get_Base() or something that you pass your model into and it returns a lua entity in which you can call SetKey() from. The other route to go is to just use the C++/BMax functions for SetEntityKey() passing in the model that is passed to SetKey(). I'm pretty sure the same result will happen.

Link to comment
Share on other sites

I have to be close then:

switch.lua

	group=grid:AddGroup( 'Icon' )
group:AddProperty( 'showicon', PROPERTY_BOOL,'', 'Show hand icon' )
group:Expand(1)
end

function SetKey(model,key,value)
if key=='showicon' then
	SetEntityKey(model,"switchIcon","true")
end
end

the fpscontroller:

-- show hand icon
if (GetEntityKey(model,"switchIcon","true")) then
handIcon = true
end

The engine crashes entirely when I approach the switch.

Link to comment
Share on other sites

I would expect that to work. Are you calling this GetEntityKey in fpscontroller inside the main loop or before it? If it's before it might be an issue as things might not be loaded.

 

Also your true is a string so in your if statement you will need to check for == "true"

Link to comment
Share on other sites

I though that 'model' was some kind of global class.

 

--check hand icon

	pick=CameraPick(camera,Vec3(GraphicsWidth()/2,GraphicsHeight()/2,2.0),0,0)
	if pick~=nil then
		repeat
			if pick.entity:GetClass()==ENTITY_MODEL then
				break
			end
			pick.entity=pick.entity.parent
		until pick.entity==nil

		-- show hand icon
		if (GetEntityKey(model,"switchIcon","true"))== true then
			handIcon = true
		end

	else
	handIcon = false	

	end

 

 

In a previous topic I wondered if I could change "GetClass()==ENTITY_MODEL " could change into my own kind of class. But this ways seemed the better solution to this.

Link to comment
Share on other sites

using

if (GetEntityKey(pick.entity,"switchIcon","true")) then

results in no errors, but the icon appears on every single model.

 

If I try

if (GetEntityKey(pick.entity,"switchIcon","true"))== true then

then the hand icon doesn't appear. So it looks like the command is right now, but it doesn't refer to the right key. Also this code seems quite fragile as the editor crashes with the smallest of change.

Link to comment
Share on other sites

Try

 

if (GetEntityKey(pick.entity,"switchIcon","true")== "true") then

 

 

true and "true" are not the same thing. "true" is a string and true is a boolean. You are setting the entity key to the string "true" and that if statement you have is trying to compare the key value of either "true" or "false" to true, which isn't the same. It might be better to pick some other value to avoid the confusion for you. Like "yes" or "no" maybe.

 

The good thing is we know that pick.entity is the right value because of your first if statement setting the icon with all models. The reason is because that first if statement is always returning true for some reason.

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...