Guide:Godot: Difference between revisions

From Game Making Tools Wiki
(→‎Tutorials: Car game tut)
(→‎video-to-texture.gd: Updated script to work with Godot 3)
Line 9: Line 9:


<pre>
<pre>
extends TestCube
extends MeshInstance


var stream = preload("video.ogv")
var stream = preload("video.ogv")


func _ready():
func _ready():
    var player = VideoPlayer.new()
  var player = VideoPlayer.new()
    player.set_stream(stream)
  player.set_stream(stream)
    add_child(player)
  add_child(player)
    var texture = player.get_video_texture()
  var texture = player.get_video_texture()
    var material = FixedMaterial.new()
  material.albedo_texture = texture
    material.set_texture(FixedMaterial.PARAM_DIFFUSE, texture)
  material.set_flag(material.FLAG_UNSHADED, true)
    material.set_flag(Material.FLAG_UNSHADED, true)
  set_material_override(material)
    set_material_override(material)
  player.play()
    player.play()
</pre>
</pre>


* Replace <code>"video.ogv"</code> with the path to your file. For me it is <code>"res://videos/test2.ogv"</code>. You can also use relative paths.
* Replace <code>"video.ogv"</code> with the path to your file. For me it is <code>"res://videos/test5.ogv"</code>. You can also use relative paths.
* <code>TestCube</code> can be replaced with whatever object you have this attached to, I think?
* <code>MeshInstance</code> can be replaced with whatever object you have this attached to. <code>MeshInstance</code> is the default for new mesh nodes.


And here's a version with looping video :) (and tabs ;))
And here's a version with looping video :) (and tabs ;))


<pre>
<pre>
extends TestCube
extends MeshInstance


var stream = preload("video.ogv")
var stream = preload("video.ogv")
Line 40: Line 39:
add_child(player)
add_child(player)
var texture = player.get_video_texture()
var texture = player.get_video_texture()
var material = FixedMaterial.new()
var material = SpatialMaterial.new()
material.set_texture(FixedMaterial.PARAM_DIFFUSE, texture)
material.albedo_texture = texture
material.set_flag(Material.FLAG_UNSHADED, true)
material.set_flag(material.FLAG_UNSHADED, true)
set_material_override(material)
set_material_override(material)
set_process(true)


func _process(delta):
func _physics_process(_delta):
if not player.is_playing():
if not player.is_playing():
player.play()
player.play()
</pre>
</pre>
* Note that the variable declaration <code>var player = VideoPlayer.new()</code> moves outside the <code>_ready</code> function so that it can be used elsewhere.
* Using <code>_delta</code> instead of <code>delta</code> is a trick to stop it reporting an error for not being referenced inside that function.
</div></div>
</div></div>



Revision as of 11:16, 20 November 2019

Scripts

video-to-texture.gd

This script renders a video to a new material on the object this script is attached to.

Godot only supports the Theora video codec, but you can use something like FFmpeg to convert whatever video you have.

Source: Video skydome for VR - video to image texture? Project video on sphere mesh?

extends MeshInstance

var stream = preload("video.ogv")

func _ready():
  var player = VideoPlayer.new()
  player.set_stream(stream)
  add_child(player)
  var texture = player.get_video_texture()
  material.albedo_texture = texture
  material.set_flag(material.FLAG_UNSHADED, true)
  set_material_override(material)
  player.play()
  • Replace "video.ogv" with the path to your file. For me it is "res://videos/test5.ogv". You can also use relative paths.
  • MeshInstance can be replaced with whatever object you have this attached to. MeshInstance is the default for new mesh nodes.

And here's a version with looping video :) (and tabs ;))

extends MeshInstance

var stream = preload("video.ogv")
var player = VideoPlayer.new()

func _ready():
	player.set_stream(stream)
	add_child(player)
	var texture = player.get_video_texture()
	var material = SpatialMaterial.new()
	material.albedo_texture = texture
	material.set_flag(material.FLAG_UNSHADED, true)
	set_material_override(material)

func _physics_process(_delta):
	if not player.is_playing():
		player.play()
  • Note that the variable declaration var player = VideoPlayer.new() moves outside the _ready function so that it can be used elsewhere.
  • Using _delta instead of delta is a trick to stop it reporting an error for not being referenced inside that function.

Reading and Writing Files

For example reading config files, and saving and loading games.

Tutorials

Scripting

  • Ivan Skodje's Youtube channel has Godot tutorials related to making full games and scripting.
  • Python - Free Python coarse on Code Academy.

See Also