Skip to content

Commit af2892e

Browse files
authored
Added basic random numbers example. (#130)
1 parent d7e3b9f commit af2892e

File tree

9 files changed

+163
-0
lines changed

9 files changed

+163
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
path_settings {
2+
path: "**"
3+
profile: "Default"
4+
}
5+
profiles {
6+
name: "Default"
7+
platforms {
8+
os: OS_ID_GENERIC
9+
formats {
10+
format: TEXTURE_FORMAT_RGBA
11+
compression_level: BEST
12+
compression_type: COMPRESSION_TYPE_DEFAULT
13+
}
14+
mipmaps: false
15+
max_texture_size: 0
16+
premultiply_alpha: true
17+
}
18+
}
146 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
font: "/assets/SourceSansPro-Semibold.ttf"
2+
material: "/builtins/fonts/font.material"
3+
size: 48
4+
outline_alpha: 0.0
5+
outline_width: 0.0
9.05 KB
Loading

basics/random_numbers/example.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
tags: basics
3+
title: Random numbers
4+
brief: This example shows how to generate pseudo-random numbers in Defold using built-in math API.
5+
author: Defold Foundation
6+
scripts: random_numbers.script
7+
thumbnail: thumbnail.png
8+
---
9+
10+
In this example you'll learn how to generate pseudo-random numbers in Defold using built-in math API.
11+
12+
In the example there is only a game object containing:
13+
- *Label* component where we show the text information
14+
- *Script* component where we generate random numbers
15+
16+
![collection](collection.png)
17+
18+
Script sets the built-in random generator with a value of os.time() - which should be different every time you run it.
19+
Then produces 3 random numbers using math.random().
20+
21+
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)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: "default"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "go"
5+
data: "components {\n"
6+
" id: \"script\"\n"
7+
" component: \"/example/random_numbers.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"label\"\n"
11+
" type: \"label\"\n"
12+
" data: \"size {\\n"
13+
" x: 600.0\\n"
14+
" y: 400.0\\n"
15+
"}\\n"
16+
"color {\\n"
17+
" x: 0.2\\n"
18+
" y: 0.302\\n"
19+
" z: 0.702\\n"
20+
"}\\n"
21+
"pivot: PIVOT_W\\n"
22+
"text: \\\"Label\\\"\\n"
23+
"font: \\\"/assets/text48.font\\\"\\n"
24+
"material: \\\"/builtins/fonts/label.material\\\"\\n"
25+
"\"\n"
26+
" scale {\n"
27+
" x: 0.7\n"
28+
" y: 0.7\n"
29+
" }\n"
30+
"}\n"
31+
""
32+
position {
33+
x: 50.0
34+
y: 360.0
35+
z: 0.5
36+
}
37+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function init(self)
2+
local seed = os.time()
3+
math.randomseed(seed) -- <1>
4+
5+
label.set_text("#label", "Seed: " .. seed
6+
.. "\nRandom number (0 - 1): " .. math.random() -- <2>
7+
.. "\nRandom integer (1 - 100): " .. math.random(100) -- <3>
8+
.. "\nRandom integer (-10 - 0): " .. math.random(-10,0)) -- <4>
9+
end
10+
11+
--[[
12+
1. First, set the randomseed. It can be specific, if you always want to generate same numbers,
13+
otherwise you can utilise e.g. os.time, like in the example to make it different each time.
14+
2. math.random() with no arguments - generates a random floating point number between 0-1.
15+
3. math.random(X) with one argument - generates an integer between 1 - X.
16+
4. math.random(X, Y) with two arguments - generates an integer between X - Y.
17+
--]]

basics/random_numbers/game.project

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[project]
2+
title = Defold-examples
3+
version = 0.1
4+
5+
[bootstrap]
6+
main_collection = /example/random_numbers.collectionc
7+
8+
[input]
9+
game_binding = /builtins/input/all.input_bindingc
10+
repeat_interval = 0.05
11+
12+
[display]
13+
width = 720
14+
height = 720
15+
high_dpi = 1
16+
17+
[physics]
18+
scale = 0.02
19+
gravity_y = -500.0
20+
21+
[script]
22+
shared_state = 1
23+
24+
[collection_proxy]
25+
max_count = 256
26+
27+
[label]
28+
subpixels = 1
29+
30+
[sprite]
31+
subpixels = 1
32+
max_count = 32765
33+
34+
[windows]
35+
iap_provider =
36+
37+
[android]
38+
package = com.defold.examples
39+
40+
[ios]
41+
bundle_identifier = com.defold.examples
42+
43+
[osx]
44+
bundle_identifier = com.defold.examples
45+
46+
[html5]
47+
show_fullscreen_button = 0
48+
show_made_with_defold = 0
49+
scale_mode = no_scale
50+
heap_size = 64
51+
52+
[graphics]
53+
texture_profiles = /all.texture_profiles
54+
55+
[collection]
56+
max_instances = 32765
57+
58+
[particle_fx]
59+
max_emitter_count = 1024
60+
61+
[render]
62+
clear_color_blue = 1.0
63+
clear_color_green = 1.0
64+
clear_color_red = 1.0
65+
41 KB
Loading

0 commit comments

Comments
 (0)