Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - froggy77

Pages: 1 ... 5 6 [7] 8 9 10
91
Development / HUD: ZL - level (LVL) & experience (XP)
« on: January 28, 2016, 06:54:26 pm »
HUD - ZL - level (LVL) & experience (XP)



I try to create a game system with points of experience and levels for my Zelda-like project.
Starlock sent me an email: "I was also planning on making an xp system and was hoping I could see your code and fiddle around with it :P"
So I share my work if this could be useful for others...

____________________

SCRIPT

Here is the main script for the HUD. See in attachment.
Put this script in '../data/scripts/hud/lvl_and_xp.lua'

In this script there is a table:
self.t_xp_to_levelup = {100, 200, 300, 400, 500, 600, 700, 800}

You are level 1 and you need 100 xp  to level up.
You are level 2 and you need 200 xp  to level up.
You are level 3 and you need 300 xp  to level up.
...
You are level 8 and you need 800 xp  to level up.
Then level 9 appears and xp counters are blocked (800/800). They appears in green because I used 'small_green_digits' font that I think you have got.
Put  your own values. Obviously, it will not be 100, 200, 300 etc.. ;)
You are not limited to 8 values.

____________________

IMPORTANT

You will have to add some lines in certain lua files. See below.
I hope I don't forget something.  ;D

____________________

IMAGE

Copy this image. See in attachment.

../data/sprites/hud/lvl_and_xp_icon.png

_________________

POSITION IN THE HUD

In '../data/scripts/hud.lua' , you need declare the position of lvl and xp elements.
(-104, 16) is an example.


  ---- Create each element of the HUD.
  -- blablabla

Code: [Select]
  local lvl_and_xp_builder = require("scripts/hud/lvl_and_xp")
-- blablabla

Code: [Select]
  menu = lvl_and_xp_builder:new(game)
  menu:set_dst_position(-104, 16)
  hud.elements[#hud.elements + 1] = menu

-- blablabla

____________________

SAVEGAME FILE

In "../data/scripts/game_manager.lua", and more precisely in the "function game_manager:create(file)", we have to declare two values for a new savegame file. Also add 'current_level = 0'  and 'current_xp = 0' in your existing savegame files.

--  if not exists then
    ---- This is a new savegame file.
   -- blablabla
Code: [Select]
game:set_value("current_level", 0)
game:set_value("current_xp", 0)
--  end
-- blablabla

____________________

FUNCTIONS TO ADD, SET or GET XP and LEVEL

In '../data/scripts/quest_manager.lua', because I think it is practical to add or set or  get xp and level of our hero, I added some functions.
All these functions are not necessaries, but they are very useful in debug console to test.
hero:set_level(3) is equal to game:set_value("current_level", 3)
hero:add_level(1) so  'current_level = 4' in your 'saveX.dat' if you save the game.
hero:get_level() is equal to game:get_value("current_level")
hero:set_xp(100)    is equal to  game:set_value("current_xp", 100)
hero:add_xp(10)   so  'current_xp = 110' in your 'saveX.dat' if you save the game. hero:add_xp(number) is the most useful for enemies.
hero:get_xp()  is equal to  game:get_value("current_xp")


Code: (lua) [Select]
-- Initializes some behavior of our hero.
local function initialize_hero()

  local hero_meta = sol.main.get_metatable("hero")
 
  -- ZL --  Functions for levels of our hero
  function hero_meta:set_level(lvl)

    if type(lvl) == "number" then
local game = self:get_game()
game:set_value("current_level", lvl)
else
        print("Error in hero:set_level(lvl), lvl should be a number.")
    end
  end
  function hero_meta:add_level(lvl)

    if type(lvl) == "number" then
local game = self:get_game()
local current_level = game:get_value("current_level")
game:set_value("current_level", current_level + lvl)
else
        print("Error in hero:add_level(lvl), lvl should be a number.")
end
  end
  function hero_meta:get_level()

local game = self:get_game()
local current_level = game:get_value("current_level")
return current_level
  end
  -- ZL --  Functions for experience points of our hero
  function hero_meta:set_xp(xp)

    if type(xp) == "number" then
local game = self:get_game()
game:set_value("current_xp", xp)
else
        print("Error in hero:set_xp(xp), xp should be a number.")
    end
  end
  function hero_meta:add_xp(xp)

    if type(xp) == "number" then
local game = self:get_game()
local current_xp = game:get_value("current_xp")
game:set_value("current_xp", current_xp + xp)
else
        print("Error in hero:add_xp(xp), xp should be a number.")
end
  end
  function hero_meta:get_xp()

local game = self:get_game()
local current_xp = game:get_value("current_xp")
return current_xp
  end

end


Edit: Sorry, don't forget to  initialize_hero() !!!
Code: (Lua) [Select]
function quest_manager:initialize_quest()
 
  -- blablabla
  initialize_hero()
  -- blablabla
 
end

____________________

ENEMIES

In your scripts for enemies where you have declared their behavior  (e.g  ../data/enemies/lib/slime_behavior.lua),
just add a variable named for example "properties.xp" in your 'function behavior:create(enemy, properties)'. I don't know, but the name of your function is perhaps different.

In my example, I decided to define xp according to life, damage and speed of the enemy.
Code: [Select]
properties.xp = properties.xp or (properties.life * properties.damage * properties.normal_speed)
In this example, I added xp in the 'function enemy:on_dying()', I think it is logical, but you can do differently:

  function enemy:on_dying()
 
Code: [Select]
local hero = enemy:get_map():get_entity("hero")
hero:add_xp(properties.xp)
  end

92
Development / Re: Is hero:on_taking_damage() broken ?
« on: January 24, 2016, 02:06:17 am »
There is no problem with your script.
I added this line "hero_mode = true" in save1.dat and I added your script in quest_manager.lua.
After "game:remove_life(damage)", I even added this line to test:
Code: [Select]
print ("hero_mode = ", game:get_value("hero_mode"), "so damage =", damage, "and get life = ", game:get_life())In debug mode, I typed
Code: [Select]
game:set_value("hero_mode", false)and damage returns to normal, so it works.

93
Development / Re: Save Game
« on: January 17, 2016, 08:15:09 pm »
@DevilStrawsz:
  • What is your operating system?
     
  • Where comes from your 'data' directory?   I guess it comes from an english tutorial but which one?
     
  • Original version of Solarus : ?  In your first post, you wrote "This quest is made for Solarus 0.9 but you are running Solarus 1.4.5", so I guess it was a 0.9.
     
  • I tested your project and I also have this message loading your project: 'Error: Error in string data file: [string "text/strings.dat"]:10: attempt to call global 'dialog' (a nil value)'.
    I think you made something wrong converting your project. When I open the 'data/languages/en/text/strings.dat' file of your project, the content is not correct, because according to me, it should not contain "Dialogs show when the hero obtains a treasure". I deleted the content of this file and I had no more error in the console.
  • There is also a problem with the game_manager:create("save1.dat") but I don't know why :(
    There is no error in the console.
     
  • I said you to rename your directory named '$HOME/.solarus/tuto_quest_en' into '$HOME/.solarus/tuto_quest_en_old'
    (or  for Windows, '%UserProfile%\.solarus\tuto_quest_en' into '%UserProfile%\.solarus\tuto_quest_en_old' ),
    not  'write_dir = "tuto_quest_en" into 'write_dir = "tuto_quest_en_old".
    OK now it does not matter, when we started your quest, a 'tuto_quest_en_old' is created in our  '$HOME/.solarus/.
     
  • I think you have a useless subdirectory named 'data' in your directory named 'data'. In other words, another project without maps in your project. Delete this directory will not solve your problem, but it will be cleaner.
     

94
Development / Re: Save Game
« on: January 16, 2016, 08:04:50 pm »
I think you wrote the solution
(...)
The Solarus Write Directory is already set.
(...)
  -- Directory where your quest will write its savegames and setting files.
  -- It will be a subdirectory of '$HOME/.solarus/', automatically created by
  -- the engine. Its name should identify your quest, to avoid confusion with
  -- other Solarus quests that might also be installed on the user's machine.
  -- You must define it before you can use savegames or setting files.
  -- Uncomment the line below and set its value to the name of that directory:
   write_dir = "tuto_quest_en",

In the directory $HOME/.solarus/ or  %UserProfile%\.solarus (for Windows),
it may be that there is an existing subdirectory named "tuto_quest_en".
Just rename it into, for example, "tuto_quest_en_old" and restart the Quest Editor to update the version.

95
Development / Re: Possible to create a level up system?
« on: January 10, 2016, 03:04:16 pm »
Thank you for answering. If I understand you, I should not use my enemy metatable for this, but only enemy scripts.

I also used the metatable for the hero: I created "hero:get_xp()" and "hero:set_xp(xp, boolean)" functions .(Maybe I will also create "hero:add_xp(xp)" ). It works fine, but I had then added a value in the backup file, otherwise I lost the xp value when I backed up and left the game. It would be easier without metatables for backups and to avoid having a value that is outdated.
I have another question:
Is it better to often read a value inside a metatable or a value from a backup file?


I put this code in the quest_manager.lua, maybe it is not the best file.

Code: (Lua) [Select]
-- Initializes some behavior of our hero.
local function initialize_hero()

  local hero_meta = sol.main.get_metatable("hero")
  -- ZL --  Functions for experience points of our hero
  hero_meta.t_xp = {k_xp = nil}
  function hero_meta:set_xp(v_xp, bool)
local game = self:get_game()
v_xp = v_xp or nil
bool = bool or false -- false means "do not save" with a set_value.
if type(v_xp) == "number" and type(bool) == "boolean" or bool == nil then
hero_meta.t_xp.k_xp = v_xp
if bool == true then
game:set_value("xp", v_xp)
end
else
print("error in set_xp(v_xp, bool)")
end
  end
  function hero_meta:get_xp()
local game = self:get_game()
local xp = game:get_value("xp")
if hero_meta.t_xp.k_xp == nil then
local hero = self:get_map():get_hero()
-- print("xp from file = ", xp)
hero:set_xp(xp) -- xp value is now in the metatable
return xp
else
-- print("xp from metatable = ", hero_meta.t_xp.k_xp)
return hero_meta.t_xp.k_xp
end
  end

end


function quest_manager:initialize_quest()

  -- blablabla
  initialize_hero()
  -- blablabla

end

EDIT: I said "backup file" (for save1.dat, save2.dat or save3.dat), but I should have said "buffer";  e.g "xp = 100" is first read in a "backup file" and then the value is in the buffer of the engine. But with my method, I have xp value in the buffer (with other values found in the saveX.dat) + in the metatable which also used memory: I think my method is stupid, and I should avoid to insert data in the metatable.

96
Development / Re: Possible to create a level up system?
« on: January 10, 2016, 12:01:44 pm »
Very interesting subject , at least for me, because I started a little scripting experience points .
(...)
If you want to give xp to each kill of enemy, you can modify the enemy metatable to add the xp value of enemy to the xp counter of your hero (it can be a formula if easy enemy give you less and less xp to avoid the "easy farm" ^^).
(...)
I modified the enemy metatable to add the xp value, but the problem is that the value is common to every enemy. It could be ok for a bonus. I have probably misunderstood.

97
Thank you for your test.
I also tested with Mystery of Solarus (engine v1.4.2) and the problem is the same. It is not really a bug, but I would be happy to find a solution, even if it is just a workaround.

98
I use a destructible entity, not a custom entity.
Not so cool for me. 8)

99
Development / Help: destructible / regenerate / lift / animation and sound
« on: November 21, 2015, 07:59:19 pm »
Hello,

I would like an object that can regenerate.
But, my problem is that when I tick the option to regenerate, there are two animations of destruction:
First, there is an animation of destruction (with the sound) when the hero begins to lift an object
 and then, there is the same animation and the same sound when the hero is destroying this objet (after having throwing it).
The first animation is OK for a "flower bomb", not too ugly for a bush, but strange for a rock or something in wood.

Is there a way to have two animations and sounds or even delete the first animation of destruction and its sound?
I tried with metables, but I did not succeed.

Thanks in advance.

100
I have put some rectangles that have some " ? " into it. I have called it as "exit_sensor", then i have copy it and past it, and ive notice that it is writen "exit_sensor_2", etc, etc.
This is normal. Different sensors with the same name (e;g: "exit_sensor",  "exit_sensor_2",  "exit_sensor_3") can activate the same  object (They can open a same door if the object is a door).

The next thing i wonder, is that the software "Tree" on the left. There is the folder called "script". I wonder if it is there i add the script that i could call as example: "town_exit_sensor.lua"?
No, it is easier in the map script as explained in tutorials (see below). In the tree, "solarus" > "maps" > Right click on the map > and "Open map script"

- Part 13: Lost Woods (youtube)
- Chapitre 13 : Les Bois Perdus (youtube)
- Chapitre 13 : Les Bois Perdus (Adaptation écrite)


101
Bugs & Feature requests / Re: Trying to get where the item is ...
« on: October 26, 2015, 12:00:30 pm »
You forgot an "end" in your function.

Otherwise, I would test with something like that:
Code: [Select]
if game:get_value("_item_slot_1") == "bow" or game:get_value("_item_slot_2") == "bow" then

102
Development / Re: Help? :/
« on: October 18, 2015, 12:35:09 pm »
I use repl.it to test and understand basic commands of Lua (There are different languages).



103
Your projects / Re: Solarus - ZL project
« on: October 18, 2015, 04:12:55 am »
Thank you, polyglot762  :)
I tried to follow the board members of another forum: new clouds, smaller sun and softer color frame at the horizon.
The island is slightly adapted to the new pixel art .



New pixel art with 11 colors again, most of which are taken from the first pixel art:


EDIT:
Just a mockup of a dragon of fire. Hope it will not too difficult to script. ;)


EDIT2:
A memory with Solarus!!! Solarus is so 8).  I want to improve my scripting skills before beginning to animate for example a boss with all its attacks.
The Memory will be a mini game, but I think it will be include it in my main project.
In-game:

104
Your projects / Re: Solarus - ZL project
« on: October 11, 2015, 01:28:04 am »
I drew this background image which will be probably used for the credits . There are 11 colors.

Are the colors of the image correct? Clouds not too ugly ?
Things that are not corrects?


105
Development / Re: Scrolling credits
« on: October 10, 2015, 05:57:55 pm »
I share my script if you like. I think it's pretty simple to customize.

Pages: 1 ... 5 6 [7] 8 9 10