3

It has been my late-childhood dream to create a game, and now as I actually know how I thought I should fulfill my dream and started working on a little game project in my spare time. It's basically a combat type of game where you have up to 3 units, as well as your opponent, and you take turns ( since it's http, you know that feel ) to attack each other and cast spells and stuff. The issue I came across with is with abilities and how to store them. Basically if I were to store abilities in an array it would look something like

$abilities = array(
    0 => array(
        'name' => 'Fire ball',
        'desc' => 'Hurls a fire ball at your enemy, dealing X damage.'
        'effect' => function($data){
            $data['caster']->damage($data['target'], $data['caster']->magicPower);
        }
    ),
    1 => array(...
);

But if I were to store abilities this way, every time I needed to fetch information about a single ability I would need to load the whole array and it's probably going to get pretty big in time, so that would be a tremendous waste of memory. So I jumped to my other option, to save the abilities in a mysql table, however I'm having issues with the effect part. How can I save a function into a mysql field and be able to run it on demand?

Or if you can suggest another way to save the abilities, I may have missed.

4
  • Is this a mobile game? Commented Apr 15, 2014 at 7:07
  • @kimbarcelona what difference would that make :/ Commented Apr 15, 2014 at 7:08
  • We have the same dream. :) I'm interested for your soon to be game Commented Apr 15, 2014 at 7:10
  • @kimbarcelona oh well, 'soon' is a pretty relative term :D as the majority of the logic is going to be handled by the server I guess mobile or desktop is just a layout change Commented Apr 15, 2014 at 7:17

2 Answers 2

3

To answer your question related to storing arrays into database like MySQL I would like you to serialize the Array as String. The normal direct serialization not going to work because they don't deal with closure.

You need to use classes like super_closure which can serialize the methods and convert them into string. Read more here

https://github.com/jeremeamia/super_closure

$helloWorld = new SerializableClosure(function($data){
            $data['caster']->damage($data['target'], $data['caster']->magicPower);
        });
$serializedFunc = serialize($helloWorld);

Now you can create array like this:

$abilities = array(
    0 => array(
        'name' => 'Fire ball',
        'desc' => 'Hurls a fire ball at your enemy, dealing X damage.'
        'effect' => $serializedFunc
    ));

This Array can now be saved directly, serialized or encoded to JSON.

I would recommend you to look at Redis or Memcache for caching query results and don't use MySQL to store functions.

Sign up to request clarification or add additional context in comments.

Comments

2

You could have tree tables

spell

  • id
  • name
  • description

spell_effect

  • id
  • name
  • serversidescript

spell_effect_binder

  • spell_id
  • spell_effect_id

This would make sure, that your logic is in php files, where ever you would like them to be located, but all the meta of the spells, effects and how they bind together in the database. Meaning you will only load the function/script of the ones in need. Plus, giving you the possibility to append multiple effects to one spell.

//Firedamage.php
public function calculateEffects($level,$caster,$target) {

    $extraDamage = 5*$level;
    $randDamage = rand(10,50);

    $caster->damage( $target, ($randDamage+$extraDamage) );
}

Spell_effect entry

  • id = 1
  • name = 'firedamage'
  • serversidescript = 'Firedamage.php'

spell

  • id = 1
  • name = 'Fireball'
  • description = 'Hurls a fireball at your foe'

spell_effect_binder

  • spell_id = 1
  • spell_effect_id = 1

5 Comments

That's an awesome idea. To store different functions in different files and load the file on demand. Thank you!!!
However I'm going to accept the other answer, as it answers the actual question, but I'm going with this suggestion in the project, thanks again!
Your welcome. Yet, i do not understand your argument of "answeing the actual question" since you asked: "Or if you can suggest another way to save the abilities, I may have missed." -I do consider this another way to save the abilities. But your pick. Hope it helped :)
Yeah I know but if someone is looking for the solution for the same problem in the future and they stumble onto this question and see the accepted answer is something else they may miss it. Personally when I look for something and I see the accepted answer isn't what I'm looking for I often times don't look at the other answers. I upvoted some of your other answers in compensation, so I hope it's ok, I don't mean to be rude or disrespectful.
I fully understand and accept both your decision and argument :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.