Jump to content
Azazil00

Referencing the Player in scripts.

Recommended Posts

I've noticed that when making scripts, there isn't an easy way to reference the player all the time. Like for example, right now I am trying to make a staff that throws an entity. The entity should have a rotation that is determined by which way the player is facing. This is hard to do without referencing the player position because I have to write the script in the entity template for the object so that it will rotate when I execute the throw command. Anyway, is there any way that I'm overlooking on how to get player position and rotation from any script?

 

Edit: I did figure what I was wanting to do eventually, and let me tell you, it was so much more simple than my brain was telling me. All I had to do was rotate the object before I set it to the Entity Template. Then it will be thrown in that rotation... yay me.. 

Edited by Azazil00

Share this post


Link to post
Share on other sites

So to create a better reply to this topic and clear some things up to maybe educate a few folks, I want to clarify how I actually reference players in my scripts. But firstly, I want to clarify what I initially made this post about so as to not cause any confusion. 

I made this post because I was trying to throw the "Iron Spike" entity in a specific orientation that made it look like the spike is flying toward the direction being thrown. I thought I had to manually reference the player and set the rotation and all that after the entity was thrown, but it turns out you can just set the rotation of the entity before you reference it in your entity template. This will set its default orientation.

 

Now anyway, back to the topic of the discussion. How do I reference the player in scripts? Well ultimately, I think you have to start with an event that has either the "Player" or "Trigger Entity" already referenced. So I'm still not sure if there is a way to reference the player in a script that has no default connection to the player.

What I like to do is make magic staves and other weapons that give their wielder abilities depending on what attack they use. To begin, I always start my scripts in the staff that I am referencing. This way you gain access to the "On Melee Attack" event, which has three references: Trigger Entity, Attack Type, and Combo Sequence. I never use Combo Sequence because, quite frankly, I don't know how, but I always use the first two.1705877825_Ylands_201020_002518(2).thumb.png.f55007cbe74928f006a5fdb150911372.png

Trigger Entity is just the Entity that Triggered the attack, AKA the player who attacked with the staff. Attack Type is exactly that. There are three attack types, Light, Heavy, and Jump attacks. 

So after I grab the "On Melee Attack" event, I will then make three "IF" statements to tell what attack I am doing. Here, you can get a reference of the Attack Type and ask if it is = to either a light, heavy, or jump attack to do a task.

 921679566_Ylands_201020_002527(2).png.ddafe9e8d9a1d8caa1622df61513c12f.png

After I set up my "IF" statements, I then fill them with instructions I've made. (Or I do now at least. My old methods of making staves did not involve Instructions and were much more messy.) Instructions are just a block of code that you can call later and anything inside is executed. It's pretty much a time savor and organization tool, and a very useful one at that. The Instructions have a name of your choosing, and you can also add in references of your choosing too, but these do not automatically reference anything you like. For example, if you create a reference named "Player" it does not automatically reference the player. When calling the Instruction later, you have to assign a value to this reference, meaning you already have to have reference to the player to assign the player to the Reference "Player". All a Reference is is a reference to a Variable of some kind, whether that be your player character, some other Entity, or Item, Game Logic, Number, whatever it may be. 

 

 

Here, you can see my instructions have a single reference beside their names.191771560_Ylands_201020_002924(2).thumb.png.ae6dc7fb8ff4627226d8de9d5685af6b.png This reference is called "Player" but it actually has no value, but I assign the "Trigger Entity" to it later. Inside the Instructions are just simply a few particle effects, sound effects, a raycast command, and a variable to hold the direction of the raycast. Anytime you want to reference the player in this script, just drag the "Player" reference to the spot you want. Don't worry about the names of the instructions, you can call them whatever you like, I called mine here "Gust" and "Wind Blast" because I am making a "Wind" based magic staff. Also, usually, I will use a throw command instead of a raycast, but they are set up and function almost entirely the same way.

 

 

 

 

 

 

 

 

 

 

 

1008266042_Ylands_201020_000722(2).png.ae4a156ccc974b5d54f41b5e01977ced.png

When you call your instructions in the "IF" statements, you just drag the "Trigger Entity" into the open spot next to "Player" and viola!

<--- This is what your "On Melee Attack" event should look like at the end.

 

  • Thanks 2
  • Upvote 1

Share this post


Link to post
Share on other sites

UPDATE! This is probably the answer to many people's scripts and questions!

So I've recently learned how I think you can reference the player in any script, even those without events that reference the player. All you have to do is make a "Global Storage" game logic, edit its script, then create a variable called "Player" inside it. After that, go into a "Player Spawn Point" script, grab the "On Player Created" event, and set the variable "Player" to the Player that the spawn point creates. The global storage gives you global variables, which means they can be used in any script from what I can tell. This means you can reference that "Player" variable after in any script and it will actually be referencing your player!

 I hope this helps, I know it definitely helps me!

Edited by Azazil00
  • Thanks 1

Share this post


Link to post
Share on other sites

Also,you can make instructions inside the gloval storage and call them from anywhere like global variables.

  • Like 1
  • Upvote 1

Share this post


Link to post
Share on other sites

Hi, I did not read whole thread but I have answer to the original question.

If you have game which is set Single Player only then there is a special script tile Get Local Player in Multiplayer category (since 1.5 there will be probably separate Single player category)

singleplayer.PNG

In multiplayer game there is no The player. You can have there any number of players. However, in some cases the "Get players" script tile may be useful.

  • Thanks 1
  • Upvote 1

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×