Jump to content

How to? Because this does nothing.


XirmiX

Recommended Posts

I tried to get some modding done and learn a bit about lua. So many people are angry for the Winter kill start that I wanted to make a mod, which provides starting items for DST players. First I tried something simple, like three flint to be added. I assumed that what I write is gonna have to be global, because all characters would need them. This is what I wrote in the modmain.lua:

start_inv={	"flint",	"flint",	"flint",}

I'm guessing it needs to be local. But... where? And how do I make this apply to ALL characters without tossing out their special items (if they have any)? And how do I make them be different depending on the seasons? I'm assuming it's something like:

if season = summer and if daytime = day thenstart_inv={"flint" 5,}

But I'm no programming expert, so what the heck do I know? I know how it works, but I don't know how to write it :/

Link to comment
Share on other sites

Try adding a condition that only takes place when you want it to take place (e.g. Maxwells intro, age component, maybe there's an event for that?). Starting inventories are player-specific as far as I know.

And how do I do that? Do I write something like:

local SpawnStartItems

Then what?

Link to comment
Share on other sites

And how do I do that?

Then what?

oh dear, you were not lying...

local SpawnPrefab = GLOBAL.SpawnPrefab --only need this once per filelocal function SpawnDemo()    for i = 1, NUM_ITEMS do          local item = SpawnPrefab(NAME_ITEM)         item.Transform:SetPosition(GetPlayer():GetPosition()) --simple placement, no offset.    endendif CONDITIONS.START_INV then    SpawnDemo()  --instead, you could put the for-loop in directly    --or maybe add other stuff hereend

The things in caps are tuning variables, you have to declare them at the top or replace them with the numbers needed directly.

 

I literally just came up with that, no guarantees!

 

EDIT: Added GLOBAL

 

EDIT2: Like by DEBUGMAN omg I can't believe! Actually, Simplex liked me before, so this isn't too exciting. I'm popular! ( - :

Link to comment
Share on other sites

oh dear, you were not lying...

local SpawnPrefab = GLOBAL.SpawnPrefab --only need this once per filelocal function SpawnDemo()    for i = 1, NUM_ITEMS do          local item = SpawnPrefab(NAME_ITEM)         item.Transform:SetPosition(GetPlayer():GetPosition()) --simple placement, no offset.    endendif CONDITIONS.START_INV then    SpawnDemo()  --instead, you could put the for-loop in directly    --or maybe add other stuff hereend

The things in caps are tuning variables, you have to declare them at the top or replace them with the numbers needed directly.

 

I literally just came up with that, no guarantees!

 

EDIT: Added GLOBAL

I didn't understand any of that. What is simple placement? What is offset? Why do you need a loop? What do you mean "directly"?

 

Because I can't even... What, what is that code supposed to tell/do? Hmmm? Hmmm? I'm not here to spawn prefabs, but to set people with certain prefabs in their INVENTORY. Was I not clear on that at the beginning?

Link to comment
Share on other sites

I didn't understand any of that. What is simple placement? What is offset? Why do you need a loop? What do you mean "directly"?

 

I'll understand that as epistemophilia, and not blunt inexperience. Let's go over it again in detail! Everything after two -- is a comment in Lua, that means it's not code.

local SpawnPrefab = GLOBAL.SpawnPrefab --This allows us to use the command whenever we want to.local function SpawnDemo() --This is a simple function, a command if you will. (it is local, that means it is only available in this file)    for i = 1, NUM_ITEMS do --This is a for-loop. It executes the code below, increasing 'i' by one everytime. When 'i' hits the second number, the loop stops.          local item = SpawnPrefab(NAME_ITEM)--This executes the spawn command and assigns a variable to that prefab. ('item' refers to the prefab created)         item.Transform:SetPosition(GetPlayer():GetPosition()) --This sets the items position to the players position.    end --every loop and function needs an 'end'endif CONDITIONS.START_INV then --This is an if-clause. Only when the condition is 'true', it'll run the code below.    SpawnDemo()  --This calls our function. You could move the code here directly, but it is more structured this way. Also, this way allows to re-use the function elsewhere.end --if then also needs an end

Any questions?

 

EDIT: Oh, and you may want to consider the Lua-Users wiki and forums to research even more. It's useful for looking up basic functions and syntax sugar.

Link to comment
Share on other sites

I'll understand that as epistemophilia, and not blunt inexperience. Let's go over it again in detail! Everything after two -- is a comment in Lua, that means it's not code.

local SpawnPrefab = GLOBAL.SpawnPrefab --This allows us to use the command whenever we want to.local function SpawnDemo() --This is a simple function, a command if you will. (it is local, that means it is only available in this file)    for i = 1, NUM_ITEMS do --This is a for-loop. It executes the code below, increasing 'i' by one everytime. When 'i' hits the second number, the loop stops.          local item = SpawnPrefab(NAME_ITEM)--This executes the spawn command and assigns a variable to that prefab. ('item' refers to the prefab created)         item.Transform:SetPosition(GetPlayer():GetPosition()) --This sets the items position to the players position.    end --every loop and function needs an 'end'endif CONDITIONS.START_INV then --This is an if-clause. Only when the condition is 'true', it'll run the code below.    SpawnDemo()  --This calls our function. You could move the code here directly, but it is more structured this way. Also, this way allows to re-use the function elsewhere.end --if then also needs an end

Any questions?

 

EDIT: Oh, and you may want to consider the Lua-Users wiki and forums to research even more. It's useful for looking up basic functions and syntax sugar.

I'm starting to get things and I understand why I'd need an "if" statement. But how does this relate to getting stuff in inventory? If you're doing something spawn, that's not inventory. And how am I supposed to write my code from here on out? Take flint for example: How am I gonna make someone spawn with 5 flint in their inventory with a code like this?

 

I know what and "if" is, I know what the "--" is for, I know what local and global is, I know what functions are. But what I don't know is what that code does. This is how far I wrote my code:

local SpawnStartItems = require("prefabs") --I wrote my thingy--I selected all the prefabs I needlocal prefabs ={    "flint",    "grass",    "log",    "twigs",    "winterhat",    "carrot",    "heatrock",}--I set specific ones needed for inventory, which I'm assuming is the "start_inv"local start_inv ={	"flint"; 5,}end --And I put an "end" function to it
Link to comment
Share on other sites

I'm starting to get things and I understand why I'd need an "if" statement. But how does this relate to getting stuff in inventory? If you're doing something spawn, that's not inventory. And how am I supposed to write my code from here on out? Take flint for example: How am I gonna make someone spawn with 5 flint in their inventory with a code like this?

 

I know what and "if" is, I know what the "--" is for, I know what local and global is, I know what functions are. But what I don't know is what that code does. This is how far I wrote my code:

local SpawnStartItems = require("prefabs") --Needless--Also needlesslocal prefabs ={    "flint",    "grass",    "log",    "twigs",    "winterhat",    "carrot",    "heatrock",}--just a local, doesn't do nutslocal start_inv ={	"flint"; 5, --syntax fail}end --end is only needed to mark the end of a function of such

 

I slightly missunderstood your goal. If you want to give the item to the player, rather than drop it next to him, all you need to do is replace the line about setting position with

 

GetPlayer().components.inventory:GiveItem(item,nil,nil,true)

 

You can do that for eveything you want the player to have. You can even use a for-loop to iterate the 'start_inv' table (asuming you get the syntax fixed. I suggest making the elements of the table tables too, which contain a "name" and a "num" variable).

 

I've done some more research. Of course, as it's custom on my side, I entirely ignored that you can try something else too...

 

if not GetPlayer().components.inventory.starting_inventory then --make a table if there isn't one

    GetPlayer().components.inventory.starting_inventory == {}

end

table.insert(GetPlayer().components.inventory.starting_inventory,"flint") --add one flint

 

I am not sure whether that code would run in time, or after the player got their starter items.

Link to comment
Share on other sites

I slightly missunderstood your goal.

:mad-new:

 

 

I've done some more research. Of course, as it's custom on my side, I entirely ignored that you can try something else too...

 

if not GetPlayer().components.inventory.starting_inventory then --make a table if there isn't one

    GetPlayer().components.inventory.starting_inventory == {}

end

table.insert(GetPlayer().components.inventory.starting_inventory,"flint") --add one flint

 

I am not sure whether that code would run in time, or after the player got their starter items.

 

But wouldn't that intervene with players which already start with the special items?

 

And how do I make a table?

Link to comment
Share on other sites

If you want to understand how its works - look into source code.

Start with function which adds player's prefab and follow what it calls.

Many questions will disappear. Its only way to learn this game by yourself, don't think that KLEI have time and will to make full documentation. They are just small indie company.

Link to comment
Share on other sites

But wouldn't that intervene with players which already start with the special items?

 

And how do I make a table?

 

Please don't hate me for being just as stupid as everyone is from time to time. No really, insulting people who want to help you is plain dumb.

 

The function table.insert inserts an element to an existing table. A table is anything with curly brackets (I make an empty table in the code).

 

Please read that link I posted above to understand Lua better. Study some other mods and maybe simple source code. When you're more confident with basic coding, read over those code snippets again.

Link to comment
Share on other sites

Please don't hate me for being just as stupid as everyone is from time to time. No really, insulting people who want to help you is plain dumb.

The function table.insert inserts an element to an existing table. A table is anything with curly brackets (I make an empty table in the code).

Please read that link I posted above to understand Lua better. Study some other mods and maybe simple source code. When you're more confident with basic coding, read over those code snippets again.

Oh, alright then, though I doubt I'll get far. Sorry if I insulted you, I didn't mean to, but I'm surprised that my goal wasn't obvious enough in the first place ;_;

Thanks for helping out, though.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...