Recommended Posts

How to code a character to recover his sanity next to Wilson

Explain what and how

(in code)

local function friendsFOREVER(inst)

local pt = inst:GetPosition()
local range = 10  -- range of ability
local hps = 5 -- sanity amount per tick
local frens = { "wilson" }

    local targets = TheSim:FindEntities(pt.x,pt.y,pt.z, range, frens)
        for _,ent in ipairs(targets) do
            inst.components.sanity:DoDelta(hps)        
        end          
end

local function BFFs(inst)
     if not inst.components.health:IsDead() and not inst:HasTag("playerghost") then                
        friendsFOREVER(inst)    
    end
end

(common_postinit)

local toim = 2 -- time interval between every sanity tick (seconds-ish, it's not 100% accurate)
    inst:DoPeriodicTask(toim, BFFs, nil, inst)

 

 

This code was given to me by one of the forum admins, but I don't understand much

And this code doesn't work

From what they explained to me, you need to add a tag, which tag I don't know

I would be very grateful if you could explain "how and with what it is eaten"

 

(I apologize for my stupidity, I am not the most experienced in programming)

Edited by Ghost_starved

Share this post


Link to post
Share on other sites
Ultroman    725

It's difficult to gauge exactly where in the process of making a mod you are at. Have you any idea what you're doing? Does the character work yet? Where did you put this code? It looks perfectly fine to me, aside from all the variables and functions being named poorly for comedic value, lowering the educational value by adding confusion. While I have explained it a bit below, nothing will suddenly make sense if you don't know how to code. Coding without knowing how to code, is like driving without learning to drive and also the car is an armed F16 that is target-locked on your computer and the computers of everyone you give your code to...so you should learn how to drive the F16 first. You can use the Newcomer Post to help you get started.

Nothing is "eaten" in the code.

The friendsFOREVER function just checks within a range (10) for entities with the tag "wilson" on it. For each entity of the type "wilson" it finds, it adds 5 sanity. It does this using the function FindEntities on the "simulation controller" called TheSim. A function that has a wide array of use-cases.

The BFFs function is basically just a middle-man function, which first checks that the player isn't dead, before firing the friendsFOREVER function.

inst:DoPeriodicTask is a function on entities which calls a function periodically, and you can tell it how often it should call the function and some other parameters.

The toim variable is TIME, set to 2 seconds.

In conclusion, the code ends up continuously calling BFFs every 2 seconds, and every time if the player isn't dead then BFFs calls friendsFOREVER and otherwise it doesn't. friendsFOREVER does its thing. The end.

Edited by Ultroman

Share this post


Link to post
Share on other sites

I cannot do this part of the code

I can send you all the code, but it seems rather strange to me.

(P.s This is the final part of the code . I can send it to you in private messages, but I suppose you don't read them.)

In general, I want my character to recover his sanity next to Wilson (as let's say next to a friendly pig)

Share this post


Link to post
Share on other sites
Ultroman    725

OK, I'll bite. Compress your mod folder to a zip-file and drag it onto a reply here on the forum, and I'll see if I can figure out what went wrong.

  • Big Ups 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this