Browse Source

Add setget method and tool mode

William Tumeo 8 years ago
parent
commit
c6a09b1a7e

+ 36 - 29
2d/split_screen_platformer/split_stage.gd

@@ -1,17 +1,18 @@
+tool
 extends Control
 
 #export split screen modes
-export(int, "None", "Horizontal", "Vertical") var split = 1
-var split_mode = -1
+#if used with setget, the mode can be made chageable by inspector/animation key
+export(int, "None", "Horizontal", "Vertical") var split_mode = 1 setget set_split_mode
+
+#other split_mode variable to check previous mode on split_mode set method
+var _split_mode = -1
 
 #split modes constants
 const SPLIT_NONE = 0
 const SPLIT_HORIZONTAL = 1
 const SPLIT_VERTICAL = 2
 
-#top and bottom controls
-onready var top = get_node("top")
-onready var bottom = get_node("bottom")
 
 func _ready():
 	#make bottom viewport have the same world2D as the top viewport, so both show the same
@@ -26,36 +27,42 @@ func _ready():
 	#simple and alternatively, copy them to the other viewport, but they must be erased when level is unloaded
 	#get_node("bottom/viewport").add_child( get_node("top/viewport/stage/parallax_bg").duplicate() )
 	
-	#set split screen mode for the first time
-	set_split_mode(split)
-	#if used inside _process/_fixed_process, the mode can be made chageable by inspector/animation key
+	#setting split mode for the first time if node is ready
+	set_split_mode(split_mode)
 
 
 func set_split_mode(mode):
-	#change mode only if "split_mode" is different
-	if mode != split_mode and mode in [0, 1, 2]:
+	split_mode = mode
+	if get_viewport() != null:
+		#apply mode
+		_set_split_mode()
+
+
+func _set_split_mode():
+	#change mode only if "_split_mode" is different
+	if _split_mode != split_mode:
 		#get game screen size
-		var screen = get_viewport().get_rect().size
+		var screen = get_size()
 		
 		#changing sizes and positions
-		if mode == SPLIT_HORIZONTAL:
-			top.set_size(Vector2(screen.x, screen.y/2))
-			top.set_pos(Vector2(0, 0))
-			bottom.set_size(Vector2(screen.x, screen.y/2))
-			bottom.set_pos(Vector2(0, screen.y/2))
+		if split_mode == SPLIT_HORIZONTAL:
+			get_node("top").set_size(Vector2(screen.x, screen.y/2))
+			get_node("top").set_pos(Vector2(0, 0))
+			get_node("bottom").set_size(Vector2(screen.x, screen.y/2))
+			get_node("bottom").set_pos(Vector2(0, screen.y/2))
 		
-		elif mode == SPLIT_VERTICAL:
-			top.set_size(Vector2(screen.x/2, screen.y))
-			top.set_pos(Vector2(0, 0))
-			bottom.set_size(Vector2(screen.x/2, screen.y))
-			bottom.set_pos(Vector2(screen.x/2, 0))
+		elif split_mode == SPLIT_VERTICAL:
+			get_node("top").set_size(Vector2(screen.x/2, screen.y))
+			get_node("top").set_pos(Vector2(0, 0))
+			get_node("bottom").set_size(Vector2(screen.x/2, screen.y))
+			get_node("bottom").set_pos(Vector2(screen.x/2, 0))
 		
-		elif mode == SPLIT_NONE:
-			top.set_size(Vector2(screen.x, screen.y))
-			top.set_pos(Vector2(0, 0))
-			bottom.set_hidden(true)
+		elif split_mode == SPLIT_NONE:
+			get_node("top").set_size(Vector2(screen.x, screen.y))
+			get_node("top").set_pos(Vector2(0, 0))
+			get_node("bottom").set_hidden(true)
 		
-		if split_mode == SPLIT_NONE:
-			bottom.set_hidden(false)
-		#apply mode
-		split_mode = mode
+		if _split_mode == SPLIT_NONE:
+			get_node("bottom").set_hidden(false)
+		
+		_split_mode = split_mode

+ 1 - 1
2d/split_screen_platformer/split_stage.tscn

@@ -16,7 +16,7 @@ margin/top = 0.0
 margin/right = 0.0
 margin/bottom = 0.0
 script/script = ExtResource( 1 )
-split = 1
+split_mode = 1
 
 [node name="top" type="Control" parent="."]