From 74d6b594a6fa5c5062426092001042f59672df61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Br=C3=BCckner?= Date: Mon, 19 Oct 2020 18:21:56 +0200 Subject: [PATCH] Two new logic nodes for the "Random" category --- Sources/armory/logicnode/RandomChoiceNode.hx | 14 ++++++++++ Sources/armory/logicnode/RandomOutputNode.hx | 12 ++++++++ .../arm/logicnode/random/LN_random_choice.py | 17 +++++++++++ .../arm/logicnode/random/LN_random_output.py | 28 +++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 Sources/armory/logicnode/RandomChoiceNode.hx create mode 100644 Sources/armory/logicnode/RandomOutputNode.hx create mode 100644 blender/arm/logicnode/random/LN_random_choice.py create mode 100644 blender/arm/logicnode/random/LN_random_output.py diff --git a/Sources/armory/logicnode/RandomChoiceNode.hx b/Sources/armory/logicnode/RandomChoiceNode.hx new file mode 100644 index 00000000..dc07ac29 --- /dev/null +++ b/Sources/armory/logicnode/RandomChoiceNode.hx @@ -0,0 +1,14 @@ +package armory.logicnode; + +class RandomChoiceNode extends LogicNode { + + public function new(tree: LogicTree) { + super(tree); + } + + override function get(from: Int): Dynamic { + var array: Array = inputs[0].get(); + + return array[Std.random(array.length)]; + } +} diff --git a/Sources/armory/logicnode/RandomOutputNode.hx b/Sources/armory/logicnode/RandomOutputNode.hx new file mode 100644 index 00000000..cfa7c344 --- /dev/null +++ b/Sources/armory/logicnode/RandomOutputNode.hx @@ -0,0 +1,12 @@ +package armory.logicnode; + +class RandomOutputNode extends LogicNode { + + public function new(tree: LogicTree) { + super(tree); + } + + override function run(from: Int) { + runOutput(Std.random(outputs.length)); + } +} diff --git a/blender/arm/logicnode/random/LN_random_choice.py b/blender/arm/logicnode/random/LN_random_choice.py new file mode 100644 index 00000000..bf529faf --- /dev/null +++ b/blender/arm/logicnode/random/LN_random_choice.py @@ -0,0 +1,17 @@ +from arm.logicnode.arm_nodes import * + + +class RandomChoiceNode(ArmLogicTreeNode): + """Choose a random value from a given array.""" + bl_idname = 'LNRandomChoiceNode' + bl_label = 'Random Choice' + arm_version = 1 + + def init(self, context): + super().init(context) + + self.add_input('ArmNodeSocketArray', 'Array') + self.add_output('NodeSocketShader', 'Value') + + +add_node(RandomChoiceNode, category=PKG_AS_CATEGORY) diff --git a/blender/arm/logicnode/random/LN_random_output.py b/blender/arm/logicnode/random/LN_random_output.py new file mode 100644 index 00000000..66ba6515 --- /dev/null +++ b/blender/arm/logicnode/random/LN_random_output.py @@ -0,0 +1,28 @@ +from arm.logicnode.arm_nodes import * + + +class RandomOutputNode(ArmLogicTreeNode): + """Activate a random output when the input is activated.""" + bl_idname = 'LNRandomOutputNode' + bl_label = 'Random Output' + arm_version = 1 + + def __init__(self): + array_nodes[str(id(self))] = self + + def init(self, context): + super().init(context) + + self.add_input('ArmNodeSocketAction', 'In') + + def draw_buttons(self, context, layout): + row = layout.row(align=True) + + op = row.operator('arm.node_add_output', text='New', icon='PLUS', emboss=True) + op.node_index = str(id(self)) + op.socket_type = 'ArmNodeSocketAction' + op2 = row.operator('arm.node_remove_output', text='', icon='X', emboss=True) + op2.node_index = str(id(self)) + + +add_node(RandomOutputNode, category=PKG_AS_CATEGORY, section='logic')