Space Engineers

Space Engineers

Not enough ratings
Script Tutorial Turn OnOff Lights
   
Award
Favorite
Favorited
Unfavorite
File Size
Posted
359.048 KB
Jan 1, 2015 @ 9:52am
1 Change Note ( view )

Subscribe to download
Script Tutorial Turn OnOff Lights

In 1 collection by Nekochan
Space Engineers Tutorial Scripts
3 items
Description
This is a simple script that will retrieve all connect lights and toggle them on or off. For those not familier with C#, "//" means it is a comment and has nothing to do with the code itself.

Authors Note:
The action retrieved inside the for loop can be retrieved outside the loop also, however if you do this you need to make sure that all blocks have the action and that you are null safe with retrieving the first action. It is possible to have the initial block list with a size of zero, and if using the search by name to retrieve several blocks of different types. I.e retrieve by name "FOO" which returns a beacon and a thruster. Try to increase the radius, and the beacon will work, and the thruster will fail.
10 Comments
p3st|cIdE Jan 1, 2015 @ 1:46pm 
They should embrace LINQ, but they'll need to fix foreach first. Setters are not evil. I find that the Mod API gives a rough indication of the skill and/or diligence of the programmers at Keen. Put in an application, maybe you can help them! I'm busy trying to piece together an actual code version of the interfaces so that Real programmers can understand the dox.
Nekochan  [author] Jan 1, 2015 @ 1:29pm 
I know of two schools of thought when it comes to dealing with this kind of logic, the ons is as you see now, you retrieve an interface for the method execution and us it. The other is indeed to put in the interface for the block the method signitures it can use. I can't explain their decision to go with the former, it does seem a bit wonky. It might have something to do with the inability to use static variables, expressions, or class declarations. This is at a guess and I would ask one of the devs for more information. Personally I would prefer to type in IMyBeacon.IncreaseRadius() or even a IMyBeacon.SetRadius. I have found several limitations currently when experimenting with this, such as that infinite loop judger, increase isn't the same amount as the radius while the code is executing and such. I can just hope that they continue to work on it and refine it to a better product like they normally do.
!132 Jan 1, 2015 @ 1:23pm 
Nekochan, i don't tried to say that i am "special", i write this just to...
i'm confused that devs created so strange way to call actions and i'm trying to find out why not to call concrete methods selected for doing some stuff like OnOff. Do you know reason?

(sorry for my bad english, it is not my native language)
Nekochan  [author] Jan 1, 2015 @ 1:14pm 
!132 I tried making a tutorial for that that wouldn't overcomplicate it for non-coders, and I honestly couldn't think of one that I wouldn't have extra code in it that might confuse someone. Accessing in the for statement ensures the object exists, but I do understand if you were executing a lot it would probally eat up cpu doing it that way. I will post a note in the description for it, but I'd rather keep the tutorial simple for people who can't code.
!132 Jan 1, 2015 @ 1:08pm 
I found that actions with name "OnOff" (type ITerminalAction) for each object are same (at least hashcodes), so there is a way to create that action once and apply it to all the lights (it works).
p3st|cIdE Jan 1, 2015 @ 12:57pm 
Type inference can be helpful if used judiciously. Personally I don't avoid language features due to others' inability to understand the idioms. But I'm not trying to convince you or change your coding style; do as you will.
Nekochan  [author] Jan 1, 2015 @ 12:53pm 
Yeah I just took what you had and made it a tutorial, my avoidence of var is a coding preference, I've never liked it and probally never will. I have some OCD in regards to type declaration.
p3st|cIdE Jan 1, 2015 @ 11:51am 
Thanks for tutorializing this. regarding "honestly a better coding practice than putting it in a for statement" you're right. Regarding avoidance of var: well, you force the compiler to optimize away the two separate array indexing operations. It can probably do that. My advice to you: Find a shorter horse. I've fought more holy wars than you and survived.
Nekochan  [author] Jan 1, 2015 @ 11:25am 
Glad I could help, I uploaded another one that includes a bit more advanced stuff using the search by name method
ilbinek Jan 1, 2015 @ 10:01am 
Easy to learn. Nice work! Good JOB! For starters like me is this tutorial first step, do more please :D