ID:34923
 
A BYONDscape Classic!


Picking icon states at runtime

Some of you may have wondered: "How do I pick a random state from an icon at runtime?" Here's how!

What you need for this project:
* The BYOND package (Dream Maker/Dream Seeker)
* An icon with multiple states

Now let's get started. This is a very simple thing to do; all you do is use a few of the built-in procs Dantom has so kindly provided. We'll use:

  pick() ...picks a random item from a list.

  icon_states() ...generates a list of all icons in a file.


turf/randomturf
icon = 'turfs.dmi'
New() //when it's created
pick(icon_states(src.icon)) //picks a random icon_state from the list generated from src's icon

//Now for it to take effect on the whole world (random map type deal)
world
turf = /turf/randomturf

//Now if you had "sand", "desert", and "grass" icon states in turfs.dmi, they would appear randomly all over your map.

There -- now you have a random (totally random!) map generator.

Now, say you want to pick an icon state from a icon you define. You'd have to pass the icon's name into icon_states():

mob/verb/Random_icon_state()
//if you wanted to pick from 'icons.dmi'
usr.icon_state = pick(icon_states('icons.dmi')) //passed the file name into the arguments

Now, if you wanted to view all the icon_states of a file, you would have to loop over all of its states:

mob/verb/View_icon_states()
for(var/T in icon_states('icons.dmi')) //loops over all the icon_states
usr<<T //and outputs them.

Save files

Save files are a big part of BYOND nowadays. People use them for many things, such as saving character progress and saving pieces of their games.

Step one: understanding the basics

This is a really basic code that I will comment to explain:

mob/verb/save()
var/savefile/F = new("[usr.key].sav") //The extension doesn't matter as long as DS/DM knows
//it's a save file; the new("[usr.key].sav") part creates a file called
//userskey.sav (for example, Nadrew.sav) in the directory your game's in.
Write(F) //This calls the Write proc which saves all non-tmp, non-const, and non-global vars to the file F.

mob/verb/load()
var/savefile/F = new("[usr.key].sav") //This MUST be the exact name of the file you saved.
Read(F) //reads the file, and loads all data within it.

Step two: single var saving

"That was easy," you think. "But what if I only want to save certain vars?" Well, you'd have to save them directly into the savefile. Here's how.

mob
var
somevar = 3 //For example purposes only.

mob/verb/save_var()
var/savefile/F = new("[usr.key].sav") //See above.
F["varname"] << somevar //This saves somevar directly into the file, and tags it "varname"

mob/verb/load_var()
var/savefile/F = new("[usr.key].sav")
F["varname"] >> somevar //This finds the var tagged "varname" in file and loads it into somevar

Step three: using what you've learned

Using both of the previous steps, you can now create a sort of save system.

mob
var
hassaved = 0 //This will be used to check if you've already saved

mob/Login()
var/savefile/F = new("[src.key].sav")
F["hassaved"] >> hassaved //loads hassaved
if(hassaved) //if the hassaved var is 1 or TRUE
//Pop up a box asking you if you want to make a new player or load.
switch(alert("You have a file; what do you want to do?","Savefile alert!","Load","New"))
if("Load") //if you pick load
Read(F) //loads
else //if new
..() //continues without loading
else //no previous save
..()

mob/Logout() //Save upon leaving
var/savefile/F = new("[src.key].sav")
src.hassaved = 1 //now have saved
F["hassaved"] << hassaved //saves the hassaved var
Write(F) //saves the rest
del(src) //deletes their mob

Now, were savefiles as hard as you thought?

For further reference on this subject

Deadron's simple saving
Deadron's character saving


BYOND CGI

Many people don't know that you can make dynamic web pages using BYOND's built CGI (Common Gateway Interface) functions. CGI is commonly used to process HTML form input and generate new HTML forms based on the result.

What you'll need for this project:
* Dantom.CGI (Dantom's CGI library located here;
* Dantom.htmllib (Dantom's html library) located here;
* A server that allows BYOND CGI (every server has its own rules about this; if you don't know whether a server supports or allows BYOND CGI, you'll need to ask the server administrator).

Now that you've gotten that out of the way, lets get started.

Step one: including the libraries

When you open Dream Maker, open or create a new enviroment. Now click the "Lib" folder icon; you should see dantom.CGI and dantom.htmllib in there. Check the boxes next to them, and now it should look like this:

Step two: coding your first form

Now that you've gotten those libraries included, you can make a form to use with your CGI. Here's an example form:

Form //defined in the htmllib

startup
submit = "Start" //now your submit button will say "Start" on it

var
name
age
age_size = 2 //makes the age box the size 2
age_maxlen = 2 //now you can only enter two characters into the age box

ProcessForm() //also defined in the htmllib, and is called when the submit button is pressed
usr<<"Hello [name] you're [age] years in age!" //see what you did.

HtmlLayout() return \ //Customize how your form looks.
{"

Name:
[name]<br>

Age:
[age]<br>

[submit] - [reset]

"}

There -- that's your first form. It should look like this:

Name:
Age
-

Step three: turning your form into cgi

Using the code above add this line to your code:

CGI/default_form = /Form/startup

Now upload your .dmb file to your server, and see the results.

Check the boxes next to them, and now it should look like this:

Mine doesn't look like a red X. :(
Good catch, Jon88! There should be a better image there now. Thanks!