Jump to content

Variables in other scripts?


Yue
 Share

Go to solution Solved by Rick,

Recommended Posts

  • Solution

I assume you mean entity scripts (scripts attached to entities). There would be a couple ways to do it.

  1. You could link the entity via a script properties. In the script you want to access the other script make a Script variable like say Script.otherEntity = nil --entity. In the properties you'll have a section for this that you can drag and drop another entity in your scene onto. Now you can access it's scripts variables via self.otherEntity.script.theVariableIWant.
  2. You could loop over all entities in the world and find the one(s) you want and then access it via entity.script.theVariableIWant.

So the idea is once you get a hold of the entity itself it has a .script property which gives you access to it's script variables and functions and there are a couple ways to access an entity from another entity. If you have just a few design time entities #1 works well. If you have a lot or run-time entities then method 2 works well I think.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Assuming you have the entity the variable is attached to, it's just entity.script.varname.

In Turbo, script variables are attached directly to the entity so it's just entity.health, entity.ammo, etc.

  • 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

8 minutes ago, havenphillip said:

How do you "loop over all entities in the world'? Talking about an AABB box?

 

It's been awhile so I might mess this up, but CurrentWorld().entities I believe would be the list to loop over. Every entity loaded in the current world would be there. Even dynamic ones. It could be expensive so ideally do it on startup.

  • Like 1
  • Upvote 1
Link to comment
Share on other sites

9 minutes ago, Josh said:

Assuming you have the entity the variable is attached to, it's just entity.script.varname.

In Turbo, script variables are attached directly to the entity so it's just entity.health, entity.ammo, etc.

In Turbo, will it be possible to add multiple scripts to one entity?

  • Upvote 1

Mars.jpg.89ab63a64eebc1f5ada0ab82b66a1f8c.jpg

 

 

Link to comment
Share on other sites

2 minutes ago, Yue said:

In Turbo, will it be possible to add multiple scripts to one entity?

I think that would be tough to do given the functions/variables are right on the entity itself. If 2 scripts share names by chance you wouldn't be able to differentiate them.

  • Like 1
Link to comment
Share on other sites

2 minutes ago, Yue said:

In Turbo, will it be possible to add multiple scripts to one entity?

No, that would be bad. With one script, we can just say "entity.health", entity:TakeDamage(5), etc. I want it to get simpler, not more complicated.

  • Like 2

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

The methods I have been using are; Importing the second script, Making variables global, and using a pick to get the value. The method varies depending on how you want the code to work. If you want the script to attach to multiple entities but the functions to be per entity I find the best way is using a pick. And if I want to add multiple scripts the best method I have seen is using pivots and making them children of the entity.

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