• Advertisement
Sign in to follow this  

Unity What I want in a language

This topic is 4162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I've been working on a 2D tile-based RPG engine that uses my own scripting language but I've decided that I want to abandon this clumsy, informal language of mine and utilize a 3rd party embeddable scripting language. I've looked at a few possibilities (Lua, in particular) but it's not clear to me how or if these languages are really meant for what I want to do. Here's how my engine has worked so far: - C++ provides a library of general functions that control the game environment: creating characters, moving characters, and other fundamental arbitrary actions. - C++ creates multiple instances of what I've called ScriptHandlers. These objects have an associated script file. - Every frame, C++ loops through the ScriptHandlers. Each SH takes this opportunity to run part or all of its script. These scripts will call C++ functions to supply game-specific activity (creating a particular character, moving a character, displaying some dialogue, etc). There may be logic if/then) and looping involved. At some point, the script may need to halt itself. The program flow returns to the SH; the scripting language does not just call another C++ function! - The next time a particular SH has its turn to run its script, it needs to restart wherever it left off. There are basically three levels of "game control" going on here: Graphics, user input, collision detection, etc (C++) -> Game logic (script) -> -> Actual creation/manipulation of characters, dialogue, etc (C++) The big issue here is that the scripts need to be able to halt their own execution (and control returns to C++), but save their place so that C++ can restart the script where it was left off. Perhaps a virtual machine that can be paused and resumed. I know I've made a few threads on this subject already, but I've found few resources on these languages, unfortunately, so it's difficult to make an educated guess at what's appropriate for me. I appreciate any advice what-so-ever. Thanks!!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Quote:

The big issue here is that the scripts need to be able to halt their own execution (and control returns to C++), but save their place so that C++ can restart the script where it was left off. Perhaps a virtual machine that can be paused and resumed.
You want continuations, it seems, to implement cooperative multitasking.

Or (if they just need to be halted and resumed once) generators.

Any language which provides closures or explicit generator constructs can provide this functionality; python's generators can be very conveniently used for this. So you might embed a python interpreter in C++ (or call your C++ from python, maybe a saner choice) and write stuff like:


def a_script():
do_something1()
do_more()
#return control while saving state
yield
#execution resumes at this point when the generator object's .next() method is invoked
do_something2()
#return control after doing somethng2
yield
#execution resumes here
....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you might also want to check out AngelScript, which has its own forum here at GameDev.net

Share this post


Link to post
Share on other sites
I have looked at Angelscript and it's possible that it's just what I want. There's very little in the way of tutorials and such, though. I'll keep poking at it, though. The same goes for GameMonkey, which seems similar in functionality to Angelscript (as far as I've looked into it).

I don't know anything about continuations or generators, but the example you gave is very close to what I want. Is it usually possible for a script to yield within a logical or looping construct and resume the same state it left afterward? What are some languages that have this feature?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:

I don't know anything about continuations or generators, but the example you gave is very close to what I want. Is it usually possible for a script to yield within a logical or looping construct and resume the same state it left afterward?
Of course, that's the whole point. The generator construct takes care of remembering the state so you don't have to.

Let's make two generators, one that makes even numbers and one that makes odd numbers. Notice they use a while loop:


def generate_odds ():
current = 1
yield 1
while True:
current += 2
yield current

def generate_evens():
current = 0
yield 0
while True:
current += 2
yield current



Let's look at an example interaction:


>>> evens = generate_evens()
>>> odds = generate_odds()
>>> evens
<generator object at 0x00B3A1C0>
>>> evens.next()
0
>>> evens.next()
2
>>> odds.next()
1
>>> odds.next()
3
>>> odds.next()
5
>>>



Of course, generators are objects, so we can pass partially-executed scripts around and combine them in different ways. let's make a generator which interleaves the results of two other generators:


def interleave_generators(gen1, gen2):
state = 0
while True:
if state == 0:
state = 1
yield gen1.next()
else:
state = 0
yield gen2.next()



We can call this with two brand new even and odd generators, or with the half-executed generators 'evens' and 'odds' we defined previously (it'll start interleaving them from where we left off, that is, 4 in the evens generator and 7 in the odds one):


>>> all_numbers = interleave_generators(generate_evens(), generate_odds())
>>> all_numbers.next()
0
>>> all_numbers.next()
1
>>> all_numbers.next()
2
>>> previously_defined = interleave_generators(evens, odds)
>>> previously_defined.next()
4
>>> previously_defined.next()
7
...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By khawk
      Watch the latest from Unity.
       
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
  • Advertisement