This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

A few questions (regarding including scripts, 3d collisions and input)

DieselpiratDieselpirat Posts: 4Member
edited August 2016 in Programming
Greetings!<br /><br />I came across Godot a few days ago so I'm fairly new. I have coded before but that's some time ago. I thought the best way to learn it would be by ignoring most of the editor for now and do everything via scripts first. Most questions got easily solved using the documentation and google but I couldn't find answers to all of them.<br /><br />I created a blank project, added a "Spatial" node called 'Spatial' and attached a script to it. <br />
# Main.gd<br /><br />extends Spatial<br /><br />var cam;<br />var cube;<br />var light;<br />var ground;<br />var wall;<br />var wall_mesh;<br />var wall_coll;<br /><br />var player;<br />var player_mesh;<br />var player_coll;<br /><br />func _ready():<br />	self.set_process(true);<br />	self.set_fixed_process(true);<br />	self.set_process_input(true);<br /><br />	player = KinematicBody.new();<br />	add_child(player);<br />	<br />	player_mesh = TestCube.new();<br />	player.add_child(player_mesh);<br />	player_mesh.set_scale(Vector3(.3, .5, .3));<br />	player_mesh.set_translation(player.get_translation() + Vector3(0, .5, 0));<br />	<br />	player_coll = CollisionShape.new();<br />	player_mesh.add_child(player_coll);<br />	player_coll.set_shape(player_mesh);<br />	<br />	wall = StaticBody.new();<br />	add_child(wall);<br />	<br />	wall_mesh = TestCube.new();<br />	wall_mesh.set_scale(Vector3(.2, 2, 6));<br />	wall_mesh.set_translation(Vector3(5, 0, 0));<br />	wall.add_child(wall_mesh);<br />	<br />	wall_coll = CollisionShape.new();<br />	wall_mesh.add_child(wall_coll);<br />	wall_coll.set_shape(wall);<br />	<br />	cam = Camera.new();<br />	add_child(cam);<br />	cam.set_rotation(Vector3(-1.57, 0, 0));<br />	<br />	cube = TestCube.new();<br />	add_child(cube);<br />	cube.set_translation(Vector3(3.8, 1, 0));<br />	cube.set_scale(Vector3(.5, .5, .5));<br />	<br />	ground = TestCube.new();<br />	add_child(ground);<br />	ground.set_scale(Vector3(20, .1, 20));<br />	ground.set_translation(Vector3(0, -.09, 0));<br />	<br />	light = DirectionalLight.new();<br />	add_child(light);<br />	light.set_rotation_deg(Vector3(230, -30, 0));<br />	light.set_project_shadows(true);<br />	light.set_parameter(light.PARAM_ENERGY, 1.3)<br />	light.set_parameter(light.PARAM_SHADOW_DARKENING, .2);<br />	light.set_shadow_mode(light.SHADOW_PARALLEL_4_SPLITS);<br />	light.set_enabled(true);<br /><br /><br />#====================================================================<br /><br />var rota = 0;<br />var _w;<br />var _s;<br />var _a;<br />var _d;<br />var _sprint;<br /><br />var dir = 0;<br />var x;<br />const MOVESPEED = 5;<br />const ROTASPEED = 3;<br />const SPRINTADD = 2;<br /><br /><br />func _input(ev):<br />	if (ev.is_action_released(&quot;ui_accept&quot;)):<br />		x = get_node(&quot;CFunctions&quot;).cNew(player.get_translation());<br />		print(&quot;PING!&quot;);<br />		add_child(x);<br />	<br />	#_w = (ev.is_action_pressed(&quot;up&quot;));<br />	#_s = (ev.is_action_pressed(&quot;down&quot;));<br />	#_a = (ev.is_action_pressed(&quot;left&quot;));<br />	#_d = (ev.is_action_pressed(&quot;right&quot;));<br /><br />func _process(delta):<br />	<br />	# Input<br />	_w = Input.is_key_pressed(KEY_W);<br />	_s = Input.is_key_pressed(KEY_S);<br />	_a = Input.is_key_pressed(KEY_A);<br />	_d = Input.is_key_pressed(KEY_D);<br />	<br />	# Quit<br />	if (Input.is_key_pressed(KEY_ESCAPE)):<br />		get_node(&quot;CFunctions&quot;).cGameQuit();<br />	<br />	# Rotate cube<br />	rota += 2 * delta;<br />	cube.set_rotation(Vector3(rota, .5 * -rota, rota));<br /><br /><br />#====================================================================<br /><br />var move;<br />var tar_x;<br />var tar_z;<br /><br />func _fixed_process(delta):<br />	#MOVEMENT<br />	if (_w or _s or _a or _d):<br />		move = _w - _s;<br />		<br />		tar_x = get_node(&quot;CFunctions&quot;).cLengthDirX(move * MOVESPEED, dir);<br />		tar_z = get_node(&quot;CFunctions&quot;).cLenghtDirZ(move * MOVESPEED, dir);<br />		player.move(Vector3(tar_x * delta, 0, tar_z * delta));<br />		<br />		if (_a): dir += (ROTASPEED&nbsp; + (SPRINTADD / 2));<br />		if (_d): dir -= (ROTASPEED + (SPRINTADD / 2));<br />		print(dir);<br />		player.set_rotation(Vector3(0, deg2rad(dir), 0));<br />	<br />	cam.set_translation(player.get_translation() + Vector3(0, 10, 0));
<br /><br />Then I added another "Spatial" node called 'CFunctions' as a child to the first Spatial Node and attached this script to it containing:<br /><br />
extends Spatial<br /><br />func cGameQuit():<br />	get_tree().quit();<br /><br />func cNew(trns):<br />	var c = TestCube.new();<br />	c.set_translation(trns);<br />	c.set_scale(Vector3(2, 2, 2));<br />	return c;<br /><br />func cLengthDirX(var len, var dir):<br />	return (cos(-dir * (PI / 180)) * len);<br /><br />func cLenghtDirZ(var len, var dir):<br />	return (sin(-dir * (PI / 180)) * len);
<br /><br />Which is basically just a container for functions, which brings me to my first question: <br />1 - Is there any better way to "#include" simple library code from another *.gd file (with code completion)?<br />2 - I couldn't get 'func _input()' to work properly, so I hardcoded the controls in 'func _process()'. _input() does receive an Input, but only one key at a time and just for a few seconds.<br />3 - How do I get the 'player' to collide with 'wall' using the built-in 3D collision system? I couldn't figure out how to set it up.<br /><br />Also, I much appreciate any tips on 'good coding practice'

Tags :

Comments

  • bitwesbitwes Posts: 74Member
    1 - Is there any better way to "#include" simple library code from another *.gd file (with code completion)?
    <br />If you know the other way but are doing it this way for the code completion then the answer is "no".<br /><br />The other way being that there isn't an "include" that will insert your functions into another script.  That would be nice but it's not there.  The way to do this is to create an instance of your script.  Instead of attaching the script to an object, you can just make a script and make an instance of it.<br />[tt]scripts/my_functions.gd[/tt]<br />
    <br />func print_one():<br />&nbsp; print &#039;one&#039;<br />
    
    <br /><br />[tt]scripts/my_does_something.gd[/tt]<br />
    <br />var my_funcs = load(&#039;res://scripts/my_functions.gd&#039;)<br /><br />func _ready():<br />&nbsp; my_funcs.print_one()<br />
    
    <br />There won't be any autocomplete though.<br /><br />
    2 - I couldn't get 'func _input()' to work properly, so I hardcoded the controls in 'func _process()'. _input() does receive an Input, but only one key at a time and just for a few seconds.
    <br />In the [tt]_ready()[/tt] method  you have to enable input processing by calling [tt]set_process_unhandled_input(true)[/tt]<br /><br />

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.