https://linen.dev logo
連結相手をTrainNameで判断し、オブジェクトを切り替えるscriptについて
# forum
i
pipoさんから教えて頂いた関数で挑戦していますが、条件と違う相手と連結した際に意図しないobjが表示されるのですが対処法は何かございますでしょうか... (奥側の車両は条件を適用していない車両なのですが、手前側の車両の貫通扉が開いてしまう現状です)
e
First, you need to get the entity of the connected train, maybe by getFormation or getConnectedTrain, etc. (See "script-function" and "renderscript-function") Second, I think there's a method that can get you the train name. You can search the two channels mentioned above for that method. Then,
Copy code
js
(CONDITION-SATISFIED)? Kantsu.render(renderer): NotKantsu.render(renderer);

or

if (CONDITION-SATISFIED)
{
  Kantsu.render(renderer);
}
else
{
  NotKantsu.render(renderer);
}
Wish that helps you 🙂
p
まず前方と後方の車輌のentityですが、
Copy code
javascript
var frontTrain = entity.getConnectedTrain(0);
var backTrain = entity.getConnectedTrain(1);
で取得ができます。次に、rendererではなくてentityTrainBaseからモデル名を取得する方法ですが、modelSetとそのconfigを取得することでjsonで設定したプロパティにアクセスできます。こちらは1.7.10と1.12.2でやり方が異なります。
Copy code
javascript
if(RTMCore.VERSION.indexOf("1.7.10") != -1){//1.7.10
  var frontTrainName = frontTrain.getModelSet().getConfig().trainName;
}else{//1.12.2
  var frontTrainName = frontTrain.getResourceState().getResourceSet().getConfig().trainName;
}
(backTrainでも同様) 後は各取得段階でエラーが発生しないように、適時nullチェックを行う必要があります。
これで前後の車輌の名前が取得できるので、名前同士を参照すれば条件を同種編成の連結に絞れるかと。
Copy code
javascript
if(frontTrainName == renderer.getModelName() && backTrainName == renderer.getModelName()){
}
↑こちらは前後共にmodelNameが完全に同じでないといけません。
Copy code
javascript
if(frontTrainName.indexOf("811") != -1 && backTrainName.indexOf("811") != -1){
}
↑こちらは前後共にmodelNameが"811"を含まなければいけません。
恐らく片運転台で、前後共に同じ編成が来た時しか貫通扉展開がないと思われるので以上のスクリプトで十分かと思われますが、場合によっては先頭車+中間車の貫通扉展開であったり、両運転台であったり、異種併結などがありえます。これらの場合条件の絞りが更に困難になるので、結局はcustomButtonでの描画切り替えをすすめます。
Copy code
javascript
if(entity.getResourceState().getDataMap().getInt("Button0")) == 1){//任意のボタン(0:貫通扉オフ、1:貫通扉オン)
  Kantsu.render(renderer);
}else{
  NotKantsu.render(renderer);
}
i
ありがとうございます
nullチェックは if(entity != null){} であってますか?
e
Yes Usually I simply write
Copy code
js
if (entity)
{
    codes;
}
i
いまこの状態なんですけどこれであってますかね...?
お騒がせしました...↓で動作しました皆様ありがとうございます
ちなみにカスタムボタンだとこの構文だけでいい感じですか?
e
To add a custom button, first you should add the following lines (properties) into the JSON file of the train:
Copy code
json
"customButtons":
[
  ["Value1", "Value2", ..., "ValueN"],
  ["Value1", "Value2", ..., "ValueN"],
  ...,
  ["Value1", "Value2", ..., "ValueN"],
]
```Then, to call the value of buttons, call
```js
var customButtonValue = dataMap.getInt("button0");
(I assume you know what's data map and how to use it.) Notice: 1. If you have multiple buttons, try require the value of "button1", "button2", ... . 2. Instead of providng the tag ("ValueN") of the button, what you'll get is an integar. In the following case, if "Value2" is chosen, the value will be 1.
i
Ok. Thank you. 🙂
By the way, can I use getModelName in version 1.12.2?
e
I think the
getModelName
method itself is unavailable in 1.12.2, but you can still use the method you used early to get what you want
2 Views