Game Maker's Garage Forum

Game Creation => SilverCreator => Topic started by: Teruri on December 22, 2008, 04:09:20 AM

Title: SC hacks
Post by: Teruri on December 22, 2008, 04:09:20 AM
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
Code: [Select]
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:
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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:
Code: [Select]
LET time$ = 12to24$("2:30 am")
PRINT time$
This prints 2:30
Code: [Select]
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
Code: [Select]
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:
Code: [Select]
LET time$ = 24to12$("13:15")
PRINT time$
Prints 1:15 pm
Code: [Select]
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.
Title: Re: SC hacks
Post by: Gan on December 22, 2008, 06:56:39 AM
Wow, great job Teruri. ;)

Those functions are actually really good, though I haven't made any useful ones. Though you might want to post up Redd's changefield function which allows use of fake arrays.


-Gandolf
Title: Re: SC hacks
Post by: Teruri on December 22, 2008, 06:58:44 AM
Yeah, this one:
http://www.silvercreator.net/cgi-bin/yabb/YaBB.pl?board=sccode;action=display;num=1215970619

If someone as more methods they want to share, go ahead. We could make a vast collection of methods to use.
Title: Re: SC hacks
Post by: Teruri on December 22, 2008, 10:39:44 AM
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
Code: [Select]
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:
Code: [Select]
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

Code: [Select]
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:
Code: [Select]
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.
Title: Re: SC hacks
Post by: WarHampster on December 22, 2008, 05:30:22 PM
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

Code: [Select]
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
// ...


Title: Re: SC hacks
Post by: Teruri on December 23, 2008, 04:40:40 AM
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
Code: [Select]
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:
Code: [Select]
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.
Title: Re: SC hacks
Post by: EqwanoX on December 23, 2008, 10:17:19 AM
this is interesting, that shuffle method is very useful, i made a card game before and it used an excessive amount of code for shuffling
Title: Re: SC hacks
Post by: WarHampster on December 23, 2008, 11:54:27 AM
Quote
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.

Oh. Crap.
Title: Re: SC hacks
Post by: Redd on December 31, 2008, 09:12:14 PM
I wouldn't really call these "hacks", they're more like method add-ons.

But I like what I'm seeing here, the 12to24$ one is giving me crazy ideas. XD
Title: Re: SC hacks
Post by: GMG Hendo on May 06, 2009, 08:01:28 AM
Yeah these aren't hacks lol. Basically like a library for SC. Still cool though.
Title: Re: SC hacks
Post by: Teruri on May 06, 2009, 10:45:05 AM
Hacks just sounds more awesome.
Title: Re: SC hacks
Post by: WarHampster on May 06, 2009, 11:46:00 AM
hay guis i taked computer science 3 ima 1337 haxor lol
Title: Re: SC hacks
Post by: Telstar5 on May 06, 2009, 12:47:32 PM
I think SC uses RBScript to carry out all the scripting stuff.

Theoretically, you could put in any old RBScript commands and watch it do something.
Title: Re: SC hacks
Post by: Gan on May 06, 2009, 01:39:52 PM
Ooh. I'll try it out.


-Gandolf
Title: Re: SC hacks
Post by: Gan on May 06, 2009, 01:42:48 PM
Tried making a variable, didn't work.


-Gandolf
Title: Re: SC hacks
Post by: GMG Mike on June 10, 2009, 09:30:09 PM
Quote
I think SC uses RBScript to carry out all the scripting stuff.

Theoretically, you could put in any old RBScript commands and watch it do something.

No. SC has a fully custom written scripting system. It took several hundred hours of time back in the day.