wiki.sine.space | sinespace

Customize Components

From wiki.sine.space
Revision as of 09:54, 12 February 2019 by Eric MA (Talk | contribs) (Created page with "If you want to create your own special game in Sinespace, you may find that it’s very important to know how to customize the components -- instead of throwing all things to...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

If you want to create your own special game in Sinespace, you may find that it’s very important to know how to customize the components -- instead of throwing all things to lua script, using the components we offered in the Editor pack with a clear logic should be the best way for creation.

Basically, we could let you do the same thing as you can do in Unity vanilla(which means the pure original version of Unity, doesn’t include 3rd party plug-ins), but we still haven’t package all the things into our SDK. So, if you find something can easily done in Unity, but feel hard to realize it in Sinespace, don’t hesitate to contact the staff.

Here I will show some easy examples for you to create different types of game.

A collider-based checkpoint in racing game

Note: This is one component like I said above. It uses a component called Trigger Activator -- originally it only functions with Avatars, when you drive a car in Sinespace and pass a collider with Trigger Activator, it won’t be activated. In future version(test/release build from 2019 Feb), this Trigger Activator will also affected by vehicles, so you can use this component and a isTrigger Box collider to simply create a checkpoint/start-finish line(but of course you may still need lua script if you want to put a counter or some other function which is not directly supported by unity in it).

TriggerActivator.png

For example, in this scene, we have a thin box collider between two cylinder here, works as a checkpoint. When the vehicle pass it, it will play the particle effect(no loop), raise the counter by 1, and refresh a text of the counter indicator on the UI layer.

Because of unknown reason, it is suggested that not have any other collider contact with this box collider, otherwise the event will be triggered once when the region is loaded. Or you can check the Exclude Owner option to avoid this happen(but it will also cause your avatar won’t be able to trigger the event anymore, which may be annoying when doing the tests).

Using the component “Scripting Runtime” to add lua script to game objects. Don’t worry if you are not familiar with writing codes -- to create a game, the logic is more important than the technique of programming. It is more important that before doing the work, make yourself be clear about what function you need, and a simple way to realize it.

In this example, we can use a simple flow-process diagram to show what we want to do:

CheckpointFlowProcess.png

So from this diagram, you may find out what you need to prepare for realizing these.

Let's do them one by one.


First, create a Text object, and change its name to something unique, i.e. here it's "checkpointindicator". Add Scripting Runtime to it, and add the code below:

Space.Shared.SetGlobal("racingComponent", "ctr", 0);
counter = Space.Shared.GetGlobal("racingComponent", "ctr");
local obj = Space.Host.ExecutingObject;
obj.UIText.text = "Counter:" .. counter;

function RefreshText()
    counter = Space.Shared.GetGlobal("racingComponent", "ctr");
    obj.UIText.text = "Counter:" .. counter;
end

It declare a global variable and initialize it with 0 using SetGlobal, which can be used in other script by using GetGlobal. Then find the Text component attached to itself.

The function RefreshText is the function refresh the text when the counter's value changes. It will only be effected when called.

For example, when can call it when the vehicle exit the collider of checkpoint:

TriggerActivator2.png

The code for the checkpoint is like below:

local obj = Space.Host.ExecutingObject;
local particle = obj.ParticleSystem;
local txt = Space.Scene.Find("Notify");

function AddCounter()
  counter = Space.Shared.GetGlobal("racingComponent", "ctr");
  if(counter == 0)
  then
    counter = counter + 1;
    Space.Shared.SetGlobal("racingComponent", "ctr", counter);
    particle.Play();
    txt.UIText.text = "";
    Space.Log("counter:" .. counter);
  else
    txt.UIText.text = "Checkpoint Missed";
  end
end

Notify is another Text gameobject for showing the message of missing checkpoint. To find other object in the scene, use function Space.Scene.Find(object name) is one method, that's why set a unique name to game object is needed.

When a checkpoint is ready, you can duplicate them, and don't forget to modify the line "if(counter == 0)", where the 0 should be n-1 where the n is the order of the checkpoint.

And for finish line:

local obj = Space.Host.ExecutingObject;
local particle = obj.ParticleSystem;
local txt = Space.Scene.Find("Notify");
local txt2 = Space.Scene.Find("CheckpointIndicator");

function Reset()
  counter = Space.Shared.GetGlobal("racingComponent", "ctr");
  if(counter == 3)
  then
    particle.Play();
    txt.UIText.text = "";
    txt2.UIText.text = "Finished!";
    counter = 0;
    Space.Shared.SetGlobal("racingComponent", "ctr", counter);
  else
    txt.UIText.text = "Checkpoint Missed";
  end
end

I put 3 checkpoints in total, so here the check condition is counter == 3. You can also add a lap counter in this finish line so it can use for multiple laps race. The function Reset will be called when the vehicle reach the finish line.