There are some usefull functions missing from SC, in my opinion. This is the place to share those functions (aka making a method that does the same as the function)
Floor(number)
Function: always rounds down the input number. So 10.1 becomes 10, but 10.9 also becomes 10.
name: floor
parameters: number
return type: integer
LET string$ = STR$(number)
LET string$ = NTHFIELD$(string$,".",1)
LET number = VAL(string$)
RETURN number
Explanation:
first convert the integer to a string. Use that string to seperate the numbers infront and after the "." from each other. Then convert the first number back to a string. So in other words, it cuts of everything after the ".".
Example:
LET number = 5.2
LET number = floor(number)
PRINT STR$(number)
Prints 5
Ceil(number)
Function: always rounds up the input number. So 10.9 becomes 11, but 10.1 also becomes 11.
name: ceil
parameters: number
return type: integer
LET string$ = STR$(number)
LET string1$ = NTHFIELD$(string$,".",1)
LET string2$ = NTHFIELD$(string$,".",2)
IF string2$ <> "" then
LET number = VAL(string1$)
LET number = number + 1
END IF
RETURN number
Explanation:
First convert the number to a string. Story the number infront of the "." in string1$ and everything after in string2$. Then check if string2$ contains any data. If you won't do this check, it will screw up (example: 10 becomes 11). Converrt string1$ (so the number infront of the ".") back to a number so you can add one up (the rounding up part). Then return the number.
Example
LET number = 5.2
LET number = floor(number)
PRINT STR$(number)
Prints 6
12to24$(time$)
This is somewhat more advanced. If you to add real time features like day and night in your game, you might get some bugs when your game is played in different parts of the world. This method converts 12-hour-clock time to 24-hour-clock time.
name: 12to24$
parameters: time$
return type: string
LET string1$ = NTHFIELD$(time$," ",1)
LET string2$ = NTHFIELD$(time$," ",2)
IF string2$ = "pm" then
LET hours$ = NTHFIELD$(string1$,":",1)
LET hours = VAL(hours$)
LET hours = hours + 12
IF hours => 24 then
LET hours = hours - 24
END IF
LET newhours$ = STR$(hours)
LET string1$ = REPLACE$(string1$,hours$,newhours$)
END IF
RETURN string1$
Explanation:
First seperate the time from the "am" or "pm". Check if string2$ is pm, if so add 12 hours to the hours. Replace the new hour with the old and output.
example:
LET time$ = 12to24$("2:30 am")
PRINT time$
This prints 2:30
LET time$ = 12to24$("2:30 pm")
PRINT time$
This prints 14:30
24to12$(time$)
This coverts 24-hour time to 12-hour time (including the addfix pm or am).
name: 24to12$
parameters: time$
return type: string
LET string1$ = NTHFIELD$(time$," ",1)
LET string2$ = NTHFIELD$(time$," ",2)
if string2$ = "" then
LET hours$ = NTHFIELD$(string1$,":",1)
LET hours = VAL(hours$)
if hours > 12 then
LET hours = hours - 12
let add$ = " pm"
else
let add$ = " am"
end if
LET newhours$ = STR$(hours)
LET string1$ = REPLACE$(string1$,hours$,newhours$)
end if
return string1$+add$
So basicly this first checks if there isn't an addfix like pm or am. Then it checks if the time is after 12 noon, if so substract 12 and add pm. If not, just add am. And output this.
example:
LET time$ = 24to12$("13:15")
PRINT time$
Prints 1:15 pm
LET time$ = 24to12$("1:15")
PRINT time$
Prints 1:15 am
Note that it outputs 12:00 noon as 12:00 pm, and 12:00 midnight as 12:00 pm. You might want to build in some kind of check if you want to change this.
So with the last two methods you just convert the time, to make sure it's in your prefered format, and then just do whatever you want to do with it.
Sare your methods, new functions are hacks.
Ok, so SC as minimal array functions right? Well, why not make some more? Here is the first one:
reverse$(string$,seperator$)
This reverses all the values in the array.
name: reverse$
parameters: string$,seperator$
return type: string
LET in_string = false
LET count = COUNTFIELDS$(array$,seperator$)
FOR x = 1 to count
LET numb = count - x + 1
LET string$ = NTHFIELD$(array$,seperator$,numb)
IF x = count then
LET temp_string$ = temp_string$ + string$
ELSE
LET temp_string$ = temp_string$ + string$ + seperator$
END IF
NEXT
RETURN temp_string$
Well if you understand basic SC code, this is easy. First count how many values there are. Then do a repeat loop and select the value, then add them to the string. If the value is the last one to add, you won't need the seperator anymore so that's why the if then is there.
example:
LET array$ = "pizza:fries:pancakes:patties"
LET array2$ = reverse$(array$,":")
PRINT array2$
This prints patties:pancakes:fries:pizza.
Ok, I made one more.
shuffle$(array$,seperator$)
name: shuffle$
parameters: array,seperator$
return type: string
LET in_string = false
LET count = COUNTFIELDS$(array$,seperator$)
LET temp_array$ = array$
LET temp_string$ = ""
FOR x = 1 to count
LET numb = count - x + 1
LET rand= random(numb)
LET string$ = NTHFIELD$(temp_array$,seperator$,rand)
IF x = count then
LET temp_string$ = temp_string$ + string$
ELSE
LET temp_string$ = temp_string$ + string$ + seperator$
END IF
LET temp_array$ = replace$(temp_array$,string$+seperator$,"")
NEXT
RETURN temp_string$
It is the same as the previous one. Only this time there is the extra random and the replaceall$ is added to delete the value from the string, so they don't show up in the outcome twice.
example:
LET array$ = "1:2:3:4:5:6:7:8:9"
LET array2$ = shuffle$(array$,":")
PRINT array2$
As this is random, this could print anything. 2:4:6:5:7:8:1:9:3
I also updated the 12to24 method. In the previous version, if you entered 12:15 pm (just an example), it would return 24:15. However, it should be 00:15. I just added 3 lines of code, and it works now.
Lets make a silvercreator library! Most of the functions that I make are game specific, but this one should prove to be useful for anyone:
name: search_fields$
parameters: source$,field$
return type: string
LET fieldnum = LEN(source$)
FOR s_field = 1 TO fieldnum
IF MID$(source$,s_field,1) = field$ THEN
LET found = 1
END IF
NEXT
IF found = 1 THEN
RETURN "true"
ELSE
RETURN "false"
END IF
// Purpose:
// To search a string for a certain field, and to return "true" if that field is found and "false" if it is not.
// search_fields$: string, string -> string
// IF search_fields$(source$,field$) = "true" THEN
// ...
I made the same thing, but turns out it already exists. It's called instr and returns the field number of the string if it's found.
Array_slice$(array$,seperator$,sliced)
name: array_slice$
paramters: array$,seperator$,sliced
return type: string
LET number = COUNTFIELDS$(array$,seperator$)
LET number1 = number / 2
LET number1 = round(number1)
LET temp_array$ = ""
LET sliced_array$ = ""
FOR x = 1 to number1
IF x = number1 then
LET temp_array$ = temp_array$ + NTHFIELD$(array$,seperator$,x)
ELSE
LET temp_array$ = temp_array$ + NTHFIELD$(array$,seperator$,x) + seperator$
END IF
NEXT
IF sliced = true then
FOR y = number1 + 1 to number
IF y = number then
LET sliced_array$ = sliced_array$ + NTHFIELD$(array$,seperator$,y)
ELSE
LET sliced_array$ = sliced_array$ + NTHFIELD$(array$,seperator$,y) + seperator$
END IF
NEXT
END IF
RETURN temp_array$
This cuts the array in halve. If true is passed for the integer sliced, the other halve of the array is stored in sliced_array$. If false is passed, the other data gets lost, for ever...
example:
LET array$ = "1:2:3:4:5:6:7:8:9"
LET array2$ = array_slice$(array$,":",true)
clear
PRINT "Array 1: "+array$
PRINT "Array 2: "+array2$
PRINT "Sliced A: "+sliced_array$
This prints:
Array 1: 1:2:3:4:5:6:7:8:9
Array 2: 1:2:3:4:5
Sliced A: 6:7:8:9
Yes, I don't know why I made this. It's useless, I think. But maybe someone can think of a purpose.