------------------------------------------------------------- KSNiloc's Guide To Classes In RPGCode For those of us who like things classy ------------------------------------------------------------- ---Opening--- One thing that has always bothered me about RPGCode is its lack of class. Or more specifically- classes. CBM had it in his head that RPGCode was going to be a simplistic language and it sure ended up that way. But because of implicit calling of methods, it *is* possible to acomplish very simple classes. Read on into the mystery... ---Basic Classes--- In my mind an object has to meet certain criteria in order to actually be rewarded with the title of "class". 1. It has to have properties, methods, and events 2. You have to be able to create instances of it 3. You have to be able to destroy them 4. You have to be able to "work with" a class ---Parts of a Class--- You may not be familiar with all of the terms I just used. Let me explain some of the lesser known ones: Property: A property of a class, also known as a member variable, is a variable that *belongs* to that class. Each instance of the class has its own copy of the variable. They are usually accessed like: Class.Var = "Blah". Method: A method is something a class can do. If you had a battlesystem class one of its methods might be to battle. You would access it like this: Battlesystem.Battle(parameters). Event: Say in your battlesystem there is an event called OnPlayerWin. When the player wins the code associated with this event would be executed. Work With: Normally when you want to access a class you would go Class.Whatever but if you are accessing the same class a lot, typing all that Class. gets a tad annoying. When you work with a class you don't need to specify which class you're working with each line. ---Relation to RPGCode--- Now there acutally are ways to pull off all of this stuff in RPGCode. It may not be terriby easy, but it *is* possible. In order to get anywhere, however, you have to know how to use implicit calling of RPGCode methods. Normally when you want to call a method, you would do this: ---Program1.prg #Include("Program2.prg") #DoMet ---Program2.prg #Method DoMet() { #MWin("We go here!") #system.pause #MWinCls() } However, program1.prg could be rewritten to look like this: ---Program1.prg #Program2.DoMet That acomplishes exactly the same thing but is neater. NOTE: A little known side effect of implicit calling of methods is that from then on you would not have to use the "Program2.". You could continue to go #DoMet and it would work fine. With that said, you know how to pull off a class' method. Unfortunently, that is *all* we have to work with. Here is how we would do a property: ---Battlesystem.prg #Method Enemy(num!,text$) { #battlesystem_enemy[num!]$=text$ } To set the battle's enemies, you would do this: #BattleSystem.Enemy(1,"Blob") #BattleSystem.Enemy(2,"Blob2") In order to do an event, you have to understand another concept: ---Program1.prg #Program2.Test #Method Test2() { #Mwin("2") #system.pause #MWinCls() } ---Program2.prg #Method Test() { #Mwin("1") #Test2 } If you were to run Program1.prg, the message window would look like this: 1 2 [waits for a key] However, if you ran Program2.prg the program would close. This is because once *anything* from a program has been run, all its methods all avaliable for calling without includes or implicit calling. With that said, take a peak at this snippet: ---HardBoss.prg #MWin("You'll never beat me!") #system.pause #MWinCls() #BattleSystem.Enemy(1,"Hard Boss") #BattleSystem.Enemy(2,"Sidekick") #BattleSystem.Battle #Method OnBattleWin() { #MWin("GAHHHHHHHHH!") #system.pause #MWinCls() } #Method OnBattleLose() { #MWin("What did I tell you?") #system.pause #MWinCls() } ---BattleSystem.prg *Properties... #Method Enemy(num!,text$) { #battlesystem_enemy[num!]$=text$ } *Methods... #Method Battle() { *The battlesystem would go here* *The battle is over now* #if(win!=1)* The player won { #OnBattleWin() } #if(win!=0)* The player lost { #OnBattleLose() } } *Events... *NOTE: It's probably a good idea to keep a list of the * the class' events so you don't have to search * through the code for them. *OnBattleWin -Called if the player wins *OnBattleLose -Called if the player loses Depending on whether the player wins the battle or not, a different event is run. Though in this case it may not be practical, you get the idea. As for working with a class... I think the best way to accomplish this is through the Include command we all hate so much. Now, to prevent your code from becoming unreadable you're going to want to indent like so: ---HardBoss.prg #MWin("You'll never beat me!") #system.pause #MWinCls() #Include("battlesystem.prg") #Enemy(1,"Hard Boss") #Enemy(2,"Sidekick") #Battle Now you can easily see what belongs to battlesystem.prg. ---Instances of a Class--- There is no "good" way to do this through RPGCode but it is possible. Let's say we wanted to make the battlesystem class instanceable... ---Battlesystem.prg *Properties... #Method Enemy(inst!,num!,text$) { #CastLit(num!,num2$) #CastLit(inst!,inst2$) #temp$="#battlesystem_"+inst2$+"_enemy["+num2$+"]$="+text$ #RPGCode(temp$) #Kill(temp$) #Kill(num2$) #Kill(inst2$) } *Methods... #Method Battle(inst!) { *NOTE: It would be a little bit trickier to obtain the * enemy data now, but still possible through the * #RPGCode command as I've done above. *The battlesystem would go here* *The battle is over now* #if(win!=1)* The player won { #OnBattleWin(inst!) } #if(win!=0)* The player lost { #OnBattleLose(inst!) } } *Events... *OnBattleWin -Called if the player wins *OnBattleLose -Called if the player loses Let's redo the hard boss battle now: ---HardBoss.prg #MWin("You'll never beat me!") #system.pause #MWin("Sidekicks, get him!") #system.pause #MWinCls() #Include("battlesystem.prg") #Enemy(1,1,"Sidekick") #Enemy(1,2,"Sidekick") #Enemy(2,1,"Hard Boss") #Enemy(2,2,"Sidekick") #Battle(1) #Battle(2) #Run("BeatHardBoss.prg") #Method OnBattleWin(inst!) { #if(inst!=1) { #MWin("Now you're toast!") #system.pause #MWinCls() } #if(inst!=2) { #MWin("Ho... how?") #system.pause #MWinCls() } } #Method OnBattleLose(inst!) { #if(inst!=1) { #MWin("Game Over...") #system.pause #MWinCls() #Windows() } #if(inst!=2) { #Mwin("Ha! I didn't expect you to win!") #system.pause #MWinCls() #Run("LostToHardBoss.prg")* Alternate story sequence } } It would also be fairly simple to "destroy" an instance of the class: ---BattleSystem.prg *... #Method Destroy(inst!) { #CastLit(inst!,inst2$) #for(a!=1;a!<=9;a!=a!+1) { #CastLit(a!,a2$) #temp$="battlesystem_"+inst2$+"_enemy["+a2$+"]$" #temp$="#Kill("+temp$+")" #RPGCode(temp$) #Kill(a2$) #Kill(temp$) } #Kill(inst2$) } *... So we would modify the boss battle like this: ---BossBattle.prg *... #Include("battlesystem.prg") #Enemy(1,1,"Sidekick") #Enemy(1,2,"Sidekick") #Enemy(2,1,"Hard Boss") #Enemy(2,2,"Sidekick") #Battle(1) #Battle(2) #Destroy(1) #Destroy(2) *... Using classes in your code makes it more readable and organized. Use them wherever you can! ---Closing--- There you have it, folks- my class guide. I covered everything that I said I would. Did you find this guide helpful? I welcome your opinion. --KSNiloc ksniloc@loggerdelta.tk