Get player by name
#1
Hello. Given is the Name of the Player as string and I need it as a cPlayer. I tried FindAndDoWithPlayer and ForEachPlayer but it doesn't work at all. I tried it for two hours and the most programmers here solve my problems in a minute so I decided to ask for help...
Reply
Thanks given by:
#2
You can't get a player due to multi-threading reasons. Use callbacks instead:
cRoot:Get():FindAndDoWithPlayer(PlayerName, 
   function(a_Player)
      if (a_Player:GetName() ~= PlayerName) then
         -- Callback is called for the player who matches the target playername the closest.
         return false
      end
      -- Do stuff with the player
   end
)

Or if you already have the world object/know in which world the player is:
World:DoWithPlayer(PlayerName,
   function(a_Player)
      -- Do stuff. No need to check username here
   end
)
Reply
Thanks given by:
#3
Quote:function DetectcPlayerByName(GivenName)
cRoot:Get():FindAndDoWithPlayer(GivenName,
function(a_Player)
if (a_Player:GetName() ~= GivenName) then
-- Callback is called for the player who matches the target playername the closest.
return false
end
-- Do stuff with the player
return(a_Player)
end

I've tried that to convert the string to a cPlayer, but the console says

Quote:LUA: Plugins/Sniper/main.lua~:86: error in function 'TakeDamage'.
argument #2 is 'number'; 'cEntity' expected.

Line 86 is
Quote:Entity:TakeDamage(dtArrow, DetectcPlayerByName(ShooterName), 18, ProjectileEntity:GetPosition())
Reply
Thanks given by:
#4
Try this instead:
Entity:GetWorld():DoWithPlayer(ShooterName,
	function(a_Player)
		Entity:TakeDamage(dtArrow, a_Player, 18, ProjectileEntity:GetPosition()
	end
)
Reply
Thanks given by:
#5
I believe we are very near Blush
function OnProjectileHitEntity(ProjectileEntity, Entity)
 ShooterName = ProjectileEntity:GetCreatorName()
 --Entity:TakeDamage(DamageType, AttackerEntity, RawDamage, KnockbackAmount)
  Entity:GetWorld():DoWithPlayer(ShooterName,
    function(a_Player)
        Entity:TakeDamage(dtArrow, a_Player, 18, ProjectileEntity:GetPosition())
    end
)
end

but

Quote:[18:23:02] LUA: Plugins/Sniper/main.lua~:88: error in function 'TakeDamage'.
argument #2 is 'number'; 'cEntity' expected.

[18:23:02] Stack trace:
[18:23:02] [C](-1): TakeDamage
[18:23:02] Plugins/Sniper/main.lua~(88): (no name)
[18:23:02] [C](-1): DoWithPlayer
[18:23:02] Plugins/Sniper/main.lua~(86): (no name)
[18:23:02] Stack trace end
[18:23:02] Error in <attached> calling function <callback>()
[18:23:02] LUA: Plugins/Sniper/main.lua~:88: error in function 'TakeDamage'.
argument #2 is 'number'; 'cEntity' expected.

[18:23:02] Stack trace:
[18:23:02] [C](-1): TakeDamage
[18:23:02] Plugins/Sniper/main.lua~(88): (no name)
[18:23:02] [C](-1): DoWithPlayer
[18:23:02] Plugins/Sniper/main.lua~(86): (no name)
[18:23:02] Stack trace end
[18:23:02] Error in <attached> calling function <callback>()

What do I wrong?
Reply
Thanks given by:
#6
Strange. I don't see a TakeDamage function that has a cEntity as first parameter. What I did notice though is that you're giving the position of the projectile entity. That should be a double.
Something like this:
Entity:TakeDamage(dtArrow, a_Player, 18, 4)

Also, please use
Code:
[shcode=lua][/shcode]
to post code. It's much more clearer Smile
Reply
Thanks given by:
#7
Okay, thanksBig Grin It works nowBig Grin You are just awsome!!! Thank you!!!Smile
Reply
Thanks given by:
#8
@NiLSPACE: The cEntity:TakeDamage() function is overloaded, there are three overloads, one of them takes just a cEntity as its only parameter; tolua works by iterating over the overloads until the parameters match; if none of the overloads match, the last overload iterated is reported as the "wanted" signature, so that's what we're seeing. I'd like to change this behavior sometime by having (heavily templatized) manual bindings for all functions, somewhat like current cLuaState::GetStackValues(), but with extended error reporting.

The documentation for cEntity:TakeDamage() is somewhat flawed, there's no indication on what the datatypes are, the KnockbackAmount should be a single number (but I have no idea how that translates into knockback direction)
Reply
Thanks given by:
#9
Yeah, I didn't notice the TakeDamage function with only an entity as parameter.

We need something that allows us to tell the types of parameters. With Cuberite classes it's easy, because we just redirect them to the proper API page, but with numbers and stuff it's harder. How about we add an anker tag with a title around them. We'll have to update allot of documentation though.

Something like this (notice the title for the knockback amount):
   
Reply
Thanks given by:
#10
That's not good enough, mouse hover is not a good interface (especially if you have to hover over multiple things). On the other hand, most parameters don't need this kind of info, they are self-explanatory.

How about listing one parameter per line, and add an optional type after, say, a colon?
Code:
DamageType: [eDamageType]
AttackerEntity: [cEntity]
RawDamage
KnockbackAmount: number
(Things in [brackets] are links to respective pages)
For this to work, we could extend the APIDesc format for the "Params" - if it's a simple string, use the current behavior, if it is an array, consider each item a separate parameter and each item should have up to three member strings: name, typename, typelink:
TakeDamage =
{
	{ Params = "{{AttackerEntity|cEntity}}", Return = "", Notes = "Causes this entity to take damage that AttackerEntity would inflict. Includes their weapon and this entity's armor." },
	{
		Params = {
			{"DamageType", "eDamageType", "Globals#eDamageType"},
			{"AttackerEntity", "cEntity"},
			{"RawDamage"},
			{"KnockbackAmount", "number"}, 
		},
		Return = "",
		Notes = "Causes this entity to take damage of the specified type, from the specified attacker (may be nil). The final damage is calculated from RawDamage using the currently equipped armor."
	},
	...
},
We could even do the same for the return values - sometimes there's more of them and of various types.
Reply
Thanks given by:




Users browsing this thread: 3 Guest(s)