Page 1 of 1

How to make NPC level up with the player?

Posted: Tue May 20, 2014 11:47 pm
by wuxiangjinxing
It's lonely to fight in the infinite dungeon all by myself. So I would like to make some NPC followers. Currently I have been able to generate the NPC and let the player gain EXP when they kill monsters. However, I cannot figure out how to let them level up with the player, just like an alchemy golem.

I have found the code to false the golem to level up in Actor.lua:

-- Force levelup of the golem
if self.alchemy_golem then
self.alchemy_golem.max_level = self.max_level -- make sure golem can level up with master
self.alchemy_golem:forceLevelup(self.level)
end

However, I cannot figure out how to use the similiar strategy for those NPCs I made. So is it possible for you to teach me how to make it work? Thanks.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 6:33 pm
by Crim, The Red Thunder
You should take a look at some of the for-sale allies in infinite farportal addon. Starkeep did this for some of his. Depending on the version you either got a static one, or one that leveled like golem. Not sure which ones have which version though. But it would give you code to copy over.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 7:25 pm
by StarKeep
Unless I am really out of the loop with my own stuff, I've never applied leveling to companions.


For a quick and dirty solution:

Apply the attribute 'companion' to the npcs.

Superload Actor, and add the following code to actBase: (For examples on how to do this, check nearly any class addon out there.)

Code: Select all

if self and self.companion and self.summoner and self.summoner.level > self.level then
	self:forceLevelup(self.summoner.level)
end

Alternatively, if you like, I could go back and apply this to my old companions addon, and add support for ID into it.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 7:36 pm
by wuxiangjinxing
StarKeep wrote:Unless I am really out of the loop with my own stuff, I've never applied leveling to companions.


For a quick and dirty solution:

Apply the attribute 'companion' to the npcs.

Superload Actor, and add the following code to actBase: (For examples on how to do this, check nearly any class addon out there.)

Code: Select all

if self and self.companion and self.summoner and self.summoner.level > self.level then
	self:forceLevelup(self.summoner.level)
end

Alternatively, if you like, I could go back and apply this to my old companions addon, and add support for ID into it.
Perfect, thanks a lot.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 8:18 pm
by wuxiangjinxing
One more question, to add the attribute "companion" to the npc, shall I just add "companion = true" in the newEntity of that npc? Thanks.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 8:30 pm
by StarKeep
Yep.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 8:36 pm
by wuxiangjinxing
StarKeep wrote:Yep.
Well that does not work in my game. I did exactly what you teached me but the npc still cannot level up.

Can you update your old companion MOD and let me have an example? Thanks.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 8:49 pm
by StarKeep
:oops: So used to dealing with summons for this kind of thing.

This worked correctly after testing:

Code: Select all

if self and self.companion and game.player.level > self.level then
	self:forceLevelup(game.player.level)
end

For insight as to why the first code didn't work, the code was based off my Tower Defense addon.
The issue is that the towers from that addon correctly think of the player as a summoner, so I could easily reference that.
For random companions, however, they had no summoner, and were in no way linked to you, so you instead just reach globally for the player's level.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 8:55 pm
by wuxiangjinxing
StarKeep wrote::oops: So used to dealing with summons for this kind of thing.

This worked correctly after testing:

Code: Select all

if self and self.companion and game.player.level > self.level then
	self:forceLevelup(game.player.level)
end

For insight as to why the first code didn't work, the code was based off my Tower Defense addon.
The issue is that the towers from that addon correctly think of the player as a summoner, so I could easily reference that.
For random companions, however, they had no summoner, and were in no way linked to you, so you instead just reach globally for the player's level.
Thanks, I'm going to try that.

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 9:17 pm
by wuxiangjinxing
Unfortunately that does not work for me either, maybe I made some mistakes and I'm checking it.

This is what I did:

(1) Add the command "Companion = true" at the very end of the newEntity of the NPC

(2) in games/addons/MyMod/Overload/mod/class/Actor.lua, add the following commands at the beginning of function _M:actBase():

if self and self.companion and game.player.level > self.level then
self:forceLevelup(game.player.level)
end

However this does not work for me. Is there anything wrong?

Re: How to make NPC level up with the player?

Posted: Wed May 21, 2014 9:19 pm
by StarKeep
Superload, not Overload.

Look to other addons for examples on how to do so.

Re: How to make NPC level up with the player?

Posted: Thu May 22, 2014 1:45 am
by wuxiangjinxing
OK finally I solved the problem I had. Actually I set the level range of the NPC as (1 to game.player.level) in its newEntity. This caused the NPC to be limited.

In order to let NPC level up, I need to set the max_level of that NPC to either nil or game.player.max_level.

Thank you for all your help.