diff --git a/basics/random_numbers/all.texture_profiles b/basics/random_numbers/all.texture_profiles new file mode 100644 index 0000000..5b0d776 --- /dev/null +++ b/basics/random_numbers/all.texture_profiles @@ -0,0 +1,18 @@ +path_settings { + path: "**" + profile: "Default" +} +profiles { + name: "Default" + platforms { + os: OS_ID_GENERIC + formats { + format: TEXTURE_FORMAT_RGBA + compression_level: BEST + compression_type: COMPRESSION_TYPE_DEFAULT + } + mipmaps: false + max_texture_size: 0 + premultiply_alpha: true + } +} diff --git a/basics/random_numbers/assets/SourceSansPro-Semibold.ttf b/basics/random_numbers/assets/SourceSansPro-Semibold.ttf new file mode 100755 index 0000000..5020594 Binary files /dev/null and b/basics/random_numbers/assets/SourceSansPro-Semibold.ttf differ diff --git a/basics/random_numbers/assets/text48.font b/basics/random_numbers/assets/text48.font new file mode 100644 index 0000000..2622c04 --- /dev/null +++ b/basics/random_numbers/assets/text48.font @@ -0,0 +1,5 @@ +font: "/assets/SourceSansPro-Semibold.ttf" +material: "/builtins/fonts/font.material" +size: 48 +outline_alpha: 0.0 +outline_width: 0.0 diff --git a/basics/random_numbers/collection.png b/basics/random_numbers/collection.png new file mode 100644 index 0000000..5cdf4c1 Binary files /dev/null and b/basics/random_numbers/collection.png differ diff --git a/basics/random_numbers/example.md b/basics/random_numbers/example.md new file mode 100644 index 0000000..27f009e --- /dev/null +++ b/basics/random_numbers/example.md @@ -0,0 +1,21 @@ +--- +tags: basics +title: Random numbers +brief: This example shows how to generate pseudo-random numbers in Defold using built-in math API. +author: Defold Foundation +scripts: random_numbers.script +thumbnail: thumbnail.png +--- + +In this example you'll learn how to generate pseudo-random numbers in Defold using built-in math API. + +In the example there is only a game object containing: +- *Label* component where we show the text information +- *Script* component where we generate random numbers + +![collection](collection.png) + +Script sets the built-in random generator with a value of os.time() - which should be different every time you run it. +Then produces 3 random numbers using math.random(). + +For more details refer to Defold API: [https://defold.com/ref/stable/math-lua/#math.random:m-n](https://defold.com/ref/stable/math-lua/#math.random:m-n) \ No newline at end of file diff --git a/basics/random_numbers/example/random_numbers.collection b/basics/random_numbers/example/random_numbers.collection new file mode 100644 index 0000000..ac468f5 --- /dev/null +++ b/basics/random_numbers/example/random_numbers.collection @@ -0,0 +1,37 @@ +name: "default" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"script\"\n" + " component: \"/example/random_numbers.script\"\n" + "}\n" + "embedded_components {\n" + " id: \"label\"\n" + " type: \"label\"\n" + " data: \"size {\\n" + " x: 600.0\\n" + " y: 400.0\\n" + "}\\n" + "color {\\n" + " x: 0.2\\n" + " y: 0.302\\n" + " z: 0.702\\n" + "}\\n" + "pivot: PIVOT_W\\n" + "text: \\\"Label\\\"\\n" + "font: \\\"/assets/text48.font\\\"\\n" + "material: \\\"/builtins/fonts/label.material\\\"\\n" + "\"\n" + " scale {\n" + " x: 0.7\n" + " y: 0.7\n" + " }\n" + "}\n" + "" + position { + x: 50.0 + y: 360.0 + z: 0.5 + } +} diff --git a/basics/random_numbers/example/random_numbers.script b/basics/random_numbers/example/random_numbers.script new file mode 100644 index 0000000..ad97ab5 --- /dev/null +++ b/basics/random_numbers/example/random_numbers.script @@ -0,0 +1,17 @@ +function init(self) + local seed = os.time() + math.randomseed(seed) -- <1> + + label.set_text("#label", "Seed: " .. seed + .. "\nRandom number (0 - 1): " .. math.random() -- <2> + .. "\nRandom integer (1 - 100): " .. math.random(100) -- <3> + .. "\nRandom integer (-10 - 0): " .. math.random(-10,0)) -- <4> +end + +--[[ +1. First, set the randomseed. It can be specific, if you always want to generate same numbers, + otherwise you can utilise e.g. os.time, like in the example to make it different each time. +2. math.random() with no arguments - generates a random floating point number between 0-1. +3. math.random(X) with one argument - generates an integer between 1 - X. +4. math.random(X, Y) with two arguments - generates an integer between X - Y. +--]] \ No newline at end of file diff --git a/basics/random_numbers/game.project b/basics/random_numbers/game.project new file mode 100644 index 0000000..2214614 --- /dev/null +++ b/basics/random_numbers/game.project @@ -0,0 +1,65 @@ +[project] +title = Defold-examples +version = 0.1 + +[bootstrap] +main_collection = /example/random_numbers.collectionc + +[input] +game_binding = /builtins/input/all.input_bindingc +repeat_interval = 0.05 + +[display] +width = 720 +height = 720 +high_dpi = 1 + +[physics] +scale = 0.02 +gravity_y = -500.0 + +[script] +shared_state = 1 + +[collection_proxy] +max_count = 256 + +[label] +subpixels = 1 + +[sprite] +subpixels = 1 +max_count = 32765 + +[windows] +iap_provider = + +[android] +package = com.defold.examples + +[ios] +bundle_identifier = com.defold.examples + +[osx] +bundle_identifier = com.defold.examples + +[html5] +show_fullscreen_button = 0 +show_made_with_defold = 0 +scale_mode = no_scale +heap_size = 64 + +[graphics] +texture_profiles = /all.texture_profiles + +[collection] +max_instances = 32765 + +[particle_fx] +max_emitter_count = 1024 + +[render] +clear_color_blue = 1.0 +clear_color_green = 1.0 +clear_color_red = 1.0 + diff --git a/basics/random_numbers/thumbnail.png b/basics/random_numbers/thumbnail.png new file mode 100644 index 0000000..29883d7 Binary files /dev/null and b/basics/random_numbers/thumbnail.png differ