Jump to content

I need some help with codding pls?


Recommended Posts

So I have two finished characters here. As in the extended sample character, reskined and done (except for the speech thing, I still have to edit all that).
But now I am stumped trying to make a custom code.

Like the first character is supposed to be fast during the day and slow during dusk. And even slower during the night.
I Tried to kinda make the movement speed depend on the time of the day but that didnt seem to work ;w;
I really am not that good at coding either - as you can see haha.

The second character is supposed to grow flowers. Like just spawn flowers, kind of like Webber? But like without the beard haha, just a boy that grows flower. Like after a certain amount of time there are just flowers growing around him. Maybe this is a bit to farfetched to?

I'd be really grateful for any help I can get!

Link to comment
Share on other sites

If you want to have a conditional speed, I would use locomotor::SetExternalSpeedMultiplier for this and have a listener change the multiplier based on the time. Something like this would probably work:

local PHASE_SPEEDS = {
  ["day"] = 1.2,
  ["dusk"] = 0.9,
  ["night"] = 0.6,
}

local function OnPhaseChanged(inst, phase)
  inst.components.locomotor:SetExternalSpeedMultiplier(inst, "phase_multiplier", PHASE_SPEEDS[phase])
end

And then have the following inside the spawn function, assuming that you have inst:

inst:WatchWorldState("phase", OnPhaseChanged)

You can change the speeds to whatever you want, of course. Note that these are multipliers, so it is multiplied with your base speed. It is not additive.

As for the flowers, you can perform a periodic task with random intervals or let it have a certain probability that a flower spawns nearby. I would define a periodic task that runs every N seconds and then have a certain probability that a flower spawns nearby within a certain range. Something like this:

local SECONDS_PER_CHECK = 10
local FLOWER_PROBABILITY = 0.1
local FLOWER_RANGE = 10

local function OnFlowerCheck(inst)
  local x, y, z = inst.Transform:GetWorldPosition()
  local tx = (math.random() * FLOWER_RANGE * 2) - FLOWER_RANGE
  local tz = (math.random() * FLOWER_RANGE * 2) - FLOWER_RANGE
  local p = math.random() < FLOWER_PROBABILITY
  local x1 = x + tx
  local z1 = z + tz
  if p and TheWorld.Map:CanPlantAtPoint(x1, 0, z1) then
    local flower = SpawnPrefab("flower")
    flower.Transform:SetPosition(x1, 0, z1)
  end
end

And then add that to a periodic task. But there are more ways to do this. Note that this does not really treat the range as a circle (more like a square), because I couldn't be bothered to do that. 

Edited by Joachim
Link to comment
Share on other sites

I am sorry to say - that I really can't understand where and how to use this code. ;w;
I get how it works but uhm, i really don't know where exactly to put it so that it works in game
I tried to do it how understood it - in the prefabs folder right?
I really apreciate your help though! I'd just like to ask if you could show me where to put what code? That would be really nice and helpful haha ;w;
Maybe just screenshot it in a template? Please?

Link to comment
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
 Share

×
  • Create New...