creatures caves welcome, guest
downloads   gallery   dev   community   creatchi   forum   mycaves
hack shack | script reservations | dev resources | dev forum

  

Visualisation techniques?  on 1/2/2018 | 2 likes

Hello,
     How do you sketch out / visualise what a more complicated script, like a critter or plant timer script, will do?
- Malkin


Dear Malkin,
     Hey Malkin,

I don't have a specific way to sketch/figure out a complex agent, but for the sake of this answer I'll just write down my thought process for a random object instead.
Everyone has their own way to figure out how to code a complex agent, I have mine. For this example, I've decided on making a random tree. Enjoy!

Step 1 - Documenting
Write down what you want your object to do. How long should it live? What environment does it need? Will it influence the environment? Will it be seasonal? Be as detailed as possible. For now I'll make a tree that spawns delicious fruit.

Example:
Tree
- seasonal colors, leave color change every season
- During spring, it'll spawn flowers, during summer/fall - fruit
- It will live for 5 - 8 ingame years
- When dying, it will put some nutrients into the ground
- Only one tree per metaroom, will only grow in boggy soil.
- Minimum 2 fruits, maximum 4 per tree. A new fruit will always grow when there's less than 2 fruits. Depending on the nutrients/light/heat in the environment, a third and fourth fruit may grow.
- During the night, fruit/flowers will not drop
- Actions: Pushing/pulling the tree does nothing special, hitting the tree may drop some full grown fruit.

Fruit
- Grows from a flower, hangs on the tree for one ingame day before falling off.
- Adds a dose of vitamin C
- Depending on the nutrients around the tree when the fruit was created on the tree, will give more or less "eaten fruit" stim
- Will spoil after three ingame days. Gives nutrients to environment.
- May grow into a tree if there are no other trees in this metaroom, and if there are sufficient nutrients around.
- Actions: pickup, eat

Flower
- Will close at night when hanging on the tree
- During spring, will fall off when fully grown. Summer/fall, it'll grow into fruit.
- Flowers will grow for 1 ingame day and bloom for 1 ingame day before falling off/growing into something else
- Gives some nutrients
- Actions: pickup, eat

Detritus
- This is what happens when a flower/fruit has turned bad.
- It stays around for one ingame day, and when disappearing it will give nutrients to the environment.
- It's edible. Just not yummy. :P
- Actions: pickup, eat


...I could probably go more complex than this, but let's keep it semi-simple for now.

Step 2 - Commenting

Once you wrote down all of your objects and all of the requirements, you could make a small flowchart to visualize how the object itself needs to work. I personally don't use flowcharts, but sometimes with a very complex agent I fill the cosfile with comments before starting to code anything.

So, yeah, let's try my approach and create a small .cosfile. I make sure I have a species ID number ready, and I start writing comments directly into a cosfile. Take each object and write the initial scripts you will need for each object. Don't worry if you don't get all of them right away, you can still add them later.

** RESERVED ID: 36472

**
** INSTALL SCRIPTS
**

** This is a tree.
** This is fruit
** This is a flower
** This is detritus

**
** EVENT SCRIPTS
**

** Tree creation
** Tree timer

** Fruit creation
** Fruit timer
** Fruit pickup
** Fruit eat

** Flower creation
** Flower timer
** Flower pickup
** Flower eat

** Detritus creation
** Detritus timer
** Detritus pickup
** Detritus eat



This is how my .cosfile looks now. In the install script, I'll write down the code I need to inject either object ingame, but I will comment it out later. I consider it very useful to have example injection scripts on top of the script, then if you need to create an object or write a script for something you can just copy the code with the correct classification numbers from there.

Occasionally, if I'm not in the mood to look up any CAOS at all but I want to have a basic idea ready, I write down some pseudocode in the comments to show what I actually want to achieve.

Step 3 - Code the absolute basics

Once you got your basic commented file down, you can start to create the scripts. I usually start coding the absolute basics first, making sure the object just works, whether or not the conditions are ideal. When I'm sure the basics work, I start adding in more and more requirements/cases/doifs.
For example, let's do the most basic code for the tree. The creation and timer script.

Let's assume the tree uses the image "pilla_tree_1", which contains 5 frames of a growing tree. The tree is fully grown when it reaches the 5th frame (so frame number 4).


** Tree creation
scrp 2 4 36472 10
** Attr: Suffer collisions (64) and physics (128 ) - 192
attr 192
** Bhvr: act1 (1), act2 (2), hit (8 ) - 11
bhvr 11
** Set the tick to 1/5th of an ingame day. A tree
setv va00 game "engine_LengthOfDayInMinutes"
divv va00 5
mulv va00 1200
endm

** Tree timer
scrp 2 4 36472 9
** If the tree is not growing yet, grow the tree
doif pose lt 4
setv va00 pose
addv va00 1
pose va00
** If the tree is fully grown, spawn fruit
else
gsub spawnFruit
endi

** Everything below this point are subroutines for the tree timer
subr spawnFruit
retn
endm



I now created a script for a tree that spawns fruit if it's fully grown, this are the basics of the script.

I use subroutines a lot to keep the code readable, it also allows you to add in the same subroutine in different spots in the code, while still allowing you to pick the initial variables needed for the subroutine.

Step 4 - Increase complexity

If you want to go more and more complex, you can addd in more and more requirements/cases/exceptions to the existing code. Right now it'll forever spawn fruit, it will never die, etc. If you want it to live for a specific duration, you'll have to add life variables and increase them every time a timer tick runs. If you only want it to grow on normal/boggy soil (or whatever), you can add in a doif that specifies that the soil has to be any of those types, etc.
As long as the basic code is working, you can keep on adding to it until it works exactly as you want it to work.

That's pretty much my thought process.



-
Extra
-


$$ - Pseudocode

Pseudocode? What dat? It's just some code YOU create. With pseudocode you're writing the code for something without worrying about using the wrong commands/no punctuation/... - You just have to think logically and think about how the object has to act, and write this down.

For example, here's some example pseudocode for the fruit once it dropped from the tree.

* if current age < total life:
** increase life of fruit
** if environment = boggy soil, dry soil, normal soil and if no other trees around
*** Grow into a tree
** End the if block
* Else if current age > total life
** If environment != water
*** Turn into detritus
** Else
*** Disappear
** End if block
* End if block



$$ - Flowcharts

Some people draw up flowcharts if they need to write complex code. I personally never use flowcharts as I'm better at reading code itself than a flowchart, but obviously this is not the case for everyone.
If you feel like you would be able to work better with flowcharts, then by all means make a flowchart. Just google some info about flowcharts to get started :)

$$ - Custom scripts

In case you would want another object to 'change' the initial object, I usually work with custom scripts. Let's make a fictional example: I want that the fruit which has dropped from the tree, when eaten, makes the tree drop some leaves. (This makes no sense whatsoever but it fits the example. Deal with it. :P)

So, yeah, in this case the fruit would have to send a message to the tree when eaten, a message that tells the tree to drop some leaves. For script numbers, you can usually safely pick any number over 1000. Let's use script number 1012 for the tree:

scrp 2 4 36472 1012
gsub dropLeaf

subr dropLeaf
retn
endm


I'm not doing any actual coding yet, just preparing the scripts we'll need. Now, script number 1012 has to be triggered when the fruit has been eaten, so we'll need to do the fruit script next. Also, for fun, I want the fruit to inject vitamin C.

scrp 2 8 36472 12
* Stim writ eaten fruit
* Chem vitamin C (optional)
* Send message 1012 to the tree to trigger the leaves
endm


Now that I write down the utter basics for the fruit script, I realize the fruit needs to have the tree saved as an OV so the fruit can refer back to the tree. I'd go to the spawnFruit subroutine in the tree timer script and make sure the tree variable gets added to the fruit so the fruit can consistently refer back to the tree.

And like this you keep on writing, adding to and refining the code.

I hope my incoherent rambling somehow managed to answer your question! If you have any more questions, don't hesitiate to ask ;)

(Also, special thanks to Malkin for adding all the resource links!)
- Pilla
 
 
Pilla | 1/4/2018  log in to like post  4

Hey Malkin,

I don't have a specific way to sketch/figure out a complex agent, but for the sake of this answer I'll just write down my thought process for a random object instead.
Everyone has their own way to figure out how to code a complex agent, I have mine. For this example, I've decided on making a random tree. Enjoy!

Step 1 - Documenting
Write down what you want your object to do. How long should it live? What environment does it need? Will it influence the environment? Will it be seasonal? Be as detailed as possible. For now I'll make a tree that spawns delicious fruit.

Example:
Tree
- seasonal colors, leave color change every season
- During spring, it'll spawn flowers, during summer/fall - fruit
- It will live for 5 - 8 ingame years
- When dying, it will put some nutrients into the ground
- Only one tree per metaroom, will only grow in boggy soil.
- Minimum 2 fruits, maximum 4 per tree. A new fruit will always grow when there's less than 2 fruits. Depending on the nutrients/light/heat in the environment, a third and fourth fruit may grow.
- During the night, fruit/flowers will not drop
- Actions: Pushing/pulling the tree does nothing special, hitting the tree may drop some full grown fruit.

Fruit
- Grows from a flower, hangs on the tree for one ingame day before falling off.
- Adds a dose of vitamin C
- Depending on the nutrients around the tree when the fruit was created on the tree, will give more or less "eaten fruit" stim
- Will spoil after three ingame days. Gives nutrients to environment.
- May grow into a tree if there are no other trees in this metaroom, and if there are sufficient nutrients around.
- Actions: pickup, eat

Flower
- Will close at night when hanging on the tree
- During spring, will fall off when fully grown. Summer/fall, it'll grow into fruit.
- Flowers will grow for 1 ingame day and bloom for 1 ingame day before falling off/growing into something else
- Gives some nutrients
- Actions: pickup, eat

Detritus
- This is what happens when a flower/fruit has turned bad.
- It stays around for one ingame day, and when disappearing it will give nutrients to the environment.
- It's edible. Just not yummy. :P
- Actions: pickup, eat


...I could probably go more complex than this, but let's keep it semi-simple for now.

Step 2 - Commenting

Once you wrote down all of your objects and all of the requirements, you could make a small flowchart to visualize how the object itself needs to work. I personally don't use flowcharts, but sometimes with a very complex agent I fill the cosfile with comments before starting to code anything.

So, yeah, let's try my approach and create a small .cosfile. I make sure I have a species ID number ready, and I start writing comments directly into a cosfile. Take each object and write the initial scripts you will need for each object. Don't worry if you don't get all of them right away, you can still add them later.

** RESERVED ID: 36472

**
** INSTALL SCRIPTS
**

** This is a tree.
** This is fruit
** This is a flower
** This is detritus

**
** EVENT SCRIPTS
**

** Tree creation
** Tree timer

** Fruit creation
** Fruit timer
** Fruit pickup
** Fruit eat

** Flower creation
** Flower timer
** Flower pickup
** Flower eat

** Detritus creation
** Detritus timer
** Detritus pickup
** Detritus eat



This is how my .cosfile looks now. In the install script, I'll write down the code I need to inject either object ingame, but I will comment it out later. I consider it very useful to have example injection scripts on top of the script, then if you need to create an object or write a script for something you can just copy the code with the correct classification numbers from there.

Occasionally, if I'm not in the mood to look up any CAOS at all but I want to have a basic idea ready, I write down some pseudocode in the comments to show what I actually want to achieve.

Step 3 - Code the absolute basics

Once you got your basic commented file down, you can start to create the scripts. I usually start coding the absolute basics first, making sure the object just works, whether or not the conditions are ideal. When I'm sure the basics work, I start adding in more and more requirements/cases/doifs.
For example, let's do the most basic code for the tree. The creation and timer script.

Let's assume the tree uses the image "pilla_tree_1", which contains 5 frames of a growing tree. The tree is fully grown when it reaches the 5th frame (so frame number 4).


** Tree creation
scrp 2 4 36472 10
** Attr: Suffer collisions (64) and physics (128 ) - 192
attr 192
** Bhvr: act1 (1), act2 (2), hit (8 ) - 11
bhvr 11
** Set the tick to 1/5th of an ingame day. A tree
setv va00 game "engine_LengthOfDayInMinutes"
divv va00 5
mulv va00 1200
endm

** Tree timer
scrp 2 4 36472 9
** If the tree is not growing yet, grow the tree
doif pose lt 4
setv va00 pose
addv va00 1
pose va00
** If the tree is fully grown, spawn fruit
else
gsub spawnFruit
endi

** Everything below this point are subroutines for the tree timer
subr spawnFruit
retn
endm



I now created a script for a tree that spawns fruit if it's fully grown, this are the basics of the script.

I use subroutines a lot to keep the code readable, it also allows you to add in the same subroutine in different spots in the code, while still allowing you to pick the initial variables needed for the subroutine.

Step 4 - Increase complexity

If you want to go more and more complex, you can addd in more and more requirements/cases/exceptions to the existing code. Right now it'll forever spawn fruit, it will never die, etc. If you want it to live for a specific duration, you'll have to add life variables and increase them every time a timer tick runs. If you only want it to grow on normal/boggy soil (or whatever), you can add in a doif that specifies that the soil has to be any of those types, etc.
As long as the basic code is working, you can keep on adding to it until it works exactly as you want it to work.

That's pretty much my thought process.



-
Extra
-


$$ - Pseudocode

Pseudocode? What dat? It's just some code YOU create. With pseudocode you're writing the code for something without worrying about using the wrong commands/no punctuation/... - You just have to think logically and think about how the object has to act, and write this down.

For example, here's some example pseudocode for the fruit once it dropped from the tree.

* if current age < total life:
** increase life of fruit
** if environment = boggy soil, dry soil, normal soil and if no other trees around
*** Grow into a tree
** End the if block
* Else if current age > total life
** If environment != water
*** Turn into detritus
** Else
*** Disappear
** End if block
* End if block



$$ - Flowcharts

Some people draw up flowcharts if they need to write complex code. I personally never use flowcharts as I'm better at reading code itself than a flowchart, but obviously this is not the case for everyone.
If you feel like you would be able to work better with flowcharts, then by all means make a flowchart. Just google some info about flowcharts to get started :)

$$ - Custom scripts

In case you would want another object to 'change' the initial object, I usually work with custom scripts. Let's make a fictional example: I want that the fruit which has dropped from the tree, when eaten, makes the tree drop some leaves. (This makes no sense whatsoever but it fits the example. Deal with it. :P)

So, yeah, in this case the fruit would have to send a message to the tree when eaten, a message that tells the tree to drop some leaves. For script numbers, you can usually safely pick any number over 1000. Let's use script number 1012 for the tree:

scrp 2 4 36472 1012
gsub dropLeaf

subr dropLeaf
retn
endm


I'm not doing any actual coding yet, just preparing the scripts we'll need. Now, script number 1012 has to be triggered when the fruit has been eaten, so we'll need to do the fruit script next. Also, for fun, I want the fruit to inject vitamin C.

scrp 2 8 36472 12
* Stim writ eaten fruit
* Chem vitamin C (optional)
* Send message 1012 to the tree to trigger the leaves
endm


Now that I write down the utter basics for the fruit script, I realize the fruit needs to have the tree saved as an OV so the fruit can refer back to the tree. I'd go to the spawnFruit subroutine in the tree timer script and make sure the tree variable gets added to the fruit so the fruit can consistently refer back to the tree.

And like this you keep on writing, adding to and refining the code.

I hope my incoherent rambling somehow managed to answer your question! If you have any more questions, don't hesitiate to ask ;)

(Also, special thanks to Malkin for adding all the resource links!)


downloads
cobs
adoptions
creaturelink
metarooms
breeds
 
gallery
art
wallpaper
screenshots
graphics
promos
sprites
dev
hack shack
script reservations
dev resources
active projects
dev forum
 
community
links
advice
chat
polls
resources
creatchi
 
forum
bookmarks
general
news
help
development
strangeo
survivor
mycaves
log in
register
lost pw
0 online
creatures caves is your #1 resource for the creatures artificial life game series: creatures, creatures 2, creatures 3, docking station, and the upcoming creatures family.

contact    help    privacy policy    terms & conditions    rules    donate    wiki