Jump to content

Can't make entity:GetKeyValue("name") working


Lunarovich
 Share

Recommended Posts

Hello, this one is "hurting" me :) Here is an error message that referes to the System:Print() call:

 

error in function 'Print'.; argument #3 is 'string'; '[no object]' expected.

 

function Script:Collision(entity,position,normal,speed)
System:Print(entity:GetKeyValue("name"))
if speed>20 and entity:GetKeyValue("name") ~= "Death Trigger" then
 self:Hurt(100)
end
end

 

Also, the condition in the third line does not work, although I have an object that is called "Death Trigger" and the script gets activated when I hit it...

 

BTW, don't know what is a third argument of the Print. Also, what does the "'[no object]' expected" means?

Link to comment
Share on other sites

Thanks! When I try it, the System:Print outputs a correct value, ie. Death Trigger, to the console, but the code still doesn't work.

 

Anyway, why do I have to use tostring function. Isn't Entity:GetKeyValue supposed to give me the value that I'm looking for, ie. of type string?

 

EDIT: I've just checked with the type(entity:GetKeyValue("name")) and it does give me a string... This one puzzles me...

 

EDIT: OK, this one, for some reason, works:

 

local name = entity:GetKeyValue("name")
System:Print(name)

 

However, this one still does not work

if speed>20 and name ~= "Death Trigger" then
self:Hurt(100)
end

even though the name gets the correct value, ie. "Death Trigger" outside the if condition. Inside the if condition, name seems to be an empty string... How strange... At least for me smile.png Am I missing something about Lua lexical scoping???

 

EDIT: It seems that I am trying to call a GetKeyValue on a non-existing entity. Not yet sure, though.

Link to comment
Share on other sites

When the player first hits the "Death trigger" (which is Collision.Trigger), the Entity:GetKeyValue("name") outputs a correct value, ie. "Death Trigger, however, player's speed is 0 for some reason in that and a subsequent tick... Then player gets teleported (via SetPosition) to the respawn point. In the meantime, its speed is about 25 as it should have been when he hits the trigger... Since now the player collides with a normal ground and not a trigger, he dies. Why would the speed be 0 when he hits a trigger?

 

And how comes that an Entity:GetKeyValue("name") is an empty string in all subsequent ticks when a Script:Collision gets called? I mean, aren't we supposed to always collide with an entity and doesn't all entities have names?

 

EDIT: It seems to me that I have a general problem with the Entity:GetKeyValue("name"). I've pinned the problem down to the fact that entity:GetKeyValue("name") sometimes returns an empty string inside the Script:Collision, which I don't understand, as I've explained in the previous paragraph.

Link to comment
Share on other sites

When you are calling entity:GetKeyValue("name") and getting blank strings, it might mean you are colliding with entities with no unique names (possibly CSG or the terrain or water or something).

 

What you may have more luck doing is to flip the situation around. Instead of constant detection on the player, attach this code to the trigger. Right now you are essentially analyzing collisions every loop, and it introduces errors and unexpected behavior with minor collisions, unimportant collisions. If you perform the GetKeyValue check within the trigger code, then you will likely have better results.

 

Also, what is a tick? Do you have the console open while you are running the game? There is always latency with producing output due to the way your operating system works, and there may be some buffering going on to delay it even more. It's very likely that the data is a little old, since it should be a constant flow of data.

 

But, as Shadmar said, it would be helpful if you could provide a map with code (even if it's just a small map just for this purpose).

Link to comment
Share on other sites

Thank you for the answer. However, the fact that I can get more or less better results does not confort me. I expect it to be either a collision with a named entity or no collision at all. I know that I can check if entity ~= nil, but is there a more elegant way that always gives me intended results?

Link to comment
Share on other sites

I expect it to be either a collision with a named entity or no collision at all.

Except thats not how it works. Just the simple character controller's collision type will cause a collision response with any scene collision type (like terrain), just like nick is referring to. There will be items in the scene that will not have a name key that will cause a collision response based on the collision type assigned. The only suggestion would be to first perform a check to see if the entity has a name then perform the rest of your code.

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

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