Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Scripting for dummies?  (Read 2431 times)
0 Members and 1 Guest are viewing this topic.
Offline CyanPrime
« Posted 2011-07-11 17:58:33 »

Alright, so I wanna make a RPG, but I need to use scripting to make events and triggers and stuff. I know I'm using LuaJ, but thats it, so how do I go about this? Even the logic fails me. Can someone point me in the right direction?

Please help
Offline Z-Man
« Reply #1 - Posted 2011-07-11 19:10:34 »

I honestly know nothing about LuaJ or making RPGs, but here is a link to a LuaJ tutorial I found: Getting Started with LuaJ I am curious though as to why your using scripting. Are you going to use it alongside Java? In which case why not just use Java? Again I know nothing of making RPGs so you probably know something I don't.

EDIT: It just occurred to me that you know LuaJ and just need help getting it to do RPG stuff, in which case sorry I misread your post.
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #2 - Posted 2011-07-11 21:03:38 »

I honestly know nothing about LuaJ or making RPGs, but here is a link to a LuaJ tutorial I found: Getting Started with LuaJ I am curious though as to why your using scripting. Are you going to use it alongside Java? In which case why not just use Java? Again I know nothing of making RPGs so you probably know something I don't

Many times there are a few key game programmers who write the engine and stuff  and non programmers tend to find scripts and lua far more approachable because they tend to be less finicky about compiling and other things. Just like using XML sheets for items/names/etc...
Allows people to add content/stuff with minimal effort and often times simply editing a txt document with notepad or simple text editor.


"Experience is what you get when you did not get what you wanted"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Man
« Reply #3 - Posted 2011-07-11 21:11:35 »

Many times there are a few key game programmers who write the engine and stuff  and non programmers tend to find scripts and lua far more approachable because they tend to be less finicky about compiling and other things. Just like using XML sheets for items/names/etc...
Allows people to add content/stuff with minimal effort and often times simply editing a txt document with notepad or simple text editor.
Oh. Didn't know that, it makes sense though. Thanks for the explanation.
Offline fletchergames

Senior Member





« Reply #4 - Posted 2011-07-14 17:27:07 »

I used to use something similar that let you use a Java-like scripting language within Java.  I thought it was way too complex.

Now, I'm using my own scripting language that looks vaguely like HTML.  It's not as powerful as XML, but it's a little easier to work with because it's designed to only include the sorts of stuff I need.  It's very limited at this point, but I'm confident that I can expand it to be what I need.

Java includes some kind of scripting support, but I haven't investigated it.  Instead, I read the book "Writing Compilers and Interpreters 2nd Edition" by Ronald Mak.  There is a 3rd edition available that uses Java instead of C and is available as a Kindle ebook.  The 2nd edition was sufficient, though I will most likely buy the 3rd edition before the next time I move.

In any case, my point is that people often program their own very limited scripting languages.  I think this produces a superior map editor because I no longer have to mess around with a bunch of features that I don't need and have everything integrated in a sensible way.  It does require some time and effort, and the custom scripting language doesn't have many of the features included in a general purpose scripting language.  I don't want to use my map editor's scripts to sort arrays or do math - only to execute a simple set of commands for displaying dialogue, opening doors, etc.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2011-07-14 21:20:04 »

You can just use Java, it's what I've done it a works great. Add a place in your level editor to type custom code, then do a bit of meta-language in order to allow access to elements in your level structure, and you're good to go.

Here is my event class (warning, I wrote this like 6 years ago) which does all the related work. It's pretty simple. The main point of interest is run(), which executes the code found in the level. Basically you would be able to visually give each entity a name (like "dog" or "hero" or whatever) and they get added into a HashMap with the name as the key. In the level editor, you type in the code @dog.doSomething() and then the makeUsable() function below translates @dog into hashMap.get(dogID) and it gets type casted to the correct thing. It's pretty simple. If you need more custom stuff, you can use different special characters and/or overload the same one to do everything you need.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
/* The Event class is used in the LevelBuilder to handle certain special
 * triggers and conditions that can happen in any level. Conditions include
 * walking into a certain place, killing a certain monster, talking to a
 * certain sentient, or moving blocks into certain places. Results include
 * adding/removing any Collidables, moving Collidables, changing talk strings,
 * opening/closing doors, giving/removes items from the player, giving any stat(s)
 * to the player, changing AI types for sentients or monsters...
*/

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Event
{  
   //Java's method sucks, so I made my own
  public static String trim(String str)
   {
      int begin = 0; int end = str.length()-1;
      while (begin < str.length() && Character.isWhitespace(str.charAt(begin)))
         begin++;
      while (end >= begin && Character.isWhitespace(str.charAt(end)))
         end--;
      return str.substring(begin,end+1);
   }
   
   public static int compile(String s, IDList ids, String name) throws IOException
   {
           // Create a temp. file
          //File file = File.createTempFile());
          File file = new File(System.getProperty("user.dir") + "/levels/Script/",name + ".java");
           
           // Get the file name and extract a class name from it
          String filename = file.getName();
           String classname = filename.substring(0, filename.length()-5);
              s = makeUseable(s,ids);
           
           // Output the source
          PrintWriter out = new PrintWriter(new FileOutputStream(file));
           out.println("public class " + classname + "\n{");
           out.println("    public static void main(IDList ids)\n    {");
           out.print("        "); out.println(s);
           out.println("    }");
           out.println("}");

           // Flush and close the stream
          out.flush(); out.close();

           // Compile
          filename = System.getProperty("user.dir") + "/levels/Script/" + filename;
           String[] args = new String[] {"-d", System.getProperty("user.dir") + "/levels/Script/", filename};
           int status = com.sun.tools.javac.Main.compile(args);
           file.delete();
           return status;
   }
   
   public static void run(Level level, IDList ids)
   {
      try
      {
         // Try to access the class and run its main method

         Class clazz = Class.forName(level.name());
         Method main = clazz.getMethod("main", new Class[] { IDList.class });
         main.invoke(null, new Object[] { ids });
      } catch (InvocationTargetException ex)
      {
         // Exception in the main method that we just tried to run
        if (level != null && level.panel() != null)
            level.panel().setDebugText("Exception in main: " + ex.getTargetException(),15);
      } catch (Exception ex)
      {
         if (level != null && level.panel() != null)
            level.panel().setDebugText(ex.toString(),15);
      }
   }
   
   private static String makeUseable(String s, IDList ids)
   {
      StringBuffer buffer = new StringBuffer(s);
      for (int i = 0; i < buffer.length(); i++)
      {
         if (buffer.charAt(i) == '@')
         {
            int j = i+1; String word = "";
            while (Character.isJavaIdentifierPart(buffer.charAt(j)))
            {
               word += buffer.charAt(j);
               j++;
            }
            int index = ids.getIndex(word);
            if (index >= 0 || index == Level.PLAYER_INDEX)
            {
               Collidable c = ids.get(word);
               String replacement = ")ids.get(" + index + "))";
               if (c instanceof Sentient)
                  replacement = "((Sentient" + replacement;
               else if (c instanceof BackgroundItem)
                  replacement = "((BackgroundItem" + replacement;
               else if (c instanceof Block)
                  replacement = "((Block" + replacement;
               else if (c instanceof Chest)
                  replacement = "((Chest" + replacement;
               else if (c instanceof Door)
                  replacement = "((Door" + replacement;
               else if (c instanceof Potion)
                  replacement = "((Potion" + replacement;
               else if (c instanceof Wall)
                  replacement = "((Wall" + replacement;
               else if (c instanceof Weapon)
                  replacement = "((Weapon" + replacement;
               else if (c instanceof Armor)
                  replacement = "((Armor" + replacement;
               else if (c instanceof Collidable)
                  replacement = "((Collidable" + replacement;
               buffer.replace(i,j,replacement);
            }
         }
      }
      return buffer.toString();
   }
}

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (28 views)
2014-09-12 09:08:26

Norakomi (57 views)
2014-09-10 13:57:51

TehJavaDev (74 views)
2014-09-10 06:39:09

Tekkerue (37 views)
2014-09-09 02:24:56

mitcheeb (57 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (28 views)
2014-09-07 01:12:14

Longarmx (34 views)
2014-09-07 01:11:22

Longarmx (34 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!