Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Change a variable in another class based on a variable string.  (Read 505 times)
0 Members and 1 Guest are viewing this topic.
Offline Bearded Cow

Senior Devvie


Medals: 2
Projects: 1
Exp: 1 year


¬..¬


« Posted 2013-08-06 12:03:23 »

I hope the subject made sense, but essentially what I want to be able to do is have a string that I can change which refers to a variable in another class which I can then change.

Something like this.

1  
2  
3  
4  
5  
String variable = "speed";

public void method(Object ob){
    ob.getVariable(variable) = 3;
}


This of course is not how it would look but I hope it helps in understanding what it does.
Offline Jeremy
« Reply #1 - Posted 2013-08-06 12:07:14 »

That is definitely do-able via reflection. http://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html

Honestly though, reflection is generally a bad idea - not always. Why do you need it? If it is to expose an interface to scripts I would consider using ScriptEngine.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Bearded Cow

Senior Devvie


Medals: 2
Projects: 1
Exp: 1 year


¬..¬


« Reply #2 - Posted 2013-08-06 12:13:57 »

Well it is more of just the sort of thing that if I could do it, it would make me happy rather than actually be very effective.

I am making a game where there are loads of items which will affect the player in different ways and I just wanted to have it so that I could just have the two strings, the variable to change and the value to change it to.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeremy
« Reply #3 - Posted 2013-08-06 12:27:30 »

Well it is more of just the sort of thing that if I could do it, it would make me happy rather than actually be very effective.

I am making a game where there are loads of items which will affect the player in different ways and I just wanted to have it so that I could just have the two strings, the variable to change and the value to change it to.

Actually, there are two routes you can take that I think you'd like over that one. If you think they're too much work and the project isn't serious than your can probably skip them and just use raw reflection.

The first (easier to do, but poorer design IMO) is to use ScriptEngine, you can expose your entire player object to the script and let it do whatever it wants to your character object. ScriptEngine will let you use tons of different scripting language implementations - but it makes most sense to use javascript. You can than write something like (in your item's script file):

1  
2  
3  
4  
5  
6  
7  
function onConsumed(player)
{
    if(player.health <= 5)
        player.speed-=5;

    game.say("omnomnom speed");
}


You could expose other variables to the script (like game) which might have a function called 'say' to expose a message on the screen.

The more complex but proper solution would be to expose a inner class that accurately represents your character object providing access to members (like speed) which should be protected so that you can limit access to those members to just the script. - I thoroughly implemented scripting into my engine and it is relatively easy to do.

http://content.gpwiki.org/index.php/Java:Tutorials:Scripting
http://docs.oracle.com/javase/6/docs/api/javax/script/ScriptEngine.html

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Danny02
« Reply #4 - Posted 2013-08-06 12:52:36 »

Java is a strongly statically typed language, so it is no good practise to do something like this.

Of course you can use HashMaps all over the place, like:
1  
2  
3  
4  
Map playerSkills = new HashMap<String, Integer>();

...
playerSkille.set("fireBall", 3);

Or pass only Object.class objects around with a lot of instanceOf , but than what is the point to use Java. If you like to code like this perhaps give javascript(browser games) or phyton a try.

When you code in Java you always have to think about types. So you have an item which can be applied to what?
Only the player or also humans, monsters, a table??

When you have this figured out, you normally build up an inheritance tree.

Thing: has a position
Table is a Thing
LivingThing is a Thing, but also has healt
Monster is a LivingThing, has loot
Zombie is a Monster
Human is a LivingThing, has a name
Player is a Human, has items

And in the and you do something like this

1  
2  
3  
4  
class HealthPotion extends Item
{
  void applyTo(LivingThing someon);
}
Offline Jeremy
« Reply #5 - Posted 2013-08-06 13:00:32 »

Java is a strongly statically typed language, so it is no good practise to do something like this.

Of course you can use HashMaps all over the place, like:
1  
2  
3  
4  
Map playerSkills = new HashMap<String, Integer>();

...
playerSkille.set("fireBall", 3);

Or pass only Object.class objects around with a lot of instanceOf , but than what is the point to use Java. If you like to code like this perhaps give javascript(browser games) or phyton a try.

When you code in Java you always have to think about types. So you have an item which can be applied to what?
Only the player or also humans, monsters, a table??

When you have this figured out, you normally build up an inheritance tree.

Thing: has a position
Table is a Thing
LivingThing is a Thing, but also has healt
Monster is a LivingThing, has loot
Zombie is a Monster
Human is a LivingThing, has a name
Player is a Human, has items

And in the and you do something like this

1  
2  
3  
4  
class HealthPotion extends Item
{
  void applyTo(LivingThing someon);
}


If you're going for a casual game, that's a good idea. For most casual games that would work fine.

However, you can't have concrete classes like HealthPotion if you are developing a flexible game engine as it would require someone to write and compile java code to the library just to add an item like potion, poison (insert any other consumable here.)

Having a single class, Item, and then an enumeration of the general type of item it is (weapon, consumable, armor, general) you can act on the item appropriately and invoke script routines appropriately when it is equip, or consumed or attacked with.

JevaEngine, Latest Playthrough (This demo is networked with a centralized server model)

http://www.youtube.com/watch?v=rWA8bajpVXg
Offline Danny02
« Reply #6 - Posted 2013-08-06 13:22:31 »

OP is clearly a beginner and my response was directed at his capabilities.
I think it is imported to just give him a general idea and not confuse him with implementations details as you are doing.

As mentioned countless times on this board, do NOT over complicate what you want to achieve.

Your suggestion to use scripts or other ways for configuration does not cover OPs problem very well. Using a scripting in a game-framework is a whole other topic.

PS: I my humble opinion a scripting has no really a place in a Java game. Before you implement your hot-reload, script loading and connection framework, I would already have written 100s of different Item classes. Second point: no need to use scripting to have an extendable framework. Third point: if you are concerned about turn-around times while development(change-compile-run) use something industrie-tested like JRebel.
Offline actual

JGO Coder


Medals: 24



« Reply #7 - Posted 2013-08-06 17:13:49 »

Another possibility is to use the Properties Pattern.

A long but interesting article explaining the Properties pattern as implemented in an RPG. This article talks about a Java implementation.

This approach would allow you to stick with plain Java code while skipping the overhead of reflection and gaining the additional flexibility.  You will still lose the nice features of static typing and compiler checking, however.
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.

Elsealabs (15 views)
2014-12-28 10:39:27

CopyableCougar4 (19 views)
2014-12-28 02:10:29

BurntPizza (24 views)
2014-12-27 22:38:51

Mr.CodeIt (14 views)
2014-12-27 04:03:04

TheDudeFromCI (19 views)
2014-12-27 02:14:49

Mr.CodeIt (26 views)
2014-12-23 03:34:11

rwatson462 (58 views)
2014-12-15 09:26:44

Mr.CodeIt (47 views)
2014-12-14 19:50:38

BurntPizza (96 views)
2014-12-09 22:41:13

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!